ueditor.all.js 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194
  1. /*!
  2. * UEditor
  3. * version: ueditor
  4. * build: Wed Aug 10 2016 11:06:16 GMT+0800 (CST)
  5. */
  6. ;(function() {
  7. var getAuthorization = function() {
  8. const currentUser = sessionStorage.getItem('currentUser')
  9. if (!currentUser) {
  10. throw '未获取到登录信息,请重新登录!'
  11. }
  12. return 'Bearer ' + JSON.parse(currentUser).token
  13. }
  14. // 处理附件预览
  15. var handlerPreviewFile = function(fileId) {
  16. var a = $(
  17. "<a href='" +
  18. window.context.front +
  19. '/file/preview/' +
  20. fileId +
  21. "' target='_blank'>ferry</a>"
  22. ).get(0)
  23. var e = document.createEvent('MouseEvents')
  24. e.initEvent('click', true, true)
  25. a.dispatchEvent(e)
  26. }
  27. // editor.js
  28. UEDITOR_CONFIG = window.UEDITOR_CONFIG || {}
  29. var baidu = window.baidu || {}
  30. window.baidu = baidu
  31. window.UE = baidu.editor = window.UE || {}
  32. UE.plugins = {}
  33. UE.commands = {}
  34. UE.instants = {}
  35. UE.I18N = {}
  36. UE._customizeUI = {}
  37. UE.version = '1.4.3'
  38. var dom = (UE.dom = {})
  39. // core/browser.js
  40. /**
  41. * 浏览器判断模块
  42. * @file
  43. * @module UE.browser
  44. * @since 1.2.6.1
  45. */
  46. /**
  47. * 提供浏览器检测的模块
  48. * @unfile
  49. * @module UE.browser
  50. */
  51. var browser = (UE.browser = (function() {
  52. var agent = navigator.userAgent.toLowerCase(),
  53. opera = window.opera,
  54. browser = {
  55. /**
  56. * @property {boolean} ie 检测当前浏览器是否为IE
  57. * @example
  58. * ```javascript
  59. * if ( UE.browser.ie ) {
  60. * console.log( '当前浏览器是IE' );
  61. * }
  62. * ```
  63. */
  64. ie: /(msie\s|trident.*rv:)([\w.]+)/.test(agent),
  65. /**
  66. * @property {boolean} opera 检测当前浏览器是否为Opera
  67. * @example
  68. * ```javascript
  69. * if ( UE.browser.opera ) {
  70. * console.log( '当前浏览器是Opera' );
  71. * }
  72. * ```
  73. */
  74. opera: !!opera && opera.version,
  75. /**
  76. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  77. * @example
  78. * ```javascript
  79. * if ( UE.browser.webkit ) {
  80. * console.log( '当前浏览器是webkit内核浏览器' );
  81. * }
  82. * ```
  83. */
  84. webkit: agent.indexOf(' applewebkit/') > -1,
  85. /**
  86. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  87. * @example
  88. * ```javascript
  89. * if ( UE.browser.mac ) {
  90. * console.log( '当前浏览器运行在mac平台下' );
  91. * }
  92. * ```
  93. */
  94. mac: agent.indexOf('macintosh') > -1,
  95. /**
  96. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  97. * @example
  98. * ```javascript
  99. * if ( UE.browser.quirks ) {
  100. * console.log( '当前浏览器运行处于“怪异模式”' );
  101. * }
  102. * ```
  103. */
  104. quirks: document.compatMode == 'BackCompat'
  105. }
  106. /**
  107. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  108. * @example
  109. * ```javascript
  110. * if ( UE.browser.gecko ) {
  111. * console.log( '当前浏览器内核是gecko内核' );
  112. * }
  113. * ```
  114. */
  115. browser.gecko =
  116. navigator.product == 'Gecko' &&
  117. !browser.webkit &&
  118. !browser.opera &&
  119. !browser.ie
  120. var version = 0
  121. // Internet Explorer 6.0+
  122. if (browser.ie) {
  123. var v1 = agent.match(/(?:msie\s([\w.]+))/)
  124. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/)
  125. if (v1 && v2 && v1[1] && v2[1]) {
  126. version = Math.max(v1[1] * 1, v2[1] * 1)
  127. } else if (v1 && v1[1]) {
  128. version = v1[1] * 1
  129. } else if (v2 && v2[1]) {
  130. version = v2[1] * 1
  131. } else {
  132. version = 0
  133. }
  134. browser.ie11Compat = document.documentMode == 11
  135. /**
  136. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  137. * @warning 如果浏览器不是IE, 则该值为undefined
  138. * @example
  139. * ```javascript
  140. * if ( UE.browser.ie9Compat ) {
  141. * console.log( '当前浏览器运行在IE9兼容模式下' );
  142. * }
  143. * ```
  144. */
  145. browser.ie9Compat = document.documentMode == 9
  146. /**
  147. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  148. * @warning 如果浏览器不是IE, 则该值为undefined
  149. * @example
  150. * ```javascript
  151. * if ( UE.browser.ie8 ) {
  152. * console.log( '当前浏览器是IE8浏览器' );
  153. * }
  154. * ```
  155. */
  156. browser.ie8 = !!document.documentMode
  157. /**
  158. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  159. * @warning 如果浏览器不是IE, 则该值为undefined
  160. * @example
  161. * ```javascript
  162. * if ( UE.browser.ie8Compat ) {
  163. * console.log( '当前浏览器运行在IE8兼容模式下' );
  164. * }
  165. * ```
  166. */
  167. browser.ie8Compat = document.documentMode == 8
  168. /**
  169. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  170. * @warning 如果浏览器不是IE, 则该值为undefined
  171. * @example
  172. * ```javascript
  173. * if ( UE.browser.ie7Compat ) {
  174. * console.log( '当前浏览器运行在IE7兼容模式下' );
  175. * }
  176. * ```
  177. */
  178. browser.ie7Compat =
  179. (version == 7 && !document.documentMode) || document.documentMode == 7
  180. /**
  181. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  182. * @warning 如果浏览器不是IE, 则该值为undefined
  183. * @example
  184. * ```javascript
  185. * if ( UE.browser.ie6Compat ) {
  186. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  187. * }
  188. * ```
  189. */
  190. browser.ie6Compat = version < 7 || browser.quirks
  191. browser.ie9above = version > 8
  192. browser.ie9below = version < 9
  193. browser.ie11above = version > 10
  194. browser.ie11below = version < 11
  195. }
  196. // Gecko.
  197. if (browser.gecko) {
  198. var geckoRelease = agent.match(/rv:([\d\.]+)/)
  199. if (geckoRelease) {
  200. geckoRelease = geckoRelease[1].split('.')
  201. version =
  202. geckoRelease[0] * 10000 +
  203. (geckoRelease[1] || 0) * 100 +
  204. (geckoRelease[2] || 0) * 1
  205. }
  206. }
  207. /**
  208. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  209. * @warning 如果浏览器不是chrome, 则该值为undefined
  210. * @example
  211. * ```javascript
  212. * if ( UE.browser.chrome ) {
  213. * console.log( '当前浏览器是Chrome' );
  214. * }
  215. * ```
  216. */
  217. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  218. browser.chrome = +RegExp['\x241']
  219. }
  220. /**
  221. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  222. * @warning 如果浏览器不是safari, 则该值为undefined
  223. * @example
  224. * ```javascript
  225. * if ( UE.browser.safari ) {
  226. * console.log( '当前浏览器是Safari' );
  227. * }
  228. * ```
  229. */
  230. if (
  231. /(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) &&
  232. !/chrome/i.test(agent)
  233. ) {
  234. browser.safari = +(RegExp['\x241'] || RegExp['\x242'])
  235. }
  236. // Opera 9.50+
  237. if (browser.opera) version = parseFloat(opera.version())
  238. // WebKit 522+ (Safari 3+)
  239. if (browser.webkit)
  240. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1])
  241. /**
  242. * @property { Number } version 检测当前浏览器版本号
  243. * @remind
  244. * <ul>
  245. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  246. * <li>gecko系列会返回10900,158900等</li>
  247. * <li>webkit系列会返回其build号 (如 522等)</li>
  248. * </ul>
  249. * @example
  250. * ```javascript
  251. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  252. * ```
  253. */
  254. browser.version = version
  255. /**
  256. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  257. * @example
  258. * ```javascript
  259. * if ( UE.browser.isCompatible ) {
  260. * console.log( '浏览器与UEditor能够良好兼容' );
  261. * }
  262. * ```
  263. */
  264. browser.isCompatible =
  265. !browser.mobile &&
  266. ((browser.ie && version >= 6) ||
  267. (browser.gecko && version >= 10801) ||
  268. (browser.opera && version >= 9.5) ||
  269. (browser.air && version >= 1) ||
  270. (browser.webkit && version >= 522) ||
  271. false)
  272. return browser
  273. })())
  274. //快捷方式
  275. var ie = browser.ie,
  276. webkit = browser.webkit,
  277. gecko = browser.gecko,
  278. opera = browser.opera
  279. // core/utils.js
  280. /**
  281. * 工具函数包
  282. * @file
  283. * @module UE.utils
  284. * @since 1.2.6.1
  285. */
  286. /**
  287. * UEditor封装使用的静态工具函数
  288. * @module UE.utils
  289. * @unfile
  290. */
  291. var utils = (UE.utils = {
  292. /**
  293. * 用给定的迭代器遍历对象
  294. * @method each
  295. * @param { Object } obj 需要遍历的对象
  296. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  297. * @example
  298. * ```javascript
  299. * var demoObj = {
  300. * key1: 1,
  301. * key2: 2
  302. * };
  303. *
  304. * //output: key1: 1, key2: 2
  305. * UE.utils.each( demoObj, funciton ( value, key ) {
  306. *
  307. * console.log( key + ":" + value );
  308. *
  309. * } );
  310. * ```
  311. */
  312. /**
  313. * 用给定的迭代器遍历数组或类数组对象
  314. * @method each
  315. * @param { Array } array 需要遍历的数组或者类数组
  316. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  317. * @example
  318. * ```javascript
  319. * var divs = document.getElmentByTagNames( "div" );
  320. *
  321. * //output: 0: DIV, 1: DIV ...
  322. * UE.utils.each( divs, funciton ( value, key ) {
  323. *
  324. * console.log( key + ":" + value.tagName );
  325. *
  326. * } );
  327. * ```
  328. */
  329. each: function(obj, iterator, context) {
  330. if (obj == null) return
  331. if (obj.length === +obj.length) {
  332. for (var i = 0, l = obj.length; i < l; i++) {
  333. if (iterator.call(context, obj[i], i, obj) === false) return false
  334. }
  335. } else {
  336. for (var key in obj) {
  337. if (obj.hasOwnProperty(key)) {
  338. if (iterator.call(context, obj[key], key, obj) === false)
  339. return false
  340. }
  341. }
  342. }
  343. },
  344. /**
  345. * 以给定对象作为原型创建一个新对象
  346. * @method makeInstance
  347. * @param { Object } protoObject 该对象将作为新创建对象的原型
  348. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  349. * @example
  350. * ```javascript
  351. *
  352. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  353. *
  354. * var newObject = UE.utils.makeInstance( protoObject );
  355. * //output: Hello UEditor!
  356. * newObject.sayHello();
  357. * ```
  358. */
  359. makeInstance: function(obj) {
  360. var noop = new Function()
  361. noop.prototype = obj
  362. obj = new noop()
  363. noop.prototype = null
  364. return obj
  365. },
  366. /**
  367. * 将source对象中的属性扩展到target对象上
  368. * @method extend
  369. * @remind 该方法将强制把source对象上的属性复制到target对象上
  370. * @see UE.utils.extend(Object,Object,Boolean)
  371. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  372. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  373. * @return { Object } 返回target对象
  374. * @example
  375. * ```javascript
  376. *
  377. * var target = { name: 'target', sex: 1 },
  378. * source = { name: 'source', age: 17 };
  379. *
  380. * UE.utils.extend( target, source );
  381. *
  382. * //output: { name: 'source', sex: 1, age: 17 }
  383. * console.log( target );
  384. *
  385. * ```
  386. */
  387. /**
  388. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  389. * 源对象属性名相同的属性值。
  390. * @method extend
  391. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  392. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  393. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  394. * @return { Object } 返回target对象
  395. * @example
  396. * ```javascript
  397. *
  398. * var target = { name: 'target', sex: 1 },
  399. * source = { name: 'source', age: 17 };
  400. *
  401. * UE.utils.extend( target, source, true );
  402. *
  403. * //output: { name: 'target', sex: 1, age: 17 }
  404. * console.log( target );
  405. *
  406. * ```
  407. */
  408. extend: function(t, s, b) {
  409. if (s) {
  410. for (var k in s) {
  411. if (!b || !t.hasOwnProperty(k)) {
  412. t[k] = s[k]
  413. }
  414. }
  415. }
  416. return t
  417. },
  418. /**
  419. * 将给定的多个对象的属性复制到目标对象target上
  420. * @method extend2
  421. * @remind 该方法将强制把源对象上的属性复制到target对象上
  422. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  423. * 将会覆盖掉之前的值。
  424. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  425. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  426. * @return { Object } 返回target对象
  427. * @example
  428. * ```javascript
  429. *
  430. * var target = {},
  431. * source1 = { name: 'source', age: 17 },
  432. * source2 = { title: 'dev' };
  433. *
  434. * UE.utils.extend2( target, source1, source2 );
  435. *
  436. * //output: { name: 'source', age: 17, title: 'dev' }
  437. * console.log( target );
  438. *
  439. * ```
  440. */
  441. extend2: function(t) {
  442. var a = arguments
  443. for (var i = 1; i < a.length; i++) {
  444. var x = a[i]
  445. for (var k in x) {
  446. if (!t.hasOwnProperty(k)) {
  447. t[k] = x[k]
  448. }
  449. }
  450. }
  451. return t
  452. },
  453. /**
  454. * 模拟继承机制, 使得subClass继承自superClass
  455. * @method inherits
  456. * @param { Object } subClass 子类对象
  457. * @param { Object } superClass 超类对象
  458. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  459. * @return { Object } 继承superClass后的子类对象
  460. * @example
  461. * ```javascript
  462. * function SuperClass(){
  463. * this.name = "小李";
  464. * }
  465. *
  466. * SuperClass.prototype = {
  467. * hello:function(str){
  468. * console.log(this.name + str);
  469. * }
  470. * }
  471. *
  472. * function SubClass(){
  473. * this.name = "小张";
  474. * }
  475. *
  476. * UE.utils.inherits(SubClass,SuperClass);
  477. *
  478. * var sub = new SubClass();
  479. * //output: '小张早上好!
  480. * sub.hello("早上好!");
  481. * ```
  482. */
  483. inherits: function(subClass, superClass) {
  484. var oldP = subClass.prototype,
  485. newP = utils.makeInstance(superClass.prototype)
  486. utils.extend(newP, oldP, true)
  487. subClass.prototype = newP
  488. return (newP.constructor = subClass)
  489. },
  490. /**
  491. * 用指定的context对象作为函数fn的上下文
  492. * @method bind
  493. * @param { Function } fn 需要绑定上下文的函数对象
  494. * @param { Object } content 函数fn新的上下文对象
  495. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  496. * @example
  497. * ```javascript
  498. *
  499. * var name = 'window',
  500. * newTest = null;
  501. *
  502. * function test () {
  503. * console.log( this.name );
  504. * }
  505. *
  506. * newTest = UE.utils.bind( test, { name: 'object' } );
  507. *
  508. * //output: object
  509. * newTest();
  510. *
  511. * //output: window
  512. * test();
  513. *
  514. * ```
  515. */
  516. bind: function(fn, context) {
  517. return function() {
  518. return fn.apply(context, arguments)
  519. }
  520. },
  521. /**
  522. * 创建延迟指定时间后执行的函数fn
  523. * @method defer
  524. * @param { Function } fn 需要延迟执行的函数对象
  525. * @param { int } delay 延迟的时间, 单位是毫秒
  526. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  527. * 而不能保证刚好到达延迟时间时执行。
  528. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  529. * @example
  530. * ```javascript
  531. * var start = 0;
  532. *
  533. * function test(){
  534. * console.log( new Date() - start );
  535. * }
  536. *
  537. * var testDefer = UE.utils.defer( test, 1000 );
  538. * //
  539. * start = new Date();
  540. * //output: (大约在1000毫秒之后输出) 1000
  541. * testDefer();
  542. * ```
  543. */
  544. /**
  545. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  546. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  547. * @method defer
  548. * @param { Function } fn 需要延迟执行的函数对象
  549. * @param { int } delay 延迟的时间, 单位是毫秒
  550. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  551. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  552. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  553. * 而不能保证刚好到达延迟时间时执行。
  554. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  555. * @example
  556. * ```javascript
  557. *
  558. * function test(){
  559. * console.log(1);
  560. * }
  561. *
  562. * var testDefer = UE.utils.defer( test, 1000, true );
  563. *
  564. * //output: (两次调用仅有一次输出) 1
  565. * testDefer();
  566. * testDefer();
  567. * ```
  568. */
  569. defer: function(fn, delay, exclusion) {
  570. var timerID
  571. return function() {
  572. if (exclusion) {
  573. clearTimeout(timerID)
  574. }
  575. timerID = setTimeout(fn, delay)
  576. }
  577. },
  578. /**
  579. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  580. * @method indexOf
  581. * @remind 该方法的匹配过程使用的是恒等“===”
  582. * @param { Array } array 需要查找的数组对象
  583. * @param { * } item 需要在目标数组中查找的值
  584. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  585. * @example
  586. * ```javascript
  587. * var item = 1,
  588. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  589. *
  590. * //output: 4
  591. * console.log( UE.utils.indexOf( arr, item ) );
  592. * ```
  593. */
  594. /**
  595. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  596. * @method indexOf
  597. * @remind 该方法的匹配过程使用的是恒等“===”
  598. * @param { Array } array 需要查找的数组对象
  599. * @param { * } item 需要在目标数组中查找的值
  600. * @param { int } start 搜索的起始位置
  601. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  602. * @example
  603. * ```javascript
  604. * var item = 1,
  605. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  606. *
  607. * //output: 9
  608. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  609. * ```
  610. */
  611. indexOf: function(array, item, start) {
  612. var index = -1
  613. start = this.isNumber(start) ? start : 0
  614. this.each(array, function(v, i) {
  615. if (i >= start && v === item) {
  616. index = i
  617. return false
  618. }
  619. })
  620. return index
  621. },
  622. /**
  623. * 移除数组array中所有的元素item
  624. * @method removeItem
  625. * @param { Array } array 要移除元素的目标数组
  626. * @param { * } item 将要被移除的元素
  627. * @remind 该方法的匹配过程使用的是恒等“===”
  628. * @example
  629. * ```javascript
  630. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  631. *
  632. * UE.utils.removeItem( arr, 4 );
  633. * //output: [ 5, 7, 1, 3, 6 ]
  634. * console.log( arr );
  635. *
  636. * ```
  637. */
  638. removeItem: function(array, item) {
  639. for (var i = 0, l = array.length; i < l; i++) {
  640. if (array[i] === item) {
  641. array.splice(i, 1)
  642. i--
  643. }
  644. }
  645. },
  646. /**
  647. * 删除字符串str的首尾空格
  648. * @method trim
  649. * @param { String } str 需要删除首尾空格的字符串
  650. * @return { String } 删除了首尾的空格后的字符串
  651. * @example
  652. * ```javascript
  653. *
  654. * var str = " UEdtior ";
  655. *
  656. * //output: 9
  657. * console.log( str.length );
  658. *
  659. * //output: 7
  660. * console.log( UE.utils.trim( " UEdtior " ).length );
  661. *
  662. * //output: 9
  663. * console.log( str.length );
  664. *
  665. * ```
  666. */
  667. trim: function(str) {
  668. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '')
  669. },
  670. /**
  671. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  672. * @method listToMap
  673. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  674. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  675. * @return { Object } 转化之后的hash对象
  676. * @example
  677. * ```javascript
  678. *
  679. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  680. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  681. *
  682. * ```
  683. */
  684. /**
  685. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  686. * @method listToMap
  687. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  688. * @param { Array } arr 字符串数组
  689. * @return { Object } 转化之后的hash对象
  690. * @example
  691. * ```javascript
  692. *
  693. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  694. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  695. *
  696. * ```
  697. */
  698. listToMap: function(list) {
  699. if (!list) return {}
  700. list = utils.isArray(list) ? list : list.split(',')
  701. for (var i = 0, ci, obj = {}; (ci = list[i++]); ) {
  702. obj[ci.toUpperCase()] = obj[ci] = 1
  703. }
  704. return obj
  705. },
  706. /**
  707. * 将str中的html符号转义,将转义“',&,<,",>”五个字符
  708. * @method unhtml
  709. * @param { String } str 需要转义的字符串
  710. * @return { String } 转义后的字符串
  711. * @example
  712. * ```javascript
  713. * var html = '<body>&</body>';
  714. *
  715. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  716. * console.log( UE.utils.unhtml( html ) );
  717. *
  718. * ```
  719. */
  720. unhtml: function(str, reg) {
  721. return str
  722. ? str.replace(
  723. reg || /[&<">'](?:(amp|lt|quot|gt|#39|nbsp|#\d+);)?/g,
  724. function(a, b) {
  725. if (b) {
  726. return a
  727. } else {
  728. return {
  729. '<': '&lt;',
  730. '&': '&amp;',
  731. '"': '&quot;',
  732. '>': '&gt;',
  733. "'": '&#39;'
  734. }[a]
  735. }
  736. }
  737. )
  738. : ''
  739. },
  740. /**
  741. * 将url中的html字符转义, 仅转义 ', ", <, > 四个字符
  742. * @param { String } str 需要转义的字符串
  743. * @param { RegExp } reg 自定义的正则
  744. * @return { String } 转义后的字符串
  745. */
  746. unhtmlForUrl: function(str, reg) {
  747. return str
  748. ? str.replace(reg || /[<">']/g, function(a) {
  749. return {
  750. '<': '&lt;',
  751. '&': '&amp;',
  752. '"': '&quot;',
  753. '>': '&gt;',
  754. "'": '&#39;'
  755. }[a]
  756. })
  757. : ''
  758. },
  759. /**
  760. * 将str中的转义字符还原成html字符
  761. * @see UE.utils.unhtml(String);
  762. * @method html
  763. * @param { String } str 需要逆转义的字符串
  764. * @return { String } 逆转义后的字符串
  765. * @example
  766. * ```javascript
  767. *
  768. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  769. *
  770. * //output: <body>&</body>
  771. * console.log( UE.utils.html( str ) );
  772. *
  773. * ```
  774. */
  775. html: function(str) {
  776. return str
  777. ? str.replace(/&((g|l|quo)t|amp|#39|nbsp);/g, function(m) {
  778. return {
  779. '&lt;': '<',
  780. '&amp;': '&',
  781. '&quot;': '"',
  782. '&gt;': '>',
  783. '&#39;': "'",
  784. '&nbsp;': ' '
  785. }[m]
  786. })
  787. : ''
  788. },
  789. /**
  790. * 将css样式转换为驼峰的形式
  791. * @method cssStyleToDomStyle
  792. * @param { String } cssName 需要转换的css样式名
  793. * @return { String } 转换成驼峰形式后的css样式名
  794. * @example
  795. * ```javascript
  796. *
  797. * var str = 'border-top';
  798. *
  799. * //output: borderTop
  800. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  801. *
  802. * ```
  803. */
  804. cssStyleToDomStyle: (function() {
  805. var test = document.createElement('div').style,
  806. cache = {
  807. float:
  808. test.cssFloat != undefined
  809. ? 'cssFloat'
  810. : test.styleFloat != undefined
  811. ? 'styleFloat'
  812. : 'float'
  813. }
  814. return function(cssName) {
  815. return (
  816. cache[cssName] ||
  817. (cache[cssName] = cssName
  818. .toLowerCase()
  819. .replace(/-./g, function(match) {
  820. return match.charAt(1).toUpperCase()
  821. }))
  822. )
  823. }
  824. })(),
  825. /**
  826. * 动态加载文件到doc中
  827. * @method loadFile
  828. * @param { DomDocument } document 需要加载资源文件的文档对象
  829. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  830. * @example
  831. * ```javascript
  832. *
  833. * UE.utils.loadFile( document, {
  834. * src:"test.js",
  835. * tag:"script",
  836. * type:"text/javascript",
  837. * defer:"defer"
  838. * } );
  839. *
  840. * ```
  841. */
  842. /**
  843. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  844. * @method loadFile
  845. * @param { DomDocument } document 需要加载资源文件的文档对象
  846. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  847. * @param { Function } fn 资源文件加载成功之后执行的回调
  848. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  849. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  850. * @example
  851. * ```javascript
  852. *
  853. * UE.utils.loadFile( document, {
  854. * src:"test.js",
  855. * tag:"script",
  856. * type:"text/javascript",
  857. * defer:"defer"
  858. * }, function () {
  859. * console.log('加载成功');
  860. * } );
  861. *
  862. * ```
  863. */
  864. loadFile: (function() {
  865. var tmpList = []
  866. function getItem(doc, obj) {
  867. try {
  868. for (var i = 0, ci; (ci = tmpList[i++]); ) {
  869. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  870. return ci
  871. }
  872. }
  873. } catch (e) {
  874. return null
  875. }
  876. }
  877. return function(doc, obj, fn) {
  878. var item = getItem(doc, obj)
  879. if (item) {
  880. if (item.ready) {
  881. fn && fn()
  882. } else {
  883. item.funs.push(fn)
  884. }
  885. return
  886. }
  887. tmpList.push({
  888. doc: doc,
  889. url: obj.src || obj.href,
  890. funs: [fn]
  891. })
  892. if (!doc.body) {
  893. var html = []
  894. for (var p in obj) {
  895. if (p == 'tag') continue
  896. html.push(p + '="' + obj[p] + '"')
  897. }
  898. doc.write(
  899. '<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>'
  900. )
  901. return
  902. }
  903. if (obj.id && doc.getElementById(obj.id)) {
  904. return
  905. }
  906. var element = doc.createElement(obj.tag)
  907. delete obj.tag
  908. for (var p in obj) {
  909. element.setAttribute(p, obj[p])
  910. }
  911. element.onload = element.onreadystatechange = function() {
  912. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  913. item = getItem(doc, obj)
  914. if (item.funs.length > 0) {
  915. item.ready = 1
  916. for (var fi; (fi = item.funs.pop()); ) {
  917. fi()
  918. }
  919. }
  920. element.onload = element.onreadystatechange = null
  921. }
  922. }
  923. element.onerror = function() {
  924. throw Error(
  925. 'The load ' +
  926. (obj.href || obj.src) +
  927. ' fails,check the url settings of file ueditor.config.js '
  928. )
  929. }
  930. doc.getElementsByTagName('head')[0].appendChild(element)
  931. }
  932. })(),
  933. /**
  934. * 判断obj对象是否为空
  935. * @method isEmptyObject
  936. * @param { * } obj 需要判断的对象
  937. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  938. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  939. * @return { Boolean } 对象是否为空
  940. * @example
  941. * ```javascript
  942. *
  943. * //output: true
  944. * console.log( UE.utils.isEmptyObject( {} ) );
  945. *
  946. * //output: true
  947. * console.log( UE.utils.isEmptyObject( [] ) );
  948. *
  949. * //output: true
  950. * console.log( UE.utils.isEmptyObject( "" ) );
  951. *
  952. * //output: false
  953. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  954. *
  955. * //output: false
  956. * console.log( UE.utils.isEmptyObject( [1] ) );
  957. *
  958. * //output: false
  959. * console.log( UE.utils.isEmptyObject( "1" ) );
  960. *
  961. * ```
  962. */
  963. isEmptyObject: function(obj) {
  964. if (obj == null) return true
  965. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0
  966. for (var key in obj) if (obj.hasOwnProperty(key)) return false
  967. return true
  968. },
  969. /**
  970. * 把rgb格式的颜色值转换成16进制格式
  971. * @method fixColor
  972. * @param { String } rgb格式的颜色值
  973. * @param { String }
  974. * @example
  975. * rgb(255,255,255) => "#ffffff"
  976. */
  977. fixColor: function(name, value) {
  978. if (/color/i.test(name) && /rgba?/.test(value)) {
  979. var array = value.split(',')
  980. if (array.length > 3) return ''
  981. value = '#'
  982. for (var i = 0, color; (color = array[i++]); ) {
  983. color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16)
  984. value += color.length == 1 ? '0' + color : color
  985. }
  986. value = value.toUpperCase()
  987. }
  988. return value
  989. },
  990. /**
  991. * 只针对border,padding,margin做了处理,因为性能问题
  992. * @public
  993. * @function
  994. * @param {String} val style字符串
  995. */
  996. optCss: function(val) {
  997. var padding, margin, border
  998. val = val.replace(
  999. /(padding|margin|border)\-([^:]+):([^;]+);?/gi,
  1000. function(str, key, name, val) {
  1001. if (val.split(' ').length == 1) {
  1002. switch (key) {
  1003. case 'padding':
  1004. !padding && (padding = {})
  1005. padding[name] = val
  1006. return ''
  1007. case 'margin':
  1008. !margin && (margin = {})
  1009. margin[name] = val
  1010. return ''
  1011. case 'border':
  1012. return val == 'initial' ? '' : str
  1013. }
  1014. }
  1015. return str
  1016. }
  1017. )
  1018. function opt(obj, name) {
  1019. if (!obj) {
  1020. return ''
  1021. }
  1022. var t = obj.top,
  1023. b = obj.bottom,
  1024. l = obj.left,
  1025. r = obj.right,
  1026. val = ''
  1027. if (!t || !l || !b || !r) {
  1028. for (var p in obj) {
  1029. val += ';' + name + '-' + p + ':' + obj[p] + ';'
  1030. }
  1031. } else {
  1032. val +=
  1033. ';' +
  1034. name +
  1035. ':' +
  1036. (t == b && b == l && l == r
  1037. ? t
  1038. : t == b && l == r
  1039. ? t + ' ' + l
  1040. : l == r
  1041. ? t + ' ' + l + ' ' + b
  1042. : t + ' ' + r + ' ' + b + ' ' + l) +
  1043. ';'
  1044. }
  1045. return val
  1046. }
  1047. val += opt(padding, 'padding') + opt(margin, 'margin')
  1048. return val
  1049. .replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, '')
  1050. .replace(/;([ \n\r\t]+)|\1;/g, ';')
  1051. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function(a, b) {
  1052. return b ? b + ';;' : ';'
  1053. })
  1054. },
  1055. /**
  1056. * 克隆对象
  1057. * @method clone
  1058. * @param { Object } source 源对象
  1059. * @return { Object } source的一个副本
  1060. */
  1061. /**
  1062. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  1063. * @method clone
  1064. * @param { Object } source 源对象
  1065. * @param { Object } target 目标对象
  1066. * @return { Object } 附加了source对象所有属性的target对象
  1067. */
  1068. clone: function(source, target) {
  1069. var tmp
  1070. target = target || {}
  1071. for (var i in source) {
  1072. if (source.hasOwnProperty(i)) {
  1073. tmp = source[i]
  1074. if (typeof tmp == 'object') {
  1075. target[i] = utils.isArray(tmp) ? [] : {}
  1076. utils.clone(source[i], target[i])
  1077. } else {
  1078. target[i] = tmp
  1079. }
  1080. }
  1081. }
  1082. return target
  1083. },
  1084. /**
  1085. * 把cm/pt为单位的值转换为px为单位的值
  1086. * @method transUnitToPx
  1087. * @param { String } 待转换的带单位的字符串
  1088. * @return { String } 转换为px为计量单位的值的字符串
  1089. * @example
  1090. * ```javascript
  1091. *
  1092. * //output: 500px
  1093. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1094. *
  1095. * //output: 27px
  1096. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1097. *
  1098. * ```
  1099. */
  1100. transUnitToPx: function(val) {
  1101. if (!/(pt|cm)/.test(val)) {
  1102. return val
  1103. }
  1104. var unit
  1105. val.replace(/([\d.]+)(\w+)/, function(str, v, u) {
  1106. val = v
  1107. unit = u
  1108. })
  1109. switch (unit) {
  1110. case 'cm':
  1111. val = parseFloat(val) * 25
  1112. break
  1113. case 'pt':
  1114. val = Math.round((parseFloat(val) * 96) / 72)
  1115. }
  1116. return val + (val ? 'px' : '')
  1117. },
  1118. /**
  1119. * 在dom树ready之后执行给定的回调函数
  1120. * @method domReady
  1121. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1122. * @param { Function } fn dom树ready之后的回调函数
  1123. * @example
  1124. * ```javascript
  1125. *
  1126. * UE.utils.domReady( function () {
  1127. *
  1128. * console.log('123');
  1129. *
  1130. * } );
  1131. *
  1132. * ```
  1133. */
  1134. domReady: (function() {
  1135. var fnArr = []
  1136. function doReady(doc) {
  1137. //确保onready只执行一次
  1138. doc.isReady = true
  1139. for (var ci; (ci = fnArr.pop()); ci()) {}
  1140. }
  1141. return function(onready, win) {
  1142. win = win || window
  1143. var doc = win.document
  1144. onready && fnArr.push(onready)
  1145. if (doc.readyState === 'complete') {
  1146. doReady(doc)
  1147. } else {
  1148. doc.isReady && doReady(doc)
  1149. if (browser.ie && browser.version != 11) {
  1150. ;(function() {
  1151. if (doc.isReady) return
  1152. try {
  1153. doc.documentElement.doScroll('left')
  1154. } catch (error) {
  1155. setTimeout(arguments.callee, 0)
  1156. return
  1157. }
  1158. doReady(doc)
  1159. })()
  1160. win.attachEvent('onload', function() {
  1161. doReady(doc)
  1162. })
  1163. } else {
  1164. doc.addEventListener(
  1165. 'DOMContentLoaded',
  1166. function() {
  1167. doc.removeEventListener(
  1168. 'DOMContentLoaded',
  1169. arguments.callee,
  1170. false
  1171. )
  1172. doReady(doc)
  1173. },
  1174. false
  1175. )
  1176. win.addEventListener(
  1177. 'load',
  1178. function() {
  1179. doReady(doc)
  1180. },
  1181. false
  1182. )
  1183. }
  1184. }
  1185. }
  1186. })(),
  1187. /**
  1188. * 动态添加css样式
  1189. * @method cssRule
  1190. * @param { String } 节点名称
  1191. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1192. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1193. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1194. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1195. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1196. */
  1197. cssRule:
  1198. browser.ie && browser.version != 11
  1199. ? function(key, style, doc) {
  1200. var indexList, index
  1201. if (
  1202. style === undefined ||
  1203. (style && style.nodeType && style.nodeType == 9)
  1204. ) {
  1205. //获取样式
  1206. doc =
  1207. style && style.nodeType && style.nodeType == 9
  1208. ? style
  1209. : doc || document
  1210. indexList = doc.indexList || (doc.indexList = {})
  1211. index = indexList[key]
  1212. if (index !== undefined) {
  1213. return doc.styleSheets[index].cssText
  1214. }
  1215. return undefined
  1216. }
  1217. doc = doc || document
  1218. indexList = doc.indexList || (doc.indexList = {})
  1219. index = indexList[key]
  1220. //清除样式
  1221. if (style === '') {
  1222. if (index !== undefined) {
  1223. doc.styleSheets[index].cssText = ''
  1224. delete indexList[key]
  1225. return true
  1226. }
  1227. return false
  1228. }
  1229. //添加样式
  1230. if (index !== undefined) {
  1231. sheetStyle = doc.styleSheets[index]
  1232. } else {
  1233. sheetStyle = doc.createStyleSheet(
  1234. '',
  1235. (index = doc.styleSheets.length)
  1236. )
  1237. indexList[key] = index
  1238. }
  1239. sheetStyle.cssText = style
  1240. }
  1241. : function(key, style, doc) {
  1242. var head, node
  1243. if (
  1244. style === undefined ||
  1245. (style && style.nodeType && style.nodeType == 9)
  1246. ) {
  1247. //获取样式
  1248. doc =
  1249. style && style.nodeType && style.nodeType == 9
  1250. ? style
  1251. : doc || document
  1252. node = doc.getElementById(key)
  1253. return node ? node.innerHTML : undefined
  1254. }
  1255. doc = doc || document
  1256. node = doc.getElementById(key)
  1257. //清除样式
  1258. if (style === '') {
  1259. if (node) {
  1260. node.parentNode.removeChild(node)
  1261. return true
  1262. }
  1263. return false
  1264. }
  1265. //添加样式
  1266. if (node) {
  1267. node.innerHTML = style
  1268. } else {
  1269. node = doc.createElement('style')
  1270. node.id = key
  1271. node.innerHTML = style
  1272. doc.getElementsByTagName('head')[0].appendChild(node)
  1273. }
  1274. },
  1275. sort: function(array, compareFn) {
  1276. compareFn =
  1277. compareFn ||
  1278. function(item1, item2) {
  1279. return item1.localeCompare(item2)
  1280. }
  1281. for (var i = 0, len = array.length; i < len; i++) {
  1282. for (var j = i, length = array.length; j < length; j++) {
  1283. if (compareFn(array[i], array[j]) > 0) {
  1284. var t = array[i]
  1285. array[i] = array[j]
  1286. array[j] = t
  1287. }
  1288. }
  1289. }
  1290. return array
  1291. },
  1292. serializeParam: function(json) {
  1293. var strArr = []
  1294. for (var i in json) {
  1295. //忽略默认的几个参数
  1296. if (i == 'method' || i == 'timeout' || i == 'async') continue
  1297. //传递过来的对象和函数不在提交之列
  1298. if (
  1299. !(
  1300. (typeof json[i]).toLowerCase() == 'function' ||
  1301. (typeof json[i]).toLowerCase() == 'object'
  1302. )
  1303. ) {
  1304. strArr.push(encodeURIComponent(i) + '=' + encodeURIComponent(json[i]))
  1305. } else if (utils.isArray(json[i])) {
  1306. //支持传数组内容
  1307. for (var j = 0; j < json[i].length; j++) {
  1308. strArr.push(
  1309. encodeURIComponent(i) + '[]=' + encodeURIComponent(json[i][j])
  1310. )
  1311. }
  1312. }
  1313. }
  1314. return strArr.join('&')
  1315. },
  1316. formatUrl: function(url) {
  1317. var u = url.replace(/&&/g, '&')
  1318. u = u.replace(/\?&/g, '?')
  1319. u = u.replace(/&$/g, '')
  1320. u = u.replace(/&#/g, '#')
  1321. u = u.replace(/&+/g, '&')
  1322. return u
  1323. },
  1324. isCrossDomainUrl: function(url) {
  1325. var a = document.createElement('a')
  1326. a.href = url
  1327. if (browser.ie) {
  1328. a.href = a.href
  1329. }
  1330. return !(
  1331. a.protocol == location.protocol &&
  1332. a.hostname == location.hostname &&
  1333. (a.port == location.port ||
  1334. (a.port == '80' && location.port == '') ||
  1335. (a.port == '' && location.port == '80'))
  1336. )
  1337. },
  1338. clearEmptyAttrs: function(obj) {
  1339. for (var p in obj) {
  1340. if (obj[p] === '') {
  1341. delete obj[p]
  1342. }
  1343. }
  1344. return obj
  1345. },
  1346. str2json: function(s) {
  1347. if (!utils.isString(s)) return null
  1348. if (window.JSON) {
  1349. return JSON.parse(s)
  1350. } else {
  1351. return new Function('return ' + utils.trim(s || ''))()
  1352. }
  1353. },
  1354. json2str: (function() {
  1355. if (window.JSON) {
  1356. return JSON.stringify
  1357. } else {
  1358. var escapeMap = {
  1359. '\b': '\\b',
  1360. '\t': '\\t',
  1361. '\n': '\\n',
  1362. '\f': '\\f',
  1363. '\r': '\\r',
  1364. '"': '\\"',
  1365. '\\': '\\\\'
  1366. }
  1367. function encodeString(source) {
  1368. if (/["\\\x00-\x1f]/.test(source)) {
  1369. source = source.replace(/["\\\x00-\x1f]/g, function(match) {
  1370. var c = escapeMap[match]
  1371. if (c) {
  1372. return c
  1373. }
  1374. c = match.charCodeAt()
  1375. return (
  1376. '\\u00' +
  1377. Math.floor(c / 16).toString(16) +
  1378. (c % 16).toString(16)
  1379. )
  1380. })
  1381. }
  1382. return '"' + source + '"'
  1383. }
  1384. function encodeArray(source) {
  1385. var result = ['['],
  1386. l = source.length,
  1387. preComma,
  1388. i,
  1389. item
  1390. for (i = 0; i < l; i++) {
  1391. item = source[i]
  1392. switch (typeof item) {
  1393. case 'undefined':
  1394. case 'function':
  1395. case 'unknown':
  1396. break
  1397. default:
  1398. if (preComma) {
  1399. result.push(',')
  1400. }
  1401. result.push(utils.json2str(item))
  1402. preComma = 1
  1403. }
  1404. }
  1405. result.push(']')
  1406. return result.join('')
  1407. }
  1408. function pad(source) {
  1409. return source < 10 ? '0' + source : source
  1410. }
  1411. function encodeDate(source) {
  1412. return (
  1413. '"' +
  1414. source.getFullYear() +
  1415. '-' +
  1416. pad(source.getMonth() + 1) +
  1417. '-' +
  1418. pad(source.getDate()) +
  1419. 'T' +
  1420. pad(source.getHours()) +
  1421. ':' +
  1422. pad(source.getMinutes()) +
  1423. ':' +
  1424. pad(source.getSeconds()) +
  1425. '"'
  1426. )
  1427. }
  1428. return function(value) {
  1429. switch (typeof value) {
  1430. case 'undefined':
  1431. return 'undefined'
  1432. case 'number':
  1433. return isFinite(value) ? String(value) : 'null'
  1434. case 'string':
  1435. return encodeString(value)
  1436. case 'boolean':
  1437. return String(value)
  1438. default:
  1439. if (value === null) {
  1440. return 'null'
  1441. } else if (utils.isArray(value)) {
  1442. return encodeArray(value)
  1443. } else if (utils.isDate(value)) {
  1444. return encodeDate(value)
  1445. } else {
  1446. var result = ['{'],
  1447. encode = utils.json2str,
  1448. preComma,
  1449. item
  1450. for (var key in value) {
  1451. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1452. item = value[key]
  1453. switch (typeof item) {
  1454. case 'undefined':
  1455. case 'unknown':
  1456. case 'function':
  1457. break
  1458. default:
  1459. if (preComma) {
  1460. result.push(',')
  1461. }
  1462. preComma = 1
  1463. result.push(encode(key) + ':' + encode(item))
  1464. }
  1465. }
  1466. }
  1467. result.push('}')
  1468. return result.join('')
  1469. }
  1470. }
  1471. }
  1472. }
  1473. })()
  1474. })
  1475. /**
  1476. * 判断给定的对象是否是字符串
  1477. * @method isString
  1478. * @param { * } object 需要判断的对象
  1479. * @return { Boolean } 给定的对象是否是字符串
  1480. */
  1481. /**
  1482. * 判断给定的对象是否是数组
  1483. * @method isArray
  1484. * @param { * } object 需要判断的对象
  1485. * @return { Boolean } 给定的对象是否是数组
  1486. */
  1487. /**
  1488. * 判断给定的对象是否是一个Function
  1489. * @method isFunction
  1490. * @param { * } object 需要判断的对象
  1491. * @return { Boolean } 给定的对象是否是Function
  1492. */
  1493. /**
  1494. * 判断给定的对象是否是Number
  1495. * @method isNumber
  1496. * @param { * } object 需要判断的对象
  1497. * @return { Boolean } 给定的对象是否是Number
  1498. */
  1499. /**
  1500. * 判断给定的对象是否是一个正则表达式
  1501. * @method isRegExp
  1502. * @param { * } object 需要判断的对象
  1503. * @return { Boolean } 给定的对象是否是正则表达式
  1504. */
  1505. /**
  1506. * 判断给定的对象是否是一个普通对象
  1507. * @method isObject
  1508. * @param { * } object 需要判断的对象
  1509. * @return { Boolean } 给定的对象是否是普通对象
  1510. */
  1511. utils.each(
  1512. ['String', 'Function', 'Array', 'Number', 'RegExp', 'Object', 'Date'],
  1513. function(v) {
  1514. UE.utils['is' + v] = function(obj) {
  1515. return Object.prototype.toString.apply(obj) == '[object ' + v + ']'
  1516. }
  1517. }
  1518. )
  1519. // core/EventBase.js
  1520. /**
  1521. * UE采用的事件基类
  1522. * @file
  1523. * @module UE
  1524. * @class EventBase
  1525. * @since 1.2.6.1
  1526. */
  1527. /**
  1528. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1529. * @unfile
  1530. * @module UE
  1531. */
  1532. /**
  1533. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1534. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1535. * @unfile
  1536. * @module UE
  1537. * @class EventBase
  1538. */
  1539. /**
  1540. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1541. * @constructor
  1542. * @example
  1543. * ```javascript
  1544. * UE.EventBase.call(editor);
  1545. * ```
  1546. */
  1547. var EventBase = (UE.EventBase = function() {})
  1548. EventBase.prototype = {
  1549. /**
  1550. * 注册事件监听器
  1551. * @method addListener
  1552. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1553. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1554. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1555. * @example
  1556. * ```javascript
  1557. * editor.addListener('selectionchange',function(){
  1558. * console.log("选区已经变化!");
  1559. * })
  1560. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1561. * if(type == 'beforegetcontent'){
  1562. * //do something
  1563. * }else{
  1564. * //do something
  1565. * }
  1566. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1567. * })
  1568. * ```
  1569. * @see UE.EventBase:fireEvent(String)
  1570. */
  1571. addListener: function(types, listener) {
  1572. types = utils.trim(types).split(/\s+/)
  1573. for (var i = 0, ti; (ti = types[i++]); ) {
  1574. getListener(this, ti, true).push(listener)
  1575. }
  1576. },
  1577. on: function(types, listener) {
  1578. return this.addListener(types, listener)
  1579. },
  1580. off: function(types, listener) {
  1581. return this.removeListener(types, listener)
  1582. },
  1583. trigger: function() {
  1584. return this.fireEvent.apply(this, arguments)
  1585. },
  1586. /**
  1587. * 移除事件监听器
  1588. * @method removeListener
  1589. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1590. * @param { Function } fn 移除监听事件的函数引用
  1591. * @example
  1592. * ```javascript
  1593. * //changeCallback为方法体
  1594. * editor.removeListener("selectionchange",changeCallback);
  1595. * ```
  1596. */
  1597. removeListener: function(types, listener) {
  1598. types = utils.trim(types).split(/\s+/)
  1599. for (var i = 0, ti; (ti = types[i++]); ) {
  1600. utils.removeItem(getListener(this, ti) || [], listener)
  1601. }
  1602. },
  1603. /**
  1604. * 触发事件
  1605. * @method fireEvent
  1606. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1607. * @remind 该方法会触发addListener
  1608. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1609. * @example
  1610. * ```javascript
  1611. * editor.fireEvent("selectionchange");
  1612. * ```
  1613. */
  1614. /**
  1615. * 触发事件
  1616. * @method fireEvent
  1617. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1618. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1619. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1620. * @example
  1621. * ```javascript
  1622. *
  1623. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1624. *
  1625. * console.log( arg1 + " " + arg2 );
  1626. *
  1627. * } );
  1628. *
  1629. * //触发selectionchange事件, 会执行上面的事件监听器
  1630. * //output: Hello World
  1631. * editor.fireEvent("selectionchange", "Hello", "World");
  1632. * ```
  1633. */
  1634. fireEvent: function() {
  1635. var types = arguments[0]
  1636. types = utils.trim(types).split(' ')
  1637. for (var i = 0, ti; (ti = types[i++]); ) {
  1638. var listeners = getListener(this, ti),
  1639. r,
  1640. t,
  1641. k
  1642. if (listeners) {
  1643. k = listeners.length
  1644. while (k--) {
  1645. if (!listeners[k]) continue
  1646. t = listeners[k].apply(this, arguments)
  1647. if (t === true) {
  1648. return t
  1649. }
  1650. if (t !== undefined) {
  1651. r = t
  1652. }
  1653. }
  1654. }
  1655. if ((t = this['on' + ti.toLowerCase()])) {
  1656. r = t.apply(this, arguments)
  1657. }
  1658. }
  1659. return r
  1660. }
  1661. }
  1662. /**
  1663. * 获得对象所拥有监听类型的所有监听器
  1664. * @unfile
  1665. * @module UE
  1666. * @since 1.2.6.1
  1667. * @method getListener
  1668. * @public
  1669. * @param { Object } obj 查询监听器的对象
  1670. * @param { String } type 事件类型
  1671. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1672. * @return { Array } 监听器数组
  1673. */
  1674. function getListener(obj, type, force) {
  1675. var allListeners
  1676. type = type.toLowerCase()
  1677. return (
  1678. (allListeners =
  1679. obj.__allListeners || (force && (obj.__allListeners = {}))) &&
  1680. (allListeners[type] || (force && (allListeners[type] = [])))
  1681. )
  1682. }
  1683. // core/dtd.js
  1684. ///import editor.js
  1685. ///import core/dom/dom.js
  1686. ///import core/utils.js
  1687. /**
  1688. * dtd html语义化的体现类
  1689. * @constructor
  1690. * @namespace dtd
  1691. */
  1692. var dtd = (dom.dtd = (function() {
  1693. function _(s) {
  1694. for (var k in s) {
  1695. s[k.toUpperCase()] = s[k]
  1696. }
  1697. return s
  1698. }
  1699. var X = utils.extend2
  1700. var A = _({isindex: 1, fieldset: 1}),
  1701. B = _({input: 1, button: 1, select: 1, textarea: 1, label: 1}),
  1702. C = X(_({a: 1}), B),
  1703. D = X({iframe: 1}, C),
  1704. E = _({
  1705. hr: 1,
  1706. ul: 1,
  1707. menu: 1,
  1708. div: 1,
  1709. blockquote: 1,
  1710. noscript: 1,
  1711. table: 1,
  1712. center: 1,
  1713. address: 1,
  1714. dir: 1,
  1715. pre: 1,
  1716. h5: 1,
  1717. dl: 1,
  1718. h4: 1,
  1719. noframes: 1,
  1720. h6: 1,
  1721. ol: 1,
  1722. h1: 1,
  1723. h3: 1,
  1724. h2: 1
  1725. }),
  1726. F = _({ins: 1, del: 1, script: 1, style: 1}),
  1727. G = X(
  1728. _({
  1729. b: 1,
  1730. acronym: 1,
  1731. bdo: 1,
  1732. var: 1,
  1733. '#': 1,
  1734. abbr: 1,
  1735. code: 1,
  1736. br: 1,
  1737. i: 1,
  1738. cite: 1,
  1739. kbd: 1,
  1740. u: 1,
  1741. strike: 1,
  1742. s: 1,
  1743. tt: 1,
  1744. strong: 1,
  1745. q: 1,
  1746. samp: 1,
  1747. em: 1,
  1748. dfn: 1,
  1749. span: 1
  1750. }),
  1751. F
  1752. ),
  1753. H = X(
  1754. _({
  1755. sub: 1,
  1756. img: 1,
  1757. embed: 1,
  1758. object: 1,
  1759. sup: 1,
  1760. basefont: 1,
  1761. map: 1,
  1762. applet: 1,
  1763. font: 1,
  1764. big: 1,
  1765. small: 1
  1766. }),
  1767. G
  1768. ),
  1769. I = X(_({p: 1}), H),
  1770. J = X(_({iframe: 1}), H, B),
  1771. K = _({
  1772. img: 1,
  1773. embed: 1,
  1774. noscript: 1,
  1775. br: 1,
  1776. kbd: 1,
  1777. center: 1,
  1778. button: 1,
  1779. basefont: 1,
  1780. h5: 1,
  1781. h4: 1,
  1782. samp: 1,
  1783. h6: 1,
  1784. ol: 1,
  1785. h1: 1,
  1786. h3: 1,
  1787. h2: 1,
  1788. form: 1,
  1789. font: 1,
  1790. '#': 1,
  1791. select: 1,
  1792. menu: 1,
  1793. ins: 1,
  1794. abbr: 1,
  1795. label: 1,
  1796. code: 1,
  1797. table: 1,
  1798. script: 1,
  1799. cite: 1,
  1800. input: 1,
  1801. iframe: 1,
  1802. strong: 1,
  1803. textarea: 1,
  1804. noframes: 1,
  1805. big: 1,
  1806. small: 1,
  1807. span: 1,
  1808. hr: 1,
  1809. sub: 1,
  1810. bdo: 1,
  1811. var: 1,
  1812. div: 1,
  1813. object: 1,
  1814. sup: 1,
  1815. strike: 1,
  1816. dir: 1,
  1817. map: 1,
  1818. dl: 1,
  1819. applet: 1,
  1820. del: 1,
  1821. isindex: 1,
  1822. fieldset: 1,
  1823. ul: 1,
  1824. b: 1,
  1825. acronym: 1,
  1826. a: 1,
  1827. blockquote: 1,
  1828. i: 1,
  1829. u: 1,
  1830. s: 1,
  1831. tt: 1,
  1832. address: 1,
  1833. q: 1,
  1834. pre: 1,
  1835. p: 1,
  1836. em: 1,
  1837. dfn: 1
  1838. }),
  1839. L = X(_({a: 0}), J), //a不能被切开,所以把他
  1840. M = _({tr: 1}),
  1841. N = _({'#': 1}),
  1842. O = X(_({param: 1}), K),
  1843. P = X(_({form: 1}), A, D, E, I),
  1844. Q = _({li: 1, ol: 1, ul: 1}),
  1845. R = _({style: 1, script: 1}),
  1846. S = _({base: 1, link: 1, meta: 1, title: 1}),
  1847. T = X(S, R),
  1848. U = _({head: 1, body: 1}),
  1849. V = _({html: 1})
  1850. var block = _({
  1851. address: 1,
  1852. blockquote: 1,
  1853. center: 1,
  1854. dir: 1,
  1855. div: 1,
  1856. dl: 1,
  1857. fieldset: 1,
  1858. form: 1,
  1859. h1: 1,
  1860. h2: 1,
  1861. h3: 1,
  1862. h4: 1,
  1863. h5: 1,
  1864. h6: 1,
  1865. hr: 1,
  1866. isindex: 1,
  1867. menu: 1,
  1868. noframes: 1,
  1869. ol: 1,
  1870. p: 1,
  1871. pre: 1,
  1872. table: 1,
  1873. ul: 1
  1874. }),
  1875. empty = _({
  1876. area: 1,
  1877. base: 1,
  1878. basefont: 1,
  1879. br: 1,
  1880. col: 1,
  1881. command: 1,
  1882. dialog: 1,
  1883. embed: 1,
  1884. hr: 1,
  1885. img: 1,
  1886. input: 1,
  1887. isindex: 1,
  1888. keygen: 1,
  1889. link: 1,
  1890. meta: 1,
  1891. param: 1,
  1892. source: 1,
  1893. track: 1,
  1894. wbr: 1
  1895. })
  1896. return _({
  1897. // $ 表示自定的属性
  1898. // body外的元素列表.
  1899. $nonBodyContent: X(V, U, S),
  1900. //块结构元素列表
  1901. $block: block,
  1902. //内联元素列表
  1903. $inline: L,
  1904. $inlineWithA: X(_({a: 1}), L),
  1905. $body: X(_({script: 1, style: 1}), block),
  1906. $cdata: _({script: 1, style: 1}),
  1907. //自闭和元素
  1908. $empty: empty,
  1909. //不是自闭合,但不能让range选中里边
  1910. $nonChild: _({iframe: 1, textarea: 1}),
  1911. //列表元素列表
  1912. $listItem: _({dd: 1, dt: 1, li: 1}),
  1913. //列表根元素列表
  1914. $list: _({ul: 1, ol: 1, dl: 1}),
  1915. //不能认为是空的元素
  1916. $isNotEmpty: _({
  1917. table: 1,
  1918. ul: 1,
  1919. ol: 1,
  1920. dl: 1,
  1921. iframe: 1,
  1922. area: 1,
  1923. base: 1,
  1924. col: 1,
  1925. hr: 1,
  1926. img: 1,
  1927. embed: 1,
  1928. input: 1,
  1929. link: 1,
  1930. meta: 1,
  1931. param: 1,
  1932. h1: 1,
  1933. h2: 1,
  1934. h3: 1,
  1935. h4: 1,
  1936. h5: 1,
  1937. h6: 1
  1938. }),
  1939. //如果没有子节点就可以删除的元素列表,像span,a
  1940. $removeEmpty: _({
  1941. a: 1,
  1942. abbr: 1,
  1943. acronym: 1,
  1944. address: 1,
  1945. b: 1,
  1946. bdo: 1,
  1947. big: 1,
  1948. cite: 1,
  1949. code: 1,
  1950. del: 1,
  1951. dfn: 1,
  1952. em: 1,
  1953. font: 1,
  1954. i: 1,
  1955. ins: 1,
  1956. label: 1,
  1957. kbd: 1,
  1958. q: 1,
  1959. s: 1,
  1960. samp: 1,
  1961. small: 1,
  1962. span: 1,
  1963. strike: 1,
  1964. strong: 1,
  1965. sub: 1,
  1966. sup: 1,
  1967. tt: 1,
  1968. u: 1,
  1969. var: 1
  1970. }),
  1971. $removeEmptyBlock: _({p: 1, div: 1}),
  1972. //在table元素里的元素列表
  1973. $tableContent: _({
  1974. caption: 1,
  1975. col: 1,
  1976. colgroup: 1,
  1977. tbody: 1,
  1978. td: 1,
  1979. tfoot: 1,
  1980. th: 1,
  1981. thead: 1,
  1982. tr: 1,
  1983. table: 1
  1984. }),
  1985. //不转换的标签
  1986. $notTransContent: _({pre: 1, script: 1, style: 1, textarea: 1}),
  1987. html: U,
  1988. head: T,
  1989. style: N,
  1990. script: N,
  1991. body: P,
  1992. base: {},
  1993. link: {},
  1994. meta: {},
  1995. title: N,
  1996. col: {},
  1997. tr: _({td: 1, th: 1}),
  1998. img: {},
  1999. embed: {},
  2000. colgroup: _({thead: 1, col: 1, tbody: 1, tr: 1, tfoot: 1}),
  2001. noscript: P,
  2002. td: P,
  2003. br: {},
  2004. th: P,
  2005. center: P,
  2006. kbd: L,
  2007. button: X(I, E),
  2008. basefont: {},
  2009. h5: L,
  2010. h4: L,
  2011. samp: L,
  2012. h6: L,
  2013. ol: Q,
  2014. h1: L,
  2015. h3: L,
  2016. option: N,
  2017. h2: L,
  2018. form: X(A, D, E, I),
  2019. select: _({optgroup: 1, option: 1}),
  2020. font: L,
  2021. ins: L,
  2022. menu: Q,
  2023. abbr: L,
  2024. label: L,
  2025. table: _({
  2026. thead: 1,
  2027. col: 1,
  2028. tbody: 1,
  2029. tr: 1,
  2030. colgroup: 1,
  2031. caption: 1,
  2032. tfoot: 1
  2033. }),
  2034. code: L,
  2035. tfoot: M,
  2036. cite: L,
  2037. li: P,
  2038. input: {},
  2039. iframe: P,
  2040. strong: L,
  2041. textarea: N,
  2042. noframes: P,
  2043. big: L,
  2044. small: L,
  2045. //trace:
  2046. span: _({
  2047. '#': 1,
  2048. br: 1,
  2049. b: 1,
  2050. strong: 1,
  2051. u: 1,
  2052. i: 1,
  2053. em: 1,
  2054. sub: 1,
  2055. sup: 1,
  2056. strike: 1,
  2057. span: 1
  2058. }),
  2059. hr: L,
  2060. dt: L,
  2061. sub: L,
  2062. optgroup: _({option: 1}),
  2063. param: {},
  2064. bdo: L,
  2065. var: L,
  2066. div: P,
  2067. object: O,
  2068. sup: L,
  2069. dd: P,
  2070. strike: L,
  2071. area: {},
  2072. dir: Q,
  2073. map: X(_({area: 1, form: 1, p: 1}), A, F, E),
  2074. applet: O,
  2075. dl: _({dt: 1, dd: 1}),
  2076. del: L,
  2077. isindex: {},
  2078. fieldset: X(_({legend: 1}), K),
  2079. thead: M,
  2080. ul: Q,
  2081. acronym: L,
  2082. b: L,
  2083. a: X(_({a: 1}), J),
  2084. blockquote: X(_({td: 1, tr: 1, tbody: 1, li: 1}), P),
  2085. caption: L,
  2086. i: L,
  2087. u: L,
  2088. tbody: M,
  2089. s: L,
  2090. address: X(D, I),
  2091. tt: L,
  2092. legend: L,
  2093. q: L,
  2094. pre: X(G, C),
  2095. p: X(_({a: 1}), L),
  2096. em: L,
  2097. dfn: L
  2098. })
  2099. })())
  2100. // core/domUtils.js
  2101. /**
  2102. * Dom操作工具包
  2103. * @file
  2104. * @module UE.dom.domUtils
  2105. * @since 1.2.6.1
  2106. */
  2107. /**
  2108. * Dom操作工具包
  2109. * @unfile
  2110. * @module UE.dom.domUtils
  2111. */
  2112. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2113. var tmpNode = startFromChild && node[start],
  2114. parent
  2115. !tmpNode && (tmpNode = node[ltr])
  2116. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2117. if (parent.tagName == 'BODY' || (guard && !guard(parent))) {
  2118. return null
  2119. }
  2120. tmpNode = parent[ltr]
  2121. }
  2122. if (tmpNode && fn && !fn(tmpNode)) {
  2123. return getDomNode(tmpNode, start, ltr, false, fn)
  2124. }
  2125. return tmpNode
  2126. }
  2127. var attrFix =
  2128. ie && browser.version < 9
  2129. ? {
  2130. tabindex: 'tabIndex',
  2131. readonly: 'readOnly',
  2132. for: 'htmlFor',
  2133. class: 'className',
  2134. maxlength: 'maxLength',
  2135. cellspacing: 'cellSpacing',
  2136. cellpadding: 'cellPadding',
  2137. rowspan: 'rowSpan',
  2138. colspan: 'colSpan',
  2139. usemap: 'useMap',
  2140. frameborder: 'frameBorder'
  2141. }
  2142. : {
  2143. tabindex: 'tabIndex',
  2144. readonly: 'readOnly'
  2145. },
  2146. styleBlock = utils.listToMap([
  2147. '-webkit-box',
  2148. '-moz-box',
  2149. 'block',
  2150. 'list-item',
  2151. 'table',
  2152. 'table-row-group',
  2153. 'table-header-group',
  2154. 'table-footer-group',
  2155. 'table-row',
  2156. 'table-column-group',
  2157. 'table-column',
  2158. 'table-cell',
  2159. 'table-caption'
  2160. ])
  2161. var domUtils = (dom.domUtils = {
  2162. //节点常量
  2163. NODE_ELEMENT: 1,
  2164. NODE_DOCUMENT: 9,
  2165. NODE_TEXT: 3,
  2166. NODE_COMMENT: 8,
  2167. NODE_DOCUMENT_FRAGMENT: 11,
  2168. //位置关系
  2169. POSITION_IDENTICAL: 0,
  2170. POSITION_DISCONNECTED: 1,
  2171. POSITION_FOLLOWING: 2,
  2172. POSITION_PRECEDING: 4,
  2173. POSITION_IS_CONTAINED: 8,
  2174. POSITION_CONTAINS: 16,
  2175. //ie6使用其他的会有一段空白出现
  2176. fillChar: ie && browser.version == '6' ? '\ufeff' : '\u200B',
  2177. //-------------------------Node部分--------------------------------
  2178. keys: {
  2179. /*Backspace*/ 8: 1,
  2180. /*Delete*/ 46: 1,
  2181. /*Shift*/ 16: 1,
  2182. /*Ctrl*/ 17: 1,
  2183. /*Alt*/ 18: 1,
  2184. 37: 1,
  2185. 38: 1,
  2186. 39: 1,
  2187. 40: 1,
  2188. 13: 1 /*enter*/
  2189. },
  2190. /**
  2191. * 获取节点A相对于节点B的位置关系
  2192. * @method getPosition
  2193. * @param { Node } nodeA 需要查询位置关系的节点A
  2194. * @param { Node } nodeB 需要查询位置关系的节点B
  2195. * @return { Number } 节点A与节点B的关系
  2196. * @example
  2197. * ```javascript
  2198. * //output: 20
  2199. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2200. *
  2201. * switch ( position ) {
  2202. *
  2203. * //0
  2204. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2205. * console.log('元素相同');
  2206. * break;
  2207. * //1
  2208. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2209. * console.log('两个节点在不同的文档中');
  2210. * break;
  2211. * //2
  2212. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2213. * console.log('节点A在节点B之后');
  2214. * break;
  2215. * //4
  2216. * case UE.dom.domUtils.POSITION_PRECEDING;
  2217. * console.log('节点A在节点B之前');
  2218. * break;
  2219. * //8
  2220. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2221. * console.log('节点A被节点B包含');
  2222. * break;
  2223. * case 10:
  2224. * console.log('节点A被节点B包含且节点A在节点B之后');
  2225. * break;
  2226. * //16
  2227. * case UE.dom.domUtils.POSITION_CONTAINS:
  2228. * console.log('节点A包含节点B');
  2229. * break;
  2230. * case 20:
  2231. * console.log('节点A包含节点B且节点A在节点B之前');
  2232. * break;
  2233. *
  2234. * }
  2235. * ```
  2236. */
  2237. getPosition: function(nodeA, nodeB) {
  2238. // 如果两个节点是同一个节点
  2239. if (nodeA === nodeB) {
  2240. // domUtils.POSITION_IDENTICAL
  2241. return 0
  2242. }
  2243. var node,
  2244. parentsA = [nodeA],
  2245. parentsB = [nodeB]
  2246. node = nodeA
  2247. while ((node = node.parentNode)) {
  2248. // 如果nodeB是nodeA的祖先节点
  2249. if (node === nodeB) {
  2250. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2251. return 10
  2252. }
  2253. parentsA.push(node)
  2254. }
  2255. node = nodeB
  2256. while ((node = node.parentNode)) {
  2257. // 如果nodeA是nodeB的祖先节点
  2258. if (node === nodeA) {
  2259. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2260. return 20
  2261. }
  2262. parentsB.push(node)
  2263. }
  2264. parentsA.reverse()
  2265. parentsB.reverse()
  2266. if (parentsA[0] !== parentsB[0]) {
  2267. // domUtils.POSITION_DISCONNECTED
  2268. return 1
  2269. }
  2270. var i = -1
  2271. while ((i++, parentsA[i] === parentsB[i])) {}
  2272. nodeA = parentsA[i]
  2273. nodeB = parentsB[i]
  2274. while ((nodeA = nodeA.nextSibling)) {
  2275. if (nodeA === nodeB) {
  2276. // domUtils.POSITION_PRECEDING
  2277. return 4
  2278. }
  2279. }
  2280. // domUtils.POSITION_FOLLOWING
  2281. return 2
  2282. },
  2283. /**
  2284. * 检测节点node在父节点中的索引位置
  2285. * @method getNodeIndex
  2286. * @param { Node } node 需要检测的节点对象
  2287. * @return { Number } 该节点在父节点中的位置
  2288. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2289. */
  2290. /**
  2291. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2292. * @method getNodeIndex
  2293. * @param { Node } node 需要检测的节点对象
  2294. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2295. * @return { Number } 该节点在父节点中的位置
  2296. * @example
  2297. * ```javascript
  2298. *
  2299. * var node = document.createElement("div");
  2300. *
  2301. * node.appendChild( document.createTextNode( "hello" ) );
  2302. * node.appendChild( document.createTextNode( "world" ) );
  2303. * node.appendChild( node = document.createElement( "div" ) );
  2304. *
  2305. * //output: 2
  2306. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2307. *
  2308. * //output: 1
  2309. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2310. *
  2311. * ```
  2312. */
  2313. getNodeIndex: function(node, ignoreTextNode) {
  2314. var preNode = node,
  2315. i = 0
  2316. while ((preNode = preNode.previousSibling)) {
  2317. if (ignoreTextNode && preNode.nodeType == 3) {
  2318. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2319. i++
  2320. }
  2321. continue
  2322. }
  2323. i++
  2324. }
  2325. return i
  2326. },
  2327. /**
  2328. * 检测节点node是否在给定的document对象上
  2329. * @method inDoc
  2330. * @param { Node } node 需要检测的节点对象
  2331. * @param { DomDocument } doc 需要检测的document对象
  2332. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2333. * @example
  2334. * ```javascript
  2335. *
  2336. * var node = document.createElement("div");
  2337. *
  2338. * //output: false
  2339. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2340. *
  2341. * document.body.appendChild( node );
  2342. *
  2343. * //output: true
  2344. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2345. *
  2346. * ```
  2347. */
  2348. inDoc: function(node, doc) {
  2349. return domUtils.getPosition(node, doc) == 10
  2350. },
  2351. /**
  2352. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2353. * 查找的起点是给定node节点的父节点。
  2354. * @method findParent
  2355. * @param { Node } node 需要查找的节点
  2356. * @param { Function } filterFn 自定义的过滤方法。
  2357. * @warning 查找的终点是到body节点为止
  2358. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2359. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2360. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2361. * @example
  2362. * ```javascript
  2363. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2364. *
  2365. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2366. * return node.tagName === "HTML";
  2367. *
  2368. * } );
  2369. *
  2370. * //output: true
  2371. * console.log( filterNode === null );
  2372. * ```
  2373. */
  2374. /**
  2375. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2376. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2377. * @method findParent
  2378. * @param { Node } node 需要查找的节点
  2379. * @param { Function } filterFn 自定义的过滤方法。
  2380. * @param { Boolean } includeSelf 查找过程是否包含自身
  2381. * @warning 查找的终点是到body节点为止
  2382. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2383. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2384. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2385. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2386. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2387. * @example
  2388. * ```html
  2389. * <body>
  2390. *
  2391. * <div id="test">
  2392. * </div>
  2393. *
  2394. * <script type="text/javascript">
  2395. *
  2396. * //output: DIV, BODY
  2397. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2398. *
  2399. * console.log( node.tagName );
  2400. * return false;
  2401. *
  2402. * }, true );
  2403. *
  2404. * </script>
  2405. * </body>
  2406. * ```
  2407. */
  2408. findParent: function(node, filterFn, includeSelf) {
  2409. if (node && !domUtils.isBody(node)) {
  2410. node = includeSelf ? node : node.parentNode
  2411. while (node) {
  2412. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2413. return filterFn && !filterFn(node) && domUtils.isBody(node)
  2414. ? null
  2415. : node
  2416. }
  2417. node = node.parentNode
  2418. }
  2419. }
  2420. return null
  2421. },
  2422. /**
  2423. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2424. * @method findParentByTagName
  2425. * @param { Node } node 需要查找的节点对象
  2426. * @param { Array } tagNames 需要查找的父节点的名称数组
  2427. * @warning 查找的终点是到body节点为止
  2428. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2429. * @example
  2430. * ```javascript
  2431. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2432. * //output: BODY
  2433. * console.log( node.tagName );
  2434. * ```
  2435. */
  2436. /**
  2437. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2438. * 否则, 起点是node的父节点。
  2439. * @method findParentByTagName
  2440. * @param { Node } node 需要查找的节点对象
  2441. * @param { Array } tagNames 需要查找的父节点的名称数组
  2442. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2443. * @warning 查找的终点是到body节点为止
  2444. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2445. * @example
  2446. * ```javascript
  2447. * var queryTarget = document.getElementsByTagName("div")[0];
  2448. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2449. * //output: true
  2450. * console.log( queryTarget === node );
  2451. * ```
  2452. */
  2453. findParentByTagName: function(node, tagNames, includeSelf, excludeFn) {
  2454. tagNames = utils.listToMap(
  2455. utils.isArray(tagNames) ? tagNames : [tagNames]
  2456. )
  2457. return domUtils.findParent(
  2458. node,
  2459. function(node) {
  2460. return tagNames[node.tagName] && !(excludeFn && excludeFn(node))
  2461. },
  2462. includeSelf
  2463. )
  2464. },
  2465. /**
  2466. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2467. * @method findParents
  2468. * @param { Node } node 需要查找的节点对象
  2469. * @return { Array } 给定节点的祖先节点数组
  2470. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2471. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2472. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2473. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2474. */
  2475. /**
  2476. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2477. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2478. * @method findParents
  2479. * @param { Node } node 需要查找的节点对象
  2480. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2481. * @return { Array } 给定节点的祖先节点数组
  2482. */
  2483. findParents: function(node, includeSelf, filterFn, closerFirst) {
  2484. var parents =
  2485. includeSelf && ((filterFn && filterFn(node)) || !filterFn) ? [node] : []
  2486. while ((node = domUtils.findParent(node, filterFn))) {
  2487. parents.push(node)
  2488. }
  2489. return closerFirst ? parents : parents.reverse()
  2490. },
  2491. /**
  2492. * 在节点node后面插入新节点newNode
  2493. * @method insertAfter
  2494. * @param { Node } node 目标节点
  2495. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2496. * @return { Node } 新插入的节点
  2497. */
  2498. insertAfter: function(node, newNode) {
  2499. return node.nextSibling
  2500. ? node.parentNode.insertBefore(newNode, node.nextSibling)
  2501. : node.parentNode.appendChild(newNode)
  2502. },
  2503. /**
  2504. * 删除节点node及其下属的所有节点
  2505. * @method remove
  2506. * @param { Node } node 需要删除的节点对象
  2507. * @return { Node } 返回刚删除的节点对象
  2508. * @example
  2509. * ```html
  2510. * <div id="test">
  2511. * <div id="child">你好</div>
  2512. * </div>
  2513. * <script>
  2514. * UE.dom.domUtils.remove( document.body, false );
  2515. * //output: false
  2516. * console.log( document.getElementById( "child" ) !== null );
  2517. * </script>
  2518. * ```
  2519. */
  2520. /**
  2521. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2522. * @method remove
  2523. * @param { Node } node 需要删除的节点对象
  2524. * @param { Boolean } keepChildren 是否需要保留子节点
  2525. * @return { Node } 返回刚删除的节点对象
  2526. * @example
  2527. * ```html
  2528. * <div id="test">
  2529. * <div id="child">你好</div>
  2530. * </div>
  2531. * <script>
  2532. * UE.dom.domUtils.remove( document.body, true );
  2533. * //output: true
  2534. * console.log( document.getElementById( "child" ) !== null );
  2535. * </script>
  2536. * ```
  2537. */
  2538. remove: function(node, keepChildren) {
  2539. var parent = node.parentNode,
  2540. child
  2541. if (parent) {
  2542. if (keepChildren && node.hasChildNodes()) {
  2543. while ((child = node.firstChild)) {
  2544. parent.insertBefore(child, node)
  2545. }
  2546. }
  2547. parent.removeChild(node)
  2548. }
  2549. return node
  2550. },
  2551. /**
  2552. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2553. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2554. * @method getNextDomNode
  2555. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2556. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2557. * @example
  2558. * ```html
  2559. * <body>
  2560. * <div id="test">
  2561. * <span></span>
  2562. * </div>
  2563. * <i>xxx</i>
  2564. * </body>
  2565. * <script>
  2566. *
  2567. * //output: i节点
  2568. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2569. *
  2570. * </script>
  2571. * ```
  2572. * @example
  2573. * ```html
  2574. * <body>
  2575. * <div>
  2576. * <span></span>
  2577. * <i id="test">xxx</i>
  2578. * </div>
  2579. * <b>xxx</b>
  2580. * </body>
  2581. * <script>
  2582. *
  2583. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2584. * //output: b节点
  2585. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2586. *
  2587. * </script>
  2588. * ```
  2589. */
  2590. /**
  2591. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2592. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2593. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2594. * @method getNextDomNode
  2595. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2596. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2597. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2598. * @see UE.dom.domUtils.getNextDomNode(Node)
  2599. */
  2600. getNextDomNode: function(node, startFromChild, filterFn, guard) {
  2601. return getDomNode(
  2602. node,
  2603. 'firstChild',
  2604. 'nextSibling',
  2605. startFromChild,
  2606. filterFn,
  2607. guard
  2608. )
  2609. },
  2610. getPreDomNode: function(node, startFromChild, filterFn, guard) {
  2611. return getDomNode(
  2612. node,
  2613. 'lastChild',
  2614. 'previousSibling',
  2615. startFromChild,
  2616. filterFn,
  2617. guard
  2618. )
  2619. },
  2620. /**
  2621. * 检测节点node是否属是UEditor定义的bookmark节点
  2622. * @method isBookmarkNode
  2623. * @private
  2624. * @param { Node } node 需要检测的节点对象
  2625. * @return { Boolean } 是否是bookmark节点
  2626. * @example
  2627. * ```html
  2628. * <span id="_baidu_bookmark_1"></span>
  2629. * <script>
  2630. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2631. * //output: true
  2632. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2633. * </script>
  2634. * ```
  2635. */
  2636. isBookmarkNode: function(node) {
  2637. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id)
  2638. },
  2639. /**
  2640. * 获取节点node所属的window对象
  2641. * @method getWindow
  2642. * @param { Node } node 节点对象
  2643. * @return { Window } 当前节点所属的window对象
  2644. * @example
  2645. * ```javascript
  2646. * //output: true
  2647. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2648. * ```
  2649. */
  2650. getWindow: function(node) {
  2651. var doc = node.ownerDocument || node
  2652. return doc.defaultView || doc.parentWindow
  2653. },
  2654. /**
  2655. * 获取离nodeA与nodeB最近的公共的祖先节点
  2656. * @method getCommonAncestor
  2657. * @param { Node } nodeA 第一个节点
  2658. * @param { Node } nodeB 第二个节点
  2659. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2660. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2661. * @example
  2662. * ```javascript
  2663. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2664. * //output: true
  2665. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2666. * ```
  2667. */
  2668. getCommonAncestor: function(nodeA, nodeB) {
  2669. if (nodeA === nodeB) return nodeA
  2670. var parentsA = [nodeA],
  2671. parentsB = [nodeB],
  2672. parent = nodeA,
  2673. i = -1
  2674. while ((parent = parent.parentNode)) {
  2675. if (parent === nodeB) {
  2676. return parent
  2677. }
  2678. parentsA.push(parent)
  2679. }
  2680. parent = nodeB
  2681. while ((parent = parent.parentNode)) {
  2682. if (parent === nodeA) return parent
  2683. parentsB.push(parent)
  2684. }
  2685. parentsA.reverse()
  2686. parentsB.reverse()
  2687. while ((i++, parentsA[i] === parentsB[i])) {}
  2688. return i == 0 ? null : parentsA[i - 1]
  2689. },
  2690. /**
  2691. * 清除node节点左右连续为空的兄弟inline节点
  2692. * @method clearEmptySibling
  2693. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2694. * 则这些兄弟节点将被删除
  2695. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2696. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2697. * @example
  2698. * ```html
  2699. * <body>
  2700. * <div></div>
  2701. * <span id="test"></span>
  2702. * <i></i>
  2703. * <b></b>
  2704. * <em>xxx</em>
  2705. * <span></span>
  2706. * </body>
  2707. * <script>
  2708. *
  2709. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2710. *
  2711. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2712. * console.log( document.body.innerHTML );
  2713. *
  2714. * </script>
  2715. * ```
  2716. */
  2717. /**
  2718. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2719. * 则忽略对右边兄弟节点的操作。
  2720. * @method clearEmptySibling
  2721. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2722. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2723. * 则这些兄弟节点将被删除
  2724. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2725. */
  2726. /**
  2727. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2728. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2729. * @method clearEmptySibling
  2730. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2731. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2732. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2733. * 则这些兄弟节点将被删除
  2734. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2735. */
  2736. clearEmptySibling: function(node, ignoreNext, ignorePre) {
  2737. function clear(next, dir) {
  2738. var tmpNode
  2739. while (
  2740. next &&
  2741. !domUtils.isBookmarkNode(next) &&
  2742. (domUtils.isEmptyInlineElement(next) ||
  2743. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2744. !new RegExp('[^\t\n\r' + domUtils.fillChar + ']').test(
  2745. next.nodeValue
  2746. ))
  2747. ) {
  2748. tmpNode = next[dir]
  2749. domUtils.remove(next)
  2750. next = tmpNode
  2751. }
  2752. }
  2753. !ignoreNext && clear(node.nextSibling, 'nextSibling')
  2754. !ignorePre && clear(node.previousSibling, 'previousSibling')
  2755. },
  2756. /**
  2757. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2758. * @method split
  2759. * @param { Node } textNode 需要拆分的文本节点对象
  2760. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2761. * @return { Node } 拆分后形成的新节点
  2762. * @example
  2763. * ```html
  2764. * <div id="test">abcdef</div>
  2765. * <script>
  2766. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2767. * //output: def
  2768. * console.log( newNode.nodeValue );
  2769. * </script>
  2770. * ```
  2771. */
  2772. split: function(node, offset) {
  2773. var doc = node.ownerDocument
  2774. if (browser.ie && offset == node.nodeValue.length) {
  2775. var next = doc.createTextNode('')
  2776. return domUtils.insertAfter(node, next)
  2777. }
  2778. var retval = node.splitText(offset)
  2779. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2780. if (browser.ie8) {
  2781. var tmpNode = doc.createTextNode('')
  2782. domUtils.insertAfter(retval, tmpNode)
  2783. domUtils.remove(tmpNode)
  2784. }
  2785. return retval
  2786. },
  2787. /**
  2788. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2789. * @method isWhitespace
  2790. * @param { Node } node 需要检测的节点对象
  2791. * @return { Boolean } 检测的节点是否为空
  2792. * @example
  2793. * ```html
  2794. * <div id="test">
  2795. *
  2796. * </div>
  2797. * <script>
  2798. * //output: true
  2799. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2800. * </script>
  2801. * ```
  2802. */
  2803. isWhitespace: function(node) {
  2804. return !new RegExp('[^ \t\n\r' + domUtils.fillChar + ']').test(
  2805. node.nodeValue
  2806. )
  2807. },
  2808. /**
  2809. * 获取元素element相对于viewport的位置坐标
  2810. * @method getXY
  2811. * @param { Node } element 需要计算位置的节点对象
  2812. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2813. * y代表垂直偏移距离。
  2814. *
  2815. * @example
  2816. * ```javascript
  2817. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2818. * //output: test的坐标为: 12, 24
  2819. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2820. * ```
  2821. */
  2822. getXY: function(element) {
  2823. var x = 0,
  2824. y = 0
  2825. while (element.offsetParent) {
  2826. y += element.offsetTop
  2827. x += element.offsetLeft
  2828. element = element.offsetParent
  2829. }
  2830. return {x: x, y: y}
  2831. },
  2832. /**
  2833. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2834. * @method on
  2835. * @param { Node } element 需要绑定事件的节点对象
  2836. * @param { String } type 绑定的事件类型
  2837. * @param { Function } handler 事件处理器
  2838. * @example
  2839. * ```javascript
  2840. * UE.dom.domUtils.on(document.body,"click",function(e){
  2841. * //e为事件对象,this为被点击元素对戏那个
  2842. * });
  2843. * ```
  2844. */
  2845. /**
  2846. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2847. * @method on
  2848. * @param { Node } element 需要绑定事件的节点对象
  2849. * @param { Array } type 绑定的事件类型数组
  2850. * @param { Function } handler 事件处理器
  2851. * @example
  2852. * ```javascript
  2853. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2854. * //evt为事件对象,this为被点击元素对象
  2855. * });
  2856. * ```
  2857. */
  2858. on: function(element, type, handler) {
  2859. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2860. k = types.length
  2861. if (k)
  2862. while (k--) {
  2863. type = types[k]
  2864. if (element.addEventListener) {
  2865. element.addEventListener(type, handler, false)
  2866. } else {
  2867. if (!handler._d) {
  2868. handler._d = {
  2869. els: []
  2870. }
  2871. }
  2872. var key = type + handler.toString(),
  2873. index = utils.indexOf(handler._d.els, element)
  2874. if (!handler._d[key] || index == -1) {
  2875. if (index == -1) {
  2876. handler._d.els.push(element)
  2877. }
  2878. if (!handler._d[key]) {
  2879. handler._d[key] = function(evt) {
  2880. return handler.call(evt.srcElement, evt || window.event)
  2881. }
  2882. }
  2883. element.attachEvent('on' + type, handler._d[key])
  2884. }
  2885. }
  2886. }
  2887. element = null
  2888. },
  2889. /**
  2890. * 解除DOM事件绑定
  2891. * @method un
  2892. * @param { Node } element 需要解除事件绑定的节点对象
  2893. * @param { String } type 需要接触绑定的事件类型
  2894. * @param { Function } handler 对应的事件处理器
  2895. * @example
  2896. * ```javascript
  2897. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2898. * //evt为事件对象,this为被点击元素对象
  2899. * });
  2900. * ```
  2901. */
  2902. /**
  2903. * 解除DOM事件绑定
  2904. * @method un
  2905. * @param { Node } element 需要解除事件绑定的节点对象
  2906. * @param { Array } type 需要接触绑定的事件类型数组
  2907. * @param { Function } handler 对应的事件处理器
  2908. * @example
  2909. * ```javascript
  2910. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2911. * //evt为事件对象,this为被点击元素对象
  2912. * });
  2913. * ```
  2914. */
  2915. un: function(element, type, handler) {
  2916. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2917. k = types.length
  2918. if (k)
  2919. while (k--) {
  2920. type = types[k]
  2921. if (element.removeEventListener) {
  2922. element.removeEventListener(type, handler, false)
  2923. } else {
  2924. var key = type + handler.toString()
  2925. try {
  2926. element.detachEvent(
  2927. 'on' + type,
  2928. handler._d ? handler._d[key] : handler
  2929. )
  2930. } catch (e) {}
  2931. if (handler._d && handler._d[key]) {
  2932. var index = utils.indexOf(handler._d.els, element)
  2933. if (index != -1) {
  2934. handler._d.els.splice(index, 1)
  2935. }
  2936. handler._d.els.length == 0 && delete handler._d[key]
  2937. }
  2938. }
  2939. }
  2940. },
  2941. /**
  2942. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2943. * @method isSameElement
  2944. * @param { Node } nodeA 需要比较的节点
  2945. * @param { Node } nodeB 需要比较的节点
  2946. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2947. * @example
  2948. * ```html
  2949. * <span style="font-size:12px">ssss</span>
  2950. * <span style="font-size:12px">bbbbb</span>
  2951. * <span style="font-size:13px">ssss</span>
  2952. * <span style="font-size:14px">bbbbb</span>
  2953. *
  2954. * <script>
  2955. *
  2956. * var nodes = document.getElementsByTagName( "span" );
  2957. *
  2958. * //output: true
  2959. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2960. *
  2961. * //output: false
  2962. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2963. *
  2964. * </script>
  2965. * ```
  2966. */
  2967. isSameElement: function(nodeA, nodeB) {
  2968. if (nodeA.tagName != nodeB.tagName) {
  2969. return false
  2970. }
  2971. var thisAttrs = nodeA.attributes,
  2972. otherAttrs = nodeB.attributes
  2973. if (!ie && thisAttrs.length != otherAttrs.length) {
  2974. return false
  2975. }
  2976. var attrA,
  2977. attrB,
  2978. al = 0,
  2979. bl = 0
  2980. for (var i = 0; (attrA = thisAttrs[i++]); ) {
  2981. if (attrA.nodeName == 'style') {
  2982. if (attrA.specified) {
  2983. al++
  2984. }
  2985. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2986. continue
  2987. } else {
  2988. return false
  2989. }
  2990. }
  2991. if (ie) {
  2992. if (attrA.specified) {
  2993. al++
  2994. attrB = otherAttrs.getNamedItem(attrA.nodeName)
  2995. } else {
  2996. continue
  2997. }
  2998. } else {
  2999. attrB = nodeB.attributes[attrA.nodeName]
  3000. }
  3001. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  3002. return false
  3003. }
  3004. }
  3005. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  3006. if (ie) {
  3007. for (i = 0; (attrB = otherAttrs[i++]); ) {
  3008. if (attrB.specified) {
  3009. bl++
  3010. }
  3011. }
  3012. if (al != bl) {
  3013. return false
  3014. }
  3015. }
  3016. return true
  3017. },
  3018. /**
  3019. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  3020. * @method isSameStyle
  3021. * @param { Node } nodeA 需要比较的节点
  3022. * @param { Node } nodeB 需要比较的节点
  3023. * @return { Boolean } 两个节点是否具有相同的style属性值
  3024. * @example
  3025. * ```html
  3026. * <span style="font-size:12px">ssss</span>
  3027. * <span style="font-size:12px">bbbbb</span>
  3028. * <span style="font-size:13px">ssss</span>
  3029. * <span style="font-size:14px">bbbbb</span>
  3030. *
  3031. * <script>
  3032. *
  3033. * var nodes = document.getElementsByTagName( "span" );
  3034. *
  3035. * //output: true
  3036. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  3037. *
  3038. * //output: false
  3039. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  3040. *
  3041. * </script>
  3042. * ```
  3043. */
  3044. isSameStyle: function(nodeA, nodeB) {
  3045. var styleA = nodeA.style.cssText
  3046. .replace(/( ?; ?)/g, ';')
  3047. .replace(/( ?: ?)/g, ':'),
  3048. styleB = nodeB.style.cssText
  3049. .replace(/( ?; ?)/g, ';')
  3050. .replace(/( ?: ?)/g, ':')
  3051. if (browser.opera) {
  3052. styleA = nodeA.style
  3053. styleB = nodeB.style
  3054. if (styleA.length != styleB.length) return false
  3055. for (var p in styleA) {
  3056. if (/^(\d+|csstext)$/i.test(p)) {
  3057. continue
  3058. }
  3059. if (styleA[p] != styleB[p]) {
  3060. return false
  3061. }
  3062. }
  3063. return true
  3064. }
  3065. if (!styleA || !styleB) {
  3066. return styleA == styleB
  3067. }
  3068. styleA = styleA.split(';')
  3069. styleB = styleB.split(';')
  3070. if (styleA.length != styleB.length) {
  3071. return false
  3072. }
  3073. for (var i = 0, ci; (ci = styleA[i++]); ) {
  3074. if (utils.indexOf(styleB, ci) == -1) {
  3075. return false
  3076. }
  3077. }
  3078. return true
  3079. },
  3080. /**
  3081. * 检查节点node是否为block元素
  3082. * @method isBlockElm
  3083. * @param { Node } node 需要检测的节点对象
  3084. * @return { Boolean } 是否是block元素节点
  3085. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3086. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3087. * @example
  3088. * ```html
  3089. * <span id="test1" style="display: block"></span>
  3090. * <span id="test2"></span>
  3091. * <div id="test3" style="display: inline"></div>
  3092. *
  3093. * <script>
  3094. *
  3095. * //output: true
  3096. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3097. *
  3098. * //output: false
  3099. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3100. *
  3101. * //output: true
  3102. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3103. *
  3104. * </script>
  3105. * ```
  3106. */
  3107. isBlockElm: function(node) {
  3108. return (
  3109. node.nodeType == 1 &&
  3110. (dtd.$block[node.tagName] ||
  3111. styleBlock[domUtils.getComputedStyle(node, 'display')]) &&
  3112. !dtd.$nonChild[node.tagName]
  3113. )
  3114. },
  3115. /**
  3116. * 检测node节点是否为body节点
  3117. * @method isBody
  3118. * @param { Element } node 需要检测的dom元素
  3119. * @return { Boolean } 给定的元素是否是body元素
  3120. * @example
  3121. * ```javascript
  3122. * //output: true
  3123. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3124. * ```
  3125. */
  3126. isBody: function(node) {
  3127. return node && node.nodeType == 1 && node.tagName.toLowerCase() == 'body'
  3128. },
  3129. /**
  3130. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3131. * 拆分形成的两个节点之间是node节点
  3132. * @method breakParent
  3133. * @param { Node } node 作为分界的节点对象
  3134. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3135. * @return { Node } 给定的node分界节点
  3136. * @example
  3137. * ```javascript
  3138. *
  3139. * var node = document.createElement("span"),
  3140. * wrapNode = document.createElement( "div" ),
  3141. * parent = document.createElement("p");
  3142. *
  3143. * parent.appendChild( node );
  3144. * wrapNode.appendChild( parent );
  3145. *
  3146. * //拆分前
  3147. * //output: <p><span></span></p>
  3148. * console.log( wrapNode.innerHTML );
  3149. *
  3150. *
  3151. * UE.dom.domUtils.breakParent( node, parent );
  3152. * //拆分后
  3153. * //output: <p></p><span></span><p></p>
  3154. * console.log( wrapNode.innerHTML );
  3155. *
  3156. * ```
  3157. */
  3158. breakParent: function(node, parent) {
  3159. var tmpNode,
  3160. parentClone = node,
  3161. clone = node,
  3162. leftNodes,
  3163. rightNodes
  3164. do {
  3165. parentClone = parentClone.parentNode
  3166. if (leftNodes) {
  3167. tmpNode = parentClone.cloneNode(false)
  3168. tmpNode.appendChild(leftNodes)
  3169. leftNodes = tmpNode
  3170. tmpNode = parentClone.cloneNode(false)
  3171. tmpNode.appendChild(rightNodes)
  3172. rightNodes = tmpNode
  3173. } else {
  3174. leftNodes = parentClone.cloneNode(false)
  3175. rightNodes = leftNodes.cloneNode(false)
  3176. }
  3177. while ((tmpNode = clone.previousSibling)) {
  3178. leftNodes.insertBefore(tmpNode, leftNodes.firstChild)
  3179. }
  3180. while ((tmpNode = clone.nextSibling)) {
  3181. rightNodes.appendChild(tmpNode)
  3182. }
  3183. clone = parentClone
  3184. } while (parent !== parentClone)
  3185. tmpNode = parent.parentNode
  3186. tmpNode.insertBefore(leftNodes, parent)
  3187. tmpNode.insertBefore(rightNodes, parent)
  3188. tmpNode.insertBefore(node, rightNodes)
  3189. domUtils.remove(parent)
  3190. return node
  3191. },
  3192. /**
  3193. * 检查节点node是否是空inline节点
  3194. * @method isEmptyInlineElement
  3195. * @param { Node } node 需要检测的节点对象
  3196. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3197. * @example
  3198. * ```html
  3199. * <b><i></i></b> => 1
  3200. * <b><i></i><u></u></b> => 1
  3201. * <b></b> => 1
  3202. * <b>xx<i></i></b> => 0
  3203. * ```
  3204. */
  3205. isEmptyInlineElement: function(node) {
  3206. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3207. return 0
  3208. }
  3209. node = node.firstChild
  3210. while (node) {
  3211. //如果是创建的bookmark就跳过
  3212. if (domUtils.isBookmarkNode(node)) {
  3213. return 0
  3214. }
  3215. if (
  3216. (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node)) ||
  3217. (node.nodeType == 3 && !domUtils.isWhitespace(node))
  3218. ) {
  3219. return 0
  3220. }
  3221. node = node.nextSibling
  3222. }
  3223. return 1
  3224. },
  3225. /**
  3226. * 删除node节点下首尾两端的空白文本子节点
  3227. * @method trimWhiteTextNode
  3228. * @param { Element } node 需要执行删除操作的元素对象
  3229. * @example
  3230. * ```javascript
  3231. * var node = document.createElement("div");
  3232. *
  3233. * node.appendChild( document.createTextNode( "" ) );
  3234. *
  3235. * node.appendChild( document.createElement("div") );
  3236. *
  3237. * node.appendChild( document.createTextNode( "" ) );
  3238. *
  3239. * //3
  3240. * console.log( node.childNodes.length );
  3241. *
  3242. * UE.dom.domUtils.trimWhiteTextNode( node );
  3243. *
  3244. * //1
  3245. * console.log( node.childNodes.length );
  3246. * ```
  3247. */
  3248. trimWhiteTextNode: function(node) {
  3249. function remove(dir) {
  3250. var child
  3251. while (
  3252. (child = node[dir]) &&
  3253. child.nodeType == 3 &&
  3254. domUtils.isWhitespace(child)
  3255. ) {
  3256. node.removeChild(child)
  3257. }
  3258. }
  3259. remove('firstChild')
  3260. remove('lastChild')
  3261. },
  3262. /**
  3263. * 合并node节点下相同的子节点
  3264. * @name mergeChild
  3265. * @desc
  3266. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3267. * @example
  3268. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3269. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3270. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3271. */
  3272. mergeChild: function(node, tagName, attrs) {
  3273. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase())
  3274. for (var i = 0, ci; (ci = list[i++]); ) {
  3275. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3276. continue
  3277. }
  3278. //span单独处理
  3279. if (ci.tagName.toLowerCase() == 'span') {
  3280. if (node === ci.parentNode) {
  3281. domUtils.trimWhiteTextNode(node)
  3282. if (node.childNodes.length == 1) {
  3283. node.style.cssText = ci.style.cssText + ';' + node.style.cssText
  3284. domUtils.remove(ci, true)
  3285. continue
  3286. }
  3287. }
  3288. ci.style.cssText = node.style.cssText + ';' + ci.style.cssText
  3289. if (attrs) {
  3290. var style = attrs.style
  3291. if (style) {
  3292. style = style.split(';')
  3293. for (var j = 0, s; (s = style[j++]); ) {
  3294. ci.style[utils.cssStyleToDomStyle(s.split(':')[0])] = s.split(
  3295. ':'
  3296. )[1]
  3297. }
  3298. }
  3299. }
  3300. if (domUtils.isSameStyle(ci, node)) {
  3301. domUtils.remove(ci, true)
  3302. }
  3303. continue
  3304. }
  3305. if (domUtils.isSameElement(node, ci)) {
  3306. domUtils.remove(ci, true)
  3307. }
  3308. }
  3309. },
  3310. /**
  3311. * 原生方法getElementsByTagName的封装
  3312. * @method getElementsByTagName
  3313. * @param { Node } node 目标节点对象
  3314. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3315. * @return { Array } 符合条件的节点集合
  3316. */
  3317. getElementsByTagName: function(node, name, filter) {
  3318. if (filter && utils.isString(filter)) {
  3319. var className = filter
  3320. filter = function(node) {
  3321. return domUtils.hasClass(node, className)
  3322. }
  3323. }
  3324. name = utils
  3325. .trim(name)
  3326. .replace(/[ ]{2,}/g, ' ')
  3327. .split(' ')
  3328. var arr = []
  3329. for (var n = 0, ni; (ni = name[n++]); ) {
  3330. var list = node.getElementsByTagName(ni)
  3331. for (var i = 0, ci; (ci = list[i++]); ) {
  3332. if (!filter || filter(ci)) arr.push(ci)
  3333. }
  3334. }
  3335. return arr
  3336. },
  3337. /**
  3338. * 将节点node提取到父节点上
  3339. * @method mergeToParent
  3340. * @param { Element } node 需要提取的元素对象
  3341. * @example
  3342. * ```html
  3343. * <div id="parent">
  3344. * <div id="sub">
  3345. * <span id="child"></span>
  3346. * </div>
  3347. * </div>
  3348. *
  3349. * <script>
  3350. *
  3351. * var child = document.getElementById( "child" );
  3352. *
  3353. * //output: sub
  3354. * console.log( child.parentNode.id );
  3355. *
  3356. * UE.dom.domUtils.mergeToParent( child );
  3357. *
  3358. * //output: parent
  3359. * console.log( child.parentNode.id );
  3360. *
  3361. * </script>
  3362. * ```
  3363. */
  3364. mergeToParent: function(node) {
  3365. var parent = node.parentNode
  3366. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3367. if (parent.tagName == node.tagName || parent.tagName == 'A') {
  3368. //针对a标签单独处理
  3369. domUtils.trimWhiteTextNode(parent)
  3370. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3371. if (
  3372. (parent.tagName == 'SPAN' && !domUtils.isSameStyle(parent, node)) ||
  3373. (parent.tagName == 'A' && node.tagName == 'SPAN')
  3374. ) {
  3375. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3376. node.style.cssText =
  3377. parent.style.cssText + ';' + node.style.cssText
  3378. parent = parent.parentNode
  3379. continue
  3380. } else {
  3381. parent.style.cssText += ';' + node.style.cssText
  3382. //trace:952 a标签要保持下划线
  3383. if (parent.tagName == 'A') {
  3384. parent.style.textDecoration = 'underline'
  3385. }
  3386. }
  3387. }
  3388. if (parent.tagName != 'A') {
  3389. parent === node.parentNode && domUtils.remove(node, true)
  3390. break
  3391. }
  3392. }
  3393. parent = parent.parentNode
  3394. }
  3395. },
  3396. /**
  3397. * 合并节点node的左右兄弟节点
  3398. * @method mergeSibling
  3399. * @param { Element } node 需要合并的目标节点
  3400. * @example
  3401. * ```html
  3402. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3403. *
  3404. * <script>
  3405. * var demoNode = document.getElementById("test");
  3406. * UE.dom.domUtils.mergeSibling( demoNode );
  3407. * //output: xxxxoooxxxx
  3408. * console.log( demoNode.innerHTML );
  3409. * </script>
  3410. * ```
  3411. */
  3412. /**
  3413. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3414. * @method mergeSibling
  3415. * @param { Element } node 需要合并的目标节点
  3416. * @param { Boolean } ignorePre 是否忽略合并左节点
  3417. * @example
  3418. * ```html
  3419. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3420. *
  3421. * <script>
  3422. * var demoNode = document.getElementById("test");
  3423. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3424. * //output: oooxxxx
  3425. * console.log( demoNode.innerHTML );
  3426. * </script>
  3427. * ```
  3428. */
  3429. /**
  3430. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3431. * @method mergeSibling
  3432. * @param { Element } node 需要合并的目标节点
  3433. * @param { Boolean } ignorePre 是否忽略合并左节点
  3434. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3435. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3436. * @example
  3437. * ```html
  3438. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3439. *
  3440. * <script>
  3441. * var demoNode = document.getElementById("test");
  3442. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3443. * //output: xxxxooo
  3444. * console.log( demoNode.innerHTML );
  3445. * </script>
  3446. * ```
  3447. */
  3448. mergeSibling: function(node, ignorePre, ignoreNext) {
  3449. function merge(rtl, start, node) {
  3450. var next
  3451. if (
  3452. (next = node[rtl]) &&
  3453. !domUtils.isBookmarkNode(next) &&
  3454. next.nodeType == 1 &&
  3455. domUtils.isSameElement(node, next)
  3456. ) {
  3457. while (next.firstChild) {
  3458. if (start == 'firstChild') {
  3459. node.insertBefore(next.lastChild, node.firstChild)
  3460. } else {
  3461. node.appendChild(next.firstChild)
  3462. }
  3463. }
  3464. domUtils.remove(next)
  3465. }
  3466. }
  3467. !ignorePre && merge('previousSibling', 'firstChild', node)
  3468. !ignoreNext && merge('nextSibling', 'lastChild', node)
  3469. },
  3470. /**
  3471. * 设置节点node及其子节点不会被选中
  3472. * @method unSelectable
  3473. * @param { Element } node 需要执行操作的dom元素
  3474. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3475. * @example
  3476. * ```javascript
  3477. * UE.dom.domUtils.unSelectable( document.body );
  3478. * ```
  3479. */
  3480. unSelectable:
  3481. (ie && browser.ie9below) || browser.opera
  3482. ? function(node) {
  3483. //for ie9
  3484. node.onselectstart = function() {
  3485. return false
  3486. }
  3487. node.onclick = node.onkeyup = node.onkeydown = function() {
  3488. return false
  3489. }
  3490. node.unselectable = 'on'
  3491. node.setAttribute('unselectable', 'on')
  3492. for (var i = 0, ci; (ci = node.all[i++]); ) {
  3493. switch (ci.tagName.toLowerCase()) {
  3494. case 'iframe':
  3495. case 'textarea':
  3496. case 'input':
  3497. case 'select':
  3498. break
  3499. default:
  3500. ci.unselectable = 'on'
  3501. node.setAttribute('unselectable', 'on')
  3502. }
  3503. }
  3504. }
  3505. : function(node) {
  3506. node.style.MozUserSelect = node.style.webkitUserSelect = node.style.msUserSelect = node.style.KhtmlUserSelect =
  3507. 'none'
  3508. },
  3509. /**
  3510. * 删除节点node上的指定属性名称的属性
  3511. * @method removeAttributes
  3512. * @param { Node } node 需要删除属性的节点对象
  3513. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3514. * @example
  3515. * ```html
  3516. * <div id="wrap">
  3517. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3518. * </div>
  3519. *
  3520. * <script>
  3521. *
  3522. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3523. *
  3524. * //output: <span style="font-size:14px;">xxxxx</span>
  3525. * console.log( document.getElementById("wrap").innerHTML );
  3526. *
  3527. * </script>
  3528. * ```
  3529. */
  3530. /**
  3531. * 删除节点node上的指定属性名称的属性
  3532. * @method removeAttributes
  3533. * @param { Node } node 需要删除属性的节点对象
  3534. * @param { Array } attrNames 需要删除的属性名数组
  3535. * @example
  3536. * ```html
  3537. * <div id="wrap">
  3538. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3539. * </div>
  3540. *
  3541. * <script>
  3542. *
  3543. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3544. *
  3545. * //output: <span style="font-size:14px;">xxxxx</span>
  3546. * console.log( document.getElementById("wrap").innerHTML );
  3547. *
  3548. * </script>
  3549. * ```
  3550. */
  3551. removeAttributes: function(node, attrNames) {
  3552. attrNames = utils.isArray(attrNames)
  3553. ? attrNames
  3554. : utils
  3555. .trim(attrNames)
  3556. .replace(/[ ]{2,}/g, ' ')
  3557. .split(' ')
  3558. for (var i = 0, ci; (ci = attrNames[i++]); ) {
  3559. ci = attrFix[ci] || ci
  3560. switch (ci) {
  3561. case 'className':
  3562. node[ci] = ''
  3563. break
  3564. case 'style':
  3565. node.style.cssText = ''
  3566. var val = node.getAttributeNode('style')
  3567. !browser.ie && val && node.removeAttributeNode(val)
  3568. }
  3569. node.removeAttribute(ci)
  3570. }
  3571. },
  3572. /**
  3573. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3574. * @method createElement
  3575. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3576. * @param { String } tagName 需要创建的元素的标签名
  3577. * @param { Object } attrs 新创建的元素的属性key-value集合
  3578. * @return { Element } 新创建的元素对象
  3579. * @example
  3580. * ```javascript
  3581. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3582. * id: 'test'
  3583. * } );
  3584. *
  3585. * //output: DIV
  3586. * console.log( ele.tagName );
  3587. *
  3588. * //output: test
  3589. * console.log( ele.id );
  3590. *
  3591. * ```
  3592. */
  3593. createElement: function(doc, tag, attrs) {
  3594. return domUtils.setAttributes(doc.createElement(tag), attrs)
  3595. },
  3596. /**
  3597. * 为节点node添加属性attrs,attrs为属性键值对
  3598. * @method setAttributes
  3599. * @param { Element } node 需要设置属性的元素对象
  3600. * @param { Object } attrs 需要设置的属性名-值对
  3601. * @return { Element } 设置属性的元素对象
  3602. * @example
  3603. * ```html
  3604. * <span id="test"></span>
  3605. *
  3606. * <script>
  3607. *
  3608. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3609. * id: 'demo'
  3610. * } );
  3611. *
  3612. * //output: demo
  3613. * console.log( testNode.id );
  3614. *
  3615. * </script>
  3616. *
  3617. */
  3618. setAttributes: function(node, attrs) {
  3619. for (var attr in attrs) {
  3620. if (attrs.hasOwnProperty(attr)) {
  3621. var value = attrs[attr]
  3622. switch (attr) {
  3623. case 'class':
  3624. //ie下要这样赋值,setAttribute不起作用
  3625. node.className = value
  3626. break
  3627. case 'style':
  3628. node.style.cssText = node.style.cssText + ';' + value
  3629. break
  3630. case 'innerHTML':
  3631. node[attr] = value
  3632. break
  3633. case 'value':
  3634. node.value = value
  3635. break
  3636. default:
  3637. node.setAttribute(attrFix[attr] || attr, value)
  3638. }
  3639. }
  3640. }
  3641. return node
  3642. },
  3643. /**
  3644. * 获取元素element经过计算后的样式值
  3645. * @method getComputedStyle
  3646. * @param { Element } element 需要获取样式的元素对象
  3647. * @param { String } styleName 需要获取的样式名
  3648. * @return { String } 获取到的样式值
  3649. * @example
  3650. * ```html
  3651. * <style type="text/css">
  3652. * #test {
  3653. * font-size: 15px;
  3654. * }
  3655. * </style>
  3656. *
  3657. * <span id="test"></span>
  3658. *
  3659. * <script>
  3660. * //output: 15px
  3661. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3662. * </script>
  3663. * ```
  3664. */
  3665. getComputedStyle: function(element, styleName) {
  3666. //一下的属性单独处理
  3667. var pros = 'width height top left'
  3668. if (pros.indexOf(styleName) > -1) {
  3669. return (
  3670. element[
  3671. 'offset' +
  3672. styleName.replace(/^\w/, function(s) {
  3673. return s.toUpperCase()
  3674. })
  3675. ] + 'px'
  3676. )
  3677. }
  3678. //忽略文本节点
  3679. if (element.nodeType == 3) {
  3680. element = element.parentNode
  3681. }
  3682. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3683. if (
  3684. browser.ie &&
  3685. browser.version < 9 &&
  3686. styleName == 'font-size' &&
  3687. !element.style.fontSize &&
  3688. !dtd.$empty[element.tagName] &&
  3689. !dtd.$nonChild[element.tagName]
  3690. ) {
  3691. var span = element.ownerDocument.createElement('span')
  3692. span.style.cssText = 'padding:0;border:0;font-family:simsun;'
  3693. span.innerHTML = '.'
  3694. element.appendChild(span)
  3695. var result = span.offsetHeight
  3696. element.removeChild(span)
  3697. span = null
  3698. return result + 'px'
  3699. }
  3700. try {
  3701. var value =
  3702. domUtils.getStyle(element, styleName) ||
  3703. (window.getComputedStyle
  3704. ? domUtils
  3705. .getWindow(element)
  3706. .getComputedStyle(element, '')
  3707. .getPropertyValue(styleName)
  3708. : (element.currentStyle || element.style)[
  3709. utils.cssStyleToDomStyle(styleName)
  3710. ])
  3711. } catch (e) {
  3712. return ''
  3713. }
  3714. return utils.transUnitToPx(utils.fixColor(styleName, value))
  3715. },
  3716. /**
  3717. * 删除元素element指定的className
  3718. * @method removeClasses
  3719. * @param { Element } ele 需要删除class的元素节点
  3720. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3721. * @example
  3722. * ```html
  3723. * <span id="test" class="test1 test2 test3">xxx</span>
  3724. *
  3725. * <script>
  3726. *
  3727. * var testNode = document.getElementById( "test" );
  3728. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3729. *
  3730. * //output: test3
  3731. * console.log( testNode.className );
  3732. *
  3733. * </script>
  3734. * ```
  3735. */
  3736. /**
  3737. * 删除元素element指定的className
  3738. * @method removeClasses
  3739. * @param { Element } ele 需要删除class的元素节点
  3740. * @param { Array } classNames 需要删除的className数组
  3741. * @example
  3742. * ```html
  3743. * <span id="test" class="test1 test2 test3">xxx</span>
  3744. *
  3745. * <script>
  3746. *
  3747. * var testNode = document.getElementById( "test" );
  3748. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3749. *
  3750. * //output: test3
  3751. * console.log( testNode.className );
  3752. *
  3753. * </script>
  3754. * ```
  3755. */
  3756. removeClasses: function(elm, classNames) {
  3757. classNames = utils.isArray(classNames)
  3758. ? classNames
  3759. : utils
  3760. .trim(classNames)
  3761. .replace(/[ ]{2,}/g, ' ')
  3762. .split(' ')
  3763. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3764. cls = cls.replace(new RegExp('\\b' + ci + '\\b'), '')
  3765. }
  3766. cls = utils.trim(cls).replace(/[ ]{2,}/g, ' ')
  3767. if (cls) {
  3768. elm.className = cls
  3769. } else {
  3770. domUtils.removeAttributes(elm, ['class'])
  3771. }
  3772. },
  3773. /**
  3774. * 给元素element添加className
  3775. * @method addClass
  3776. * @param { Node } ele 需要增加className的元素
  3777. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3778. * @remind 相同的类名不会被重复添加
  3779. * @example
  3780. * ```html
  3781. * <span id="test" class="cls1 cls2"></span>
  3782. *
  3783. * <script>
  3784. * var testNode = document.getElementById("test");
  3785. *
  3786. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3787. *
  3788. * //output: cl1 cls2 cls3 cls4
  3789. * console.log( testNode.className );
  3790. *
  3791. * <script>
  3792. * ```
  3793. */
  3794. /**
  3795. * 给元素element添加className
  3796. * @method addClass
  3797. * @param { Node } ele 需要增加className的元素
  3798. * @param { Array } classNames 需要添加的className的数组
  3799. * @remind 相同的类名不会被重复添加
  3800. * @example
  3801. * ```html
  3802. * <span id="test" class="cls1 cls2"></span>
  3803. *
  3804. * <script>
  3805. * var testNode = document.getElementById("test");
  3806. *
  3807. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3808. *
  3809. * //output: cl1 cls2 cls3 cls4
  3810. * console.log( testNode.className );
  3811. *
  3812. * <script>
  3813. * ```
  3814. */
  3815. addClass: function(elm, classNames) {
  3816. if (!elm) return
  3817. classNames = utils
  3818. .trim(classNames)
  3819. .replace(/[ ]{2,}/g, ' ')
  3820. .split(' ')
  3821. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3822. if (!new RegExp('\\b' + ci + '\\b').test(cls)) {
  3823. cls += ' ' + ci
  3824. }
  3825. }
  3826. elm.className = utils.trim(cls)
  3827. },
  3828. /**
  3829. * 判断元素element是否包含给定的样式类名className
  3830. * @method hasClass
  3831. * @param { Node } ele 需要检测的元素
  3832. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3833. * @return { Boolean } 元素是否包含所有给定的className
  3834. * @example
  3835. * ```html
  3836. * <span id="test1" class="cls1 cls2"></span>
  3837. *
  3838. * <script>
  3839. * var test1 = document.getElementById("test1");
  3840. *
  3841. * //output: false
  3842. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3843. *
  3844. * //output: true
  3845. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3846. * </script>
  3847. * ```
  3848. */
  3849. /**
  3850. * 判断元素element是否包含给定的样式类名className
  3851. * @method hasClass
  3852. * @param { Node } ele 需要检测的元素
  3853. * @param { Array } classNames 需要检测的className数组
  3854. * @return { Boolean } 元素是否包含所有给定的className
  3855. * @example
  3856. * ```html
  3857. * <span id="test1" class="cls1 cls2"></span>
  3858. *
  3859. * <script>
  3860. * var test1 = document.getElementById("test1");
  3861. *
  3862. * //output: false
  3863. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3864. *
  3865. * //output: true
  3866. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3867. * </script>
  3868. * ```
  3869. */
  3870. hasClass: function(element, className) {
  3871. if (utils.isRegExp(className)) {
  3872. return className.test(element.className)
  3873. }
  3874. className = utils
  3875. .trim(className)
  3876. .replace(/[ ]{2,}/g, ' ')
  3877. .split(' ')
  3878. for (var i = 0, ci, cls = element.className; (ci = className[i++]); ) {
  3879. if (!new RegExp('\\b' + ci + '\\b', 'i').test(cls)) {
  3880. return false
  3881. }
  3882. }
  3883. return i - 1 == className.length
  3884. },
  3885. /**
  3886. * 阻止事件默认行为
  3887. * @method preventDefault
  3888. * @param { Event } evt 需要阻止默认行为的事件对象
  3889. * @example
  3890. * ```javascript
  3891. * UE.dom.domUtils.preventDefault( evt );
  3892. * ```
  3893. */
  3894. preventDefault: function(evt) {
  3895. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  3896. },
  3897. /**
  3898. * 删除元素element指定的样式
  3899. * @method removeStyle
  3900. * @param { Element } element 需要删除样式的元素
  3901. * @param { String } styleName 需要删除的样式名
  3902. * @example
  3903. * ```html
  3904. * <span id="test" style="color: red; background: blue;"></span>
  3905. *
  3906. * <script>
  3907. *
  3908. * var testNode = document.getElementById("test");
  3909. *
  3910. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3911. *
  3912. * //output: background: blue;
  3913. * console.log( testNode.style.cssText );
  3914. *
  3915. * </script>
  3916. * ```
  3917. */
  3918. removeStyle: function(element, name) {
  3919. if (browser.ie) {
  3920. //针对color先单独处理一下
  3921. if (name == 'color') {
  3922. name = '(^|;)' + name
  3923. }
  3924. element.style.cssText = element.style.cssText.replace(
  3925. new RegExp(name + '[^:]*:[^;]+;?', 'ig'),
  3926. ''
  3927. )
  3928. } else {
  3929. if (element.style.removeProperty) {
  3930. element.style.removeProperty(name)
  3931. } else {
  3932. element.style.removeAttribute(utils.cssStyleToDomStyle(name))
  3933. }
  3934. }
  3935. if (!element.style.cssText) {
  3936. domUtils.removeAttributes(element, ['style'])
  3937. }
  3938. },
  3939. /**
  3940. * 获取元素element的style属性的指定值
  3941. * @method getStyle
  3942. * @param { Element } element 需要获取属性值的元素
  3943. * @param { String } styleName 需要获取的style的名称
  3944. * @warning 该方法仅获取元素style属性中所标明的值
  3945. * @return { String } 该元素包含指定的style属性值
  3946. * @example
  3947. * ```html
  3948. * <div id="test" style="color: red;"></div>
  3949. *
  3950. * <script>
  3951. *
  3952. * var testNode = document.getElementById( "test" );
  3953. *
  3954. * //output: red
  3955. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3956. *
  3957. * //output: ""
  3958. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3959. *
  3960. * </script>
  3961. * ```
  3962. */
  3963. getStyle: function(element, name) {
  3964. var value = element.style[utils.cssStyleToDomStyle(name)]
  3965. return utils.fixColor(name, value)
  3966. },
  3967. /**
  3968. * 为元素element设置样式属性值
  3969. * @method setStyle
  3970. * @param { Element } element 需要设置样式的元素
  3971. * @param { String } styleName 样式名
  3972. * @param { String } styleValue 样式值
  3973. * @example
  3974. * ```html
  3975. * <div id="test"></div>
  3976. *
  3977. * <script>
  3978. *
  3979. * var testNode = document.getElementById( "test" );
  3980. *
  3981. * //output: ""
  3982. * console.log( testNode.style.color );
  3983. *
  3984. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3985. * //output: "red"
  3986. * console.log( testNode.style.color );
  3987. *
  3988. * </script>
  3989. * ```
  3990. */
  3991. setStyle: function(element, name, value) {
  3992. element.style[utils.cssStyleToDomStyle(name)] = value
  3993. if (!utils.trim(element.style.cssText)) {
  3994. this.removeAttributes(element, 'style')
  3995. }
  3996. },
  3997. /**
  3998. * 为元素element设置多个样式属性值
  3999. * @method setStyles
  4000. * @param { Element } element 需要设置样式的元素
  4001. * @param { Object } styles 样式名值对
  4002. * @example
  4003. * ```html
  4004. * <div id="test"></div>
  4005. *
  4006. * <script>
  4007. *
  4008. * var testNode = document.getElementById( "test" );
  4009. *
  4010. * //output: ""
  4011. * console.log( testNode.style.color );
  4012. *
  4013. * UE.dom.domUtils.setStyles( testNode, {
  4014. * 'color': 'red'
  4015. * } );
  4016. * //output: "red"
  4017. * console.log( testNode.style.color );
  4018. *
  4019. * </script>
  4020. * ```
  4021. */
  4022. setStyles: function(element, styles) {
  4023. for (var name in styles) {
  4024. if (styles.hasOwnProperty(name)) {
  4025. domUtils.setStyle(element, name, styles[name])
  4026. }
  4027. }
  4028. },
  4029. /**
  4030. * 删除_moz_dirty属性
  4031. * @private
  4032. * @method removeDirtyAttr
  4033. */
  4034. removeDirtyAttr: function(node) {
  4035. for (
  4036. var i = 0, ci, nodes = node.getElementsByTagName('*');
  4037. (ci = nodes[i++]);
  4038. ) {
  4039. ci.removeAttribute('_moz_dirty')
  4040. }
  4041. node.removeAttribute('_moz_dirty')
  4042. },
  4043. /**
  4044. * 获取子节点的数量
  4045. * @method getChildCount
  4046. * @param { Element } node 需要检测的元素
  4047. * @return { Number } 给定的node元素的子节点数量
  4048. * @example
  4049. * ```html
  4050. * <div id="test">
  4051. * <span></span>
  4052. * </div>
  4053. *
  4054. * <script>
  4055. *
  4056. * //output: 3
  4057. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  4058. *
  4059. * </script>
  4060. * ```
  4061. */
  4062. /**
  4063. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  4064. * @method getChildCount
  4065. * @param { Element } node 需要检测的元素
  4066. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  4067. * @return { Number } 符合过滤条件的node元素的子节点数量
  4068. * @example
  4069. * ```html
  4070. * <div id="test">
  4071. * <span></span>
  4072. * </div>
  4073. *
  4074. * <script>
  4075. *
  4076. * //output: 1
  4077. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  4078. *
  4079. * return node.nodeType === 1;
  4080. *
  4081. * } ) );
  4082. *
  4083. * </script>
  4084. * ```
  4085. */
  4086. getChildCount: function(node, fn) {
  4087. var count = 0,
  4088. first = node.firstChild
  4089. fn =
  4090. fn ||
  4091. function() {
  4092. return 1
  4093. }
  4094. while (first) {
  4095. if (fn(first)) {
  4096. count++
  4097. }
  4098. first = first.nextSibling
  4099. }
  4100. return count
  4101. },
  4102. /**
  4103. * 判断给定节点是否为空节点
  4104. * @method isEmptyNode
  4105. * @param { Node } node 需要检测的节点对象
  4106. * @return { Boolean } 节点是否为空
  4107. * @example
  4108. * ```javascript
  4109. * UE.dom.domUtils.isEmptyNode( document.body );
  4110. * ```
  4111. */
  4112. isEmptyNode: function(node) {
  4113. return (
  4114. !node.firstChild ||
  4115. domUtils.getChildCount(node, function(node) {
  4116. return (
  4117. !domUtils.isBr(node) &&
  4118. !domUtils.isBookmarkNode(node) &&
  4119. !domUtils.isWhitespace(node)
  4120. )
  4121. }) == 0
  4122. )
  4123. },
  4124. clearSelectedArr: function(nodes) {
  4125. var node
  4126. while ((node = nodes.pop())) {
  4127. domUtils.removeAttributes(node, ['class'])
  4128. }
  4129. },
  4130. /**
  4131. * 将显示区域滚动到指定节点的位置
  4132. * @method scrollToView
  4133. * @param {Node} node 节点
  4134. * @param {window} win window对象
  4135. * @param {Number} offsetTop 距离上方的偏移量
  4136. */
  4137. scrollToView: function(node, win, offsetTop) {
  4138. var getViewPaneSize = function() {
  4139. var doc = win.document,
  4140. mode = doc.compatMode == 'CSS1Compat'
  4141. return {
  4142. width:
  4143. (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) ||
  4144. 0,
  4145. height:
  4146. (mode
  4147. ? doc.documentElement.clientHeight
  4148. : doc.body.clientHeight) || 0
  4149. }
  4150. },
  4151. getScrollPosition = function(win) {
  4152. if ('pageXOffset' in win) {
  4153. return {
  4154. x: win.pageXOffset || 0,
  4155. y: win.pageYOffset || 0
  4156. }
  4157. } else {
  4158. var doc = win.document
  4159. return {
  4160. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4161. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0
  4162. }
  4163. }
  4164. }
  4165. var winHeight = getViewPaneSize().height,
  4166. offset = winHeight * -1 + offsetTop
  4167. offset += node.offsetHeight || 0
  4168. var elementPosition = domUtils.getXY(node)
  4169. offset += elementPosition.y
  4170. var currentScroll = getScrollPosition(win).y
  4171. // offset += 50;
  4172. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4173. win.scrollTo(0, offset + (offset < 0 ? -20 : 20))
  4174. }
  4175. },
  4176. /**
  4177. * 判断给定节点是否为br
  4178. * @method isBr
  4179. * @param { Node } node 需要判断的节点对象
  4180. * @return { Boolean } 给定的节点是否是br节点
  4181. */
  4182. isBr: function(node) {
  4183. return node.nodeType == 1 && node.tagName == 'BR'
  4184. },
  4185. /**
  4186. * 判断给定的节点是否是一个“填充”节点
  4187. * @private
  4188. * @method isFillChar
  4189. * @param { Node } node 需要判断的节点
  4190. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4191. * @returns { Boolean } 节点是否是填充节点
  4192. */
  4193. isFillChar: function(node, isInStart) {
  4194. if (node.nodeType != 3) return false
  4195. var text = node.nodeValue
  4196. if (isInStart) {
  4197. return new RegExp('^' + domUtils.fillChar).test(text)
  4198. }
  4199. return !text.replace(new RegExp(domUtils.fillChar, 'g'), '').length
  4200. },
  4201. isStartInblock: function(range) {
  4202. var tmpRange = range.cloneRange(),
  4203. flag = 0,
  4204. start = tmpRange.startContainer,
  4205. tmp
  4206. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4207. start = start.childNodes[tmpRange.startOffset]
  4208. var pre = start.previousSibling
  4209. while (pre && domUtils.isFillChar(pre)) {
  4210. start = pre
  4211. pre = pre.previousSibling
  4212. }
  4213. }
  4214. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4215. tmpRange.setStartBefore(start)
  4216. start = tmpRange.startContainer
  4217. }
  4218. while (start && domUtils.isFillChar(start)) {
  4219. tmp = start
  4220. start = start.previousSibling
  4221. }
  4222. if (tmp) {
  4223. tmpRange.setStartBefore(tmp)
  4224. start = tmpRange.startContainer
  4225. }
  4226. if (
  4227. start.nodeType == 1 &&
  4228. domUtils.isEmptyNode(start) &&
  4229. tmpRange.startOffset == 1
  4230. ) {
  4231. tmpRange.setStart(start, 0).collapse(true)
  4232. }
  4233. while (!tmpRange.startOffset) {
  4234. start = tmpRange.startContainer
  4235. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4236. flag = 1
  4237. break
  4238. }
  4239. var pre = tmpRange.startContainer.previousSibling,
  4240. tmpNode
  4241. if (!pre) {
  4242. tmpRange.setStartBefore(tmpRange.startContainer)
  4243. } else {
  4244. while (pre && domUtils.isFillChar(pre)) {
  4245. tmpNode = pre
  4246. pre = pre.previousSibling
  4247. }
  4248. if (tmpNode) {
  4249. tmpRange.setStartBefore(tmpNode)
  4250. } else {
  4251. tmpRange.setStartBefore(tmpRange.startContainer)
  4252. }
  4253. }
  4254. }
  4255. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0
  4256. },
  4257. /**
  4258. * 判断给定的元素是否是一个空元素
  4259. * @method isEmptyBlock
  4260. * @param { Element } node 需要判断的元素
  4261. * @return { Boolean } 是否是空元素
  4262. * @example
  4263. * ```html
  4264. * <div id="test"></div>
  4265. *
  4266. * <script>
  4267. * //output: true
  4268. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4269. * </script>
  4270. * ```
  4271. */
  4272. /**
  4273. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4274. * @method isEmptyBlock
  4275. * @param { Element } node 需要判断的元素
  4276. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4277. * @return { Boolean } 是否是空元素
  4278. */
  4279. isEmptyBlock: function(node, reg) {
  4280. if (node.nodeType != 1) return 0
  4281. reg = reg || new RegExp('[ \xa0\t\r\n' + domUtils.fillChar + ']', 'g')
  4282. if (
  4283. node[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').length >
  4284. 0
  4285. ) {
  4286. return 0
  4287. }
  4288. for (var n in dtd.$isNotEmpty) {
  4289. if (node.getElementsByTagName(n).length) {
  4290. return 0
  4291. }
  4292. }
  4293. return 1
  4294. },
  4295. /**
  4296. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4297. * @method setViewportOffset
  4298. * @param { Element } element 需要设置偏移量的元素
  4299. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4300. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4301. * offset.top的距离
  4302. * @example
  4303. * ```html
  4304. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4305. *
  4306. * <script>
  4307. *
  4308. * var testNode = document.getElementById("test");
  4309. *
  4310. * UE.dom.domUtils.setViewportOffset( testNode, {
  4311. * left: 200,
  4312. * top: 50
  4313. * } );
  4314. *
  4315. * //output: top: 300px; left: 100px; position: absolute;
  4316. * console.log( testNode.style.cssText );
  4317. *
  4318. * </script>
  4319. * ```
  4320. */
  4321. setViewportOffset: function(element, offset) {
  4322. var left = parseInt(element.style.left) | 0
  4323. var top = parseInt(element.style.top) | 0
  4324. var rect = element.getBoundingClientRect()
  4325. var offsetLeft = offset.left - rect.left
  4326. var offsetTop = offset.top - rect.top
  4327. if (offsetLeft) {
  4328. element.style.left = left + offsetLeft + 'px'
  4329. }
  4330. if (offsetTop) {
  4331. element.style.top = top + offsetTop + 'px'
  4332. }
  4333. },
  4334. /**
  4335. * 用“填充字符”填充节点
  4336. * @method fillNode
  4337. * @private
  4338. * @param { DomDocument } doc 填充的节点所在的docment对象
  4339. * @param { Node } node 需要填充的节点对象
  4340. * @example
  4341. * ```html
  4342. * <div id="test"></div>
  4343. *
  4344. * <script>
  4345. * var testNode = document.getElementById("test");
  4346. *
  4347. * //output: 0
  4348. * console.log( testNode.childNodes.length );
  4349. *
  4350. * UE.dom.domUtils.fillNode( document, testNode );
  4351. *
  4352. * //output: 1
  4353. * console.log( testNode.childNodes.length );
  4354. *
  4355. * </script>
  4356. * ```
  4357. */
  4358. fillNode: function(doc, node) {
  4359. var tmpNode = browser.ie
  4360. ? doc.createTextNode(domUtils.fillChar)
  4361. : doc.createElement('br')
  4362. node.innerHTML = ''
  4363. node.appendChild(tmpNode)
  4364. },
  4365. /**
  4366. * 把节点src的所有子节点追加到另一个节点tag上去
  4367. * @method moveChild
  4368. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4369. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4370. * @example
  4371. * ```html
  4372. * <div id="test1">
  4373. * <span></span>
  4374. * </div>
  4375. * <div id="test2">
  4376. * <div></div>
  4377. * </div>
  4378. *
  4379. * <script>
  4380. *
  4381. * var test1 = document.getElementById("test1"),
  4382. * test2 = document.getElementById("test2");
  4383. *
  4384. * UE.dom.domUtils.moveChild( test1, test2 );
  4385. *
  4386. * //output: ""(空字符串)
  4387. * console.log( test1.innerHTML );
  4388. *
  4389. * //output: "<div></div><span></span>"
  4390. * console.log( test2.innerHTML );
  4391. *
  4392. * </script>
  4393. * ```
  4394. */
  4395. /**
  4396. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4397. * @method moveChild
  4398. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4399. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4400. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4401. * @example
  4402. * ```html
  4403. * <div id="test1">
  4404. * <span></span>
  4405. * </div>
  4406. * <div id="test2">
  4407. * <div></div>
  4408. * </div>
  4409. *
  4410. * <script>
  4411. *
  4412. * var test1 = document.getElementById("test1"),
  4413. * test2 = document.getElementById("test2");
  4414. *
  4415. * UE.dom.domUtils.moveChild( test1, test2, true );
  4416. *
  4417. * //output: ""(空字符串)
  4418. * console.log( test1.innerHTML );
  4419. *
  4420. * //output: "<span></span><div></div>"
  4421. * console.log( test2.innerHTML );
  4422. *
  4423. * </script>
  4424. * ```
  4425. */
  4426. moveChild: function(src, tag, dir) {
  4427. while (src.firstChild) {
  4428. if (dir && tag.firstChild) {
  4429. tag.insertBefore(src.lastChild, tag.firstChild)
  4430. } else {
  4431. tag.appendChild(src.firstChild)
  4432. }
  4433. }
  4434. },
  4435. /**
  4436. * 判断节点的标签上是否不存在任何属性
  4437. * @method hasNoAttributes
  4438. * @private
  4439. * @param { Node } node 需要检测的节点对象
  4440. * @return { Boolean } 节点是否不包含任何属性
  4441. * @example
  4442. * ```html
  4443. * <div id="test"><span>xxxx</span></div>
  4444. *
  4445. * <script>
  4446. *
  4447. * //output: false
  4448. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4449. *
  4450. * //output: true
  4451. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4452. *
  4453. * </script>
  4454. * ```
  4455. */
  4456. hasNoAttributes: function(node) {
  4457. return browser.ie
  4458. ? /^<\w+\s*?>/.test(node.outerHTML)
  4459. : node.attributes.length == 0
  4460. },
  4461. /**
  4462. * 检测节点是否是UEditor所使用的辅助节点
  4463. * @method isCustomeNode
  4464. * @private
  4465. * @param { Node } node 需要检测的节点
  4466. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4467. * @return { Boolean } 给定的节点是否是一个辅助节点
  4468. */
  4469. isCustomeNode: function(node) {
  4470. return node.nodeType == 1 && node.getAttribute('_ue_custom_node_')
  4471. },
  4472. /**
  4473. * 检测节点的标签是否是给定的标签
  4474. * @method isTagNode
  4475. * @param { Node } node 需要检测的节点对象
  4476. * @param { String } tagName 标签
  4477. * @return { Boolean } 节点的标签是否是给定的标签
  4478. * @example
  4479. * ```html
  4480. * <div id="test"></div>
  4481. *
  4482. * <script>
  4483. *
  4484. * //output: true
  4485. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4486. *
  4487. * </script>
  4488. * ```
  4489. */
  4490. isTagNode: function(node, tagNames) {
  4491. return (
  4492. node.nodeType == 1 &&
  4493. new RegExp('\\b' + node.tagName + '\\b', 'i').test(tagNames)
  4494. )
  4495. },
  4496. /**
  4497. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4498. * @method filterNodeList
  4499. * @param { Array } nodeList 需要过滤的节点数组
  4500. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4501. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4502. * @example
  4503. * ```javascript
  4504. * var divNodes = document.getElementsByTagName("div");
  4505. * divNodes = [].slice.call( divNodes, 0 );
  4506. *
  4507. * //output: null
  4508. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4509. * return node.tagName.toLowerCase() !== 'div';
  4510. * } ) );
  4511. * ```
  4512. */
  4513. /**
  4514. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4515. * @method filterNodeList
  4516. * @param { Array } nodeList 需要过滤的节点数组
  4517. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4518. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4519. * @example
  4520. * ```javascript
  4521. * var divNodes = document.getElementsByTagName("div");
  4522. * divNodes = [].slice.call( divNodes, 0 );
  4523. *
  4524. * //output: null
  4525. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4526. * ```
  4527. */
  4528. /**
  4529. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4530. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4531. * @method filterNodeList
  4532. * @param { Array } nodeList 需要过滤的节点数组
  4533. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4534. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4535. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4536. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4537. * @example
  4538. * ```javascript
  4539. * var divNodes = document.getElementsByTagName("div");
  4540. * divNodes = [].slice.call( divNodes, 0 );
  4541. *
  4542. * //output: 3(假定有3个div)
  4543. * console.log( divNodes.length );
  4544. *
  4545. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4546. * return node.tagName.toLowerCase() === 'div';
  4547. * }, true );
  4548. *
  4549. * //output: 3
  4550. * console.log( nodes.length );
  4551. *
  4552. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4553. * return node.tagName.toLowerCase() === 'div';
  4554. * }, false );
  4555. *
  4556. * //output: div
  4557. * console.log( node.nodeName );
  4558. * ```
  4559. */
  4560. filterNodeList: function(nodelist, filter, forAll) {
  4561. var results = []
  4562. if (!utils.isFunction(filter)) {
  4563. var str = filter
  4564. filter = function(n) {
  4565. return (
  4566. utils.indexOf(
  4567. utils.isArray(str) ? str : str.split(' '),
  4568. n.tagName.toLowerCase()
  4569. ) != -1
  4570. )
  4571. }
  4572. }
  4573. utils.each(nodelist, function(n) {
  4574. filter(n) && results.push(n)
  4575. })
  4576. return results.length == 0
  4577. ? null
  4578. : results.length == 1 || !forAll
  4579. ? results[0]
  4580. : results
  4581. },
  4582. /**
  4583. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4584. * @method isInNodeEndBoundary
  4585. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4586. * @param node 需要检测的节点对象
  4587. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4588. */
  4589. isInNodeEndBoundary: function(rng, node) {
  4590. var start = rng.startContainer
  4591. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4592. return 0
  4593. }
  4594. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4595. return 0
  4596. }
  4597. while (start !== node) {
  4598. if (start.nextSibling) {
  4599. return 0
  4600. }
  4601. start = start.parentNode
  4602. }
  4603. return 1
  4604. },
  4605. isBoundaryNode: function(node, dir) {
  4606. var tmp
  4607. while (!domUtils.isBody(node)) {
  4608. tmp = node
  4609. node = node.parentNode
  4610. if (tmp !== node[dir]) {
  4611. return false
  4612. }
  4613. }
  4614. return true
  4615. },
  4616. fillHtml: browser.ie11below ? '&nbsp;' : '<br/>'
  4617. })
  4618. var fillCharReg = new RegExp(domUtils.fillChar, 'g')
  4619. // core/Range.js
  4620. /**
  4621. * Range封装
  4622. * @file
  4623. * @module UE.dom
  4624. * @class Range
  4625. * @since 1.2.6.1
  4626. */
  4627. /**
  4628. * dom操作封装
  4629. * @unfile
  4630. * @module UE.dom
  4631. */
  4632. /**
  4633. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4634. * @unfile
  4635. * @module UE.dom
  4636. * @class Range
  4637. */
  4638. ;(function() {
  4639. var guid = 0,
  4640. fillChar = domUtils.fillChar,
  4641. fillData
  4642. /**
  4643. * 更新range的collapse状态
  4644. * @param {Range} range range对象
  4645. */
  4646. function updateCollapse(range) {
  4647. range.collapsed =
  4648. range.startContainer &&
  4649. range.endContainer &&
  4650. range.startContainer === range.endContainer &&
  4651. range.startOffset == range.endOffset
  4652. }
  4653. function selectOneNode(rng) {
  4654. return (
  4655. !rng.collapsed &&
  4656. rng.startContainer.nodeType == 1 &&
  4657. rng.startContainer === rng.endContainer &&
  4658. rng.endOffset - rng.startOffset == 1
  4659. )
  4660. }
  4661. function setEndPoint(toStart, node, offset, range) {
  4662. //如果node是自闭合标签要处理
  4663. if (
  4664. node.nodeType == 1 &&
  4665. (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])
  4666. ) {
  4667. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1)
  4668. node = node.parentNode
  4669. }
  4670. if (toStart) {
  4671. range.startContainer = node
  4672. range.startOffset = offset
  4673. if (!range.endContainer) {
  4674. range.collapse(true)
  4675. }
  4676. } else {
  4677. range.endContainer = node
  4678. range.endOffset = offset
  4679. if (!range.startContainer) {
  4680. range.collapse(false)
  4681. }
  4682. }
  4683. updateCollapse(range)
  4684. return range
  4685. }
  4686. function execContentsAction(range, action) {
  4687. //调整边界
  4688. //range.includeBookmark();
  4689. var start = range.startContainer,
  4690. end = range.endContainer,
  4691. startOffset = range.startOffset,
  4692. endOffset = range.endOffset,
  4693. doc = range.document,
  4694. frag = doc.createDocumentFragment(),
  4695. tmpStart,
  4696. tmpEnd
  4697. if (start.nodeType == 1) {
  4698. start =
  4699. start.childNodes[startOffset] ||
  4700. (tmpStart = start.appendChild(doc.createTextNode('')))
  4701. }
  4702. if (end.nodeType == 1) {
  4703. end =
  4704. end.childNodes[endOffset] ||
  4705. (tmpEnd = end.appendChild(doc.createTextNode('')))
  4706. }
  4707. if (start === end && start.nodeType == 3) {
  4708. frag.appendChild(
  4709. doc.createTextNode(
  4710. start.substringData(startOffset, endOffset - startOffset)
  4711. )
  4712. )
  4713. //is not clone
  4714. if (action) {
  4715. start.deleteData(startOffset, endOffset - startOffset)
  4716. range.collapse(true)
  4717. }
  4718. return frag
  4719. }
  4720. var current,
  4721. currentLevel,
  4722. clone = frag,
  4723. startParents = domUtils.findParents(start, true),
  4724. endParents = domUtils.findParents(end, true)
  4725. for (var i = 0; startParents[i] == endParents[i]; ) {
  4726. i++
  4727. }
  4728. for (var j = i, si; (si = startParents[j]); j++) {
  4729. current = si.nextSibling
  4730. if (si == start) {
  4731. if (!tmpStart) {
  4732. if (range.startContainer.nodeType == 3) {
  4733. clone.appendChild(
  4734. doc.createTextNode(start.nodeValue.slice(startOffset))
  4735. )
  4736. //is not clone
  4737. if (action) {
  4738. start.deleteData(
  4739. startOffset,
  4740. start.nodeValue.length - startOffset
  4741. )
  4742. }
  4743. } else {
  4744. clone.appendChild(!action ? start.cloneNode(true) : start)
  4745. }
  4746. }
  4747. } else {
  4748. currentLevel = si.cloneNode(false)
  4749. clone.appendChild(currentLevel)
  4750. }
  4751. while (current) {
  4752. if (current === end || current === endParents[j]) {
  4753. break
  4754. }
  4755. si = current.nextSibling
  4756. clone.appendChild(!action ? current.cloneNode(true) : current)
  4757. current = si
  4758. }
  4759. clone = currentLevel
  4760. }
  4761. clone = frag
  4762. if (!startParents[i]) {
  4763. clone.appendChild(startParents[i - 1].cloneNode(false))
  4764. clone = clone.firstChild
  4765. }
  4766. for (var j = i, ei; (ei = endParents[j]); j++) {
  4767. current = ei.previousSibling
  4768. if (ei == end) {
  4769. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4770. clone.appendChild(
  4771. doc.createTextNode(end.substringData(0, endOffset))
  4772. )
  4773. //is not clone
  4774. if (action) {
  4775. end.deleteData(0, endOffset)
  4776. }
  4777. }
  4778. } else {
  4779. currentLevel = ei.cloneNode(false)
  4780. clone.appendChild(currentLevel)
  4781. }
  4782. //如果两端同级,右边第一次已经被开始做了
  4783. if (j != i || !startParents[i]) {
  4784. while (current) {
  4785. if (current === start) {
  4786. break
  4787. }
  4788. ei = current.previousSibling
  4789. clone.insertBefore(
  4790. !action ? current.cloneNode(true) : current,
  4791. clone.firstChild
  4792. )
  4793. current = ei
  4794. }
  4795. }
  4796. clone = currentLevel
  4797. }
  4798. if (action) {
  4799. range
  4800. .setStartBefore(
  4801. !endParents[i]
  4802. ? endParents[i - 1]
  4803. : !startParents[i]
  4804. ? startParents[i - 1]
  4805. : endParents[i]
  4806. )
  4807. .collapse(true)
  4808. }
  4809. tmpStart && domUtils.remove(tmpStart)
  4810. tmpEnd && domUtils.remove(tmpEnd)
  4811. return frag
  4812. }
  4813. /**
  4814. * 创建一个跟document绑定的空的Range实例
  4815. * @constructor
  4816. * @param { Document } document 新建的选区所属的文档对象
  4817. */
  4818. /**
  4819. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4820. */
  4821. /**
  4822. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4823. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4824. */
  4825. /**
  4826. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4827. */
  4828. /**
  4829. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4830. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4831. */
  4832. /**
  4833. * @property { Boolean } collapsed 当前Range是否闭合
  4834. * @default true
  4835. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4836. */
  4837. /**
  4838. * @property { Document } document 当前Range所属的Document对象
  4839. * @remind 不同range的的document属性可以是不同的
  4840. */
  4841. var Range = (dom.Range = function(document) {
  4842. var me = this
  4843. me.startContainer = me.startOffset = me.endContainer = me.endOffset = null
  4844. me.document = document
  4845. me.collapsed = true
  4846. })
  4847. /**
  4848. * 删除fillData
  4849. * @param doc
  4850. * @param excludeNode
  4851. */
  4852. function removeFillData(doc, excludeNode) {
  4853. try {
  4854. if (fillData && domUtils.inDoc(fillData, doc)) {
  4855. if (!fillData.nodeValue.replace(fillCharReg, '').length) {
  4856. var tmpNode = fillData.parentNode
  4857. domUtils.remove(fillData)
  4858. while (
  4859. tmpNode &&
  4860. domUtils.isEmptyInlineElement(tmpNode) &&
  4861. //safari的contains有bug
  4862. (browser.safari
  4863. ? !(
  4864. domUtils.getPosition(tmpNode, excludeNode) &
  4865. domUtils.POSITION_CONTAINS
  4866. )
  4867. : !tmpNode.contains(excludeNode))
  4868. ) {
  4869. fillData = tmpNode.parentNode
  4870. domUtils.remove(tmpNode)
  4871. tmpNode = fillData
  4872. }
  4873. } else {
  4874. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, '')
  4875. }
  4876. }
  4877. } catch (e) {}
  4878. }
  4879. /**
  4880. * @param node
  4881. * @param dir
  4882. */
  4883. function mergeSibling(node, dir) {
  4884. var tmpNode
  4885. node = node[dir]
  4886. while (node && domUtils.isFillChar(node)) {
  4887. tmpNode = node[dir]
  4888. domUtils.remove(node)
  4889. node = tmpNode
  4890. }
  4891. }
  4892. Range.prototype = {
  4893. /**
  4894. * 克隆选区的内容到一个DocumentFragment里
  4895. * @method cloneContents
  4896. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4897. * @example
  4898. * ```html
  4899. * <body>
  4900. * <!-- 中括号表示选区 -->
  4901. * <b>x<i>x[x</i>xx]x</b>
  4902. *
  4903. * <script>
  4904. * //range是已选中的选区
  4905. * var fragment = range.cloneContents(),
  4906. * node = document.createElement("div");
  4907. *
  4908. * node.appendChild( fragment );
  4909. *
  4910. * //output: <i>x</i>xx
  4911. * console.log( node.innerHTML );
  4912. *
  4913. * </script>
  4914. * </body>
  4915. * ```
  4916. */
  4917. cloneContents: function() {
  4918. return this.collapsed ? null : execContentsAction(this, 0)
  4919. },
  4920. /**
  4921. * 删除当前选区范围中的所有内容
  4922. * @method deleteContents
  4923. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4924. * @return { UE.dom.Range } 当前操作的Range对象
  4925. * @example
  4926. * ```html
  4927. * <body>
  4928. * <!-- 中括号表示选区 -->
  4929. * <b>x<i>x[x</i>xx]x</b>
  4930. *
  4931. * <script>
  4932. * //range是已选中的选区
  4933. * range.deleteContents();
  4934. *
  4935. * //竖线表示闭合后的选区位置
  4936. * //output: <b>x<i>x</i>|x</b>
  4937. * console.log( document.body.innerHTML );
  4938. *
  4939. * //此时, range的各项属性为
  4940. * //output: B
  4941. * console.log( range.startContainer.tagName );
  4942. * //output: 2
  4943. * console.log( range.startOffset );
  4944. * //output: B
  4945. * console.log( range.endContainer.tagName );
  4946. * //output: 2
  4947. * console.log( range.endOffset );
  4948. * //output: true
  4949. * console.log( range.collapsed );
  4950. *
  4951. * </script>
  4952. * </body>
  4953. * ```
  4954. */
  4955. deleteContents: function() {
  4956. var txt
  4957. if (!this.collapsed) {
  4958. execContentsAction(this, 1)
  4959. }
  4960. if (browser.webkit) {
  4961. txt = this.startContainer
  4962. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4963. this.setStartBefore(txt).collapse(true)
  4964. domUtils.remove(txt)
  4965. }
  4966. }
  4967. return this
  4968. },
  4969. /**
  4970. * 将当前选区的内容提取到一个DocumentFragment里
  4971. * @method extractContents
  4972. * @remind 执行该操作后, 选区将变成闭合状态
  4973. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4974. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4975. * @example
  4976. * ```html
  4977. * <body>
  4978. * <!-- 中括号表示选区 -->
  4979. * <b>x<i>x[x</i>xx]x</b>
  4980. *
  4981. * <script>
  4982. * //range是已选中的选区
  4983. * var fragment = range.extractContents(),
  4984. * node = document.createElement( "div" );
  4985. *
  4986. * node.appendChild( fragment );
  4987. *
  4988. * //竖线表示闭合后的选区位置
  4989. *
  4990. * //output: <b>x<i>x</i>|x</b>
  4991. * console.log( document.body.innerHTML );
  4992. * //output: <i>x</i>xx
  4993. * console.log( node.innerHTML );
  4994. *
  4995. * //此时, range的各项属性为
  4996. * //output: B
  4997. * console.log( range.startContainer.tagName );
  4998. * //output: 2
  4999. * console.log( range.startOffset );
  5000. * //output: B
  5001. * console.log( range.endContainer.tagName );
  5002. * //output: 2
  5003. * console.log( range.endOffset );
  5004. * //output: true
  5005. * console.log( range.collapsed );
  5006. *
  5007. * </script>
  5008. * </body>
  5009. */
  5010. extractContents: function() {
  5011. return this.collapsed ? null : execContentsAction(this, 2)
  5012. },
  5013. /**
  5014. * 设置Range的开始容器节点和偏移量
  5015. * @method setStart
  5016. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  5017. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  5018. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  5019. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  5020. * 中的索引
  5021. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  5022. * @param { int } offset 选区的开始位置偏移量
  5023. * @return { UE.dom.Range } 当前range对象
  5024. * @example
  5025. * ```html
  5026. * <!-- 选区 -->
  5027. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  5028. *
  5029. * <script>
  5030. *
  5031. * //执行操作
  5032. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  5033. *
  5034. * //此时, 选区变成了
  5035. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  5036. *
  5037. * </script>
  5038. * ```
  5039. * @example
  5040. * ```html
  5041. * <!-- 选区 -->
  5042. * <b>xxx<img>[xx]x</b>
  5043. *
  5044. * <script>
  5045. *
  5046. * //执行操作
  5047. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  5048. *
  5049. * //此时, 选区变成了
  5050. * //<b>xxx[<img>xx]x</b>
  5051. *
  5052. * </script>
  5053. * ```
  5054. */
  5055. setStart: function(node, offset) {
  5056. return setEndPoint(true, node, offset, this)
  5057. },
  5058. /**
  5059. * 设置Range的结束容器和偏移量
  5060. * @method setEnd
  5061. * @param { Node } node 作为当前选区结束边界容器的节点对象
  5062. * @param { int } offset 结束边界的偏移量
  5063. * @see UE.dom.Range:setStart(Node,int)
  5064. * @return { UE.dom.Range } 当前range对象
  5065. */
  5066. setEnd: function(node, offset) {
  5067. return setEndPoint(false, node, offset, this)
  5068. },
  5069. /**
  5070. * 将Range开始位置设置到node节点之后
  5071. * @method setStartAfter
  5072. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  5073. * @param { Node } node 选区的开始边界将紧接着该节点之后
  5074. * @return { UE.dom.Range } 当前range对象
  5075. * @example
  5076. * ```html
  5077. * <!-- 选区示例 -->
  5078. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  5079. *
  5080. * <script>
  5081. *
  5082. * //执行操作
  5083. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  5084. *
  5085. * //结果选区
  5086. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  5087. *
  5088. * </script>
  5089. * ```
  5090. */
  5091. setStartAfter: function(node) {
  5092. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1)
  5093. },
  5094. /**
  5095. * 将Range开始位置设置到node节点之前
  5096. * @method setStartBefore
  5097. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  5098. * @param { Node } node 新的选区开始位置在该节点之前
  5099. * @see UE.dom.Range:setStartAfter(Node)
  5100. * @return { UE.dom.Range } 当前range对象
  5101. */
  5102. setStartBefore: function(node) {
  5103. return this.setStart(node.parentNode, domUtils.getNodeIndex(node))
  5104. },
  5105. /**
  5106. * 将Range结束位置设置到node节点之后
  5107. * @method setEndAfter
  5108. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  5109. * @param { Node } node 目标节点
  5110. * @see UE.dom.Range:setStartAfter(Node)
  5111. * @return { UE.dom.Range } 当前range对象
  5112. * @example
  5113. * ```html
  5114. * <!-- 选区示例 -->
  5115. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  5116. *
  5117. * <script>
  5118. *
  5119. * //执行操作
  5120. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  5121. *
  5122. * //结果选区
  5123. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  5124. *
  5125. * </script>
  5126. * ```
  5127. */
  5128. setEndAfter: function(node) {
  5129. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1)
  5130. },
  5131. /**
  5132. * 将Range结束位置设置到node节点之前
  5133. * @method setEndBefore
  5134. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  5135. * @param { Node } node 目标节点
  5136. * @see UE.dom.Range:setEndAfter(Node)
  5137. * @return { UE.dom.Range } 当前range对象
  5138. */
  5139. setEndBefore: function(node) {
  5140. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node))
  5141. },
  5142. /**
  5143. * 设置Range的开始位置到node节点内的第一个子节点之前
  5144. * @method setStartAtFirst
  5145. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  5146. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5147. * @param { Node } node 目标节点
  5148. * @see UE.dom.Range:setStartBefore(Node)
  5149. * @return { UE.dom.Range } 当前range对象
  5150. * @example
  5151. * ```html
  5152. * <!-- 选区示例 -->
  5153. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5154. *
  5155. * <script>
  5156. *
  5157. * //执行操作
  5158. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  5159. *
  5160. * //结果选区
  5161. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  5162. *
  5163. * </script>
  5164. * ```
  5165. */
  5166. setStartAtFirst: function(node) {
  5167. return this.setStart(node, 0)
  5168. },
  5169. /**
  5170. * 设置Range的开始位置到node节点内的最后一个节点之后
  5171. * @method setStartAtLast
  5172. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  5173. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5174. * @param { Node } node 目标节点
  5175. * @see UE.dom.Range:setStartAtFirst(Node)
  5176. * @return { UE.dom.Range } 当前range对象
  5177. */
  5178. setStartAtLast: function(node) {
  5179. return this.setStart(
  5180. node,
  5181. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5182. )
  5183. },
  5184. /**
  5185. * 设置Range的结束位置到node节点内的第一个节点之前
  5186. * @method setEndAtFirst
  5187. * @param { Node } node 目标节点
  5188. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  5189. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5190. * @see UE.dom.Range:setStartAtFirst(Node)
  5191. * @return { UE.dom.Range } 当前range对象
  5192. */
  5193. setEndAtFirst: function(node) {
  5194. return this.setEnd(node, 0)
  5195. },
  5196. /**
  5197. * 设置Range的结束位置到node节点内的最后一个节点之后
  5198. * @method setEndAtLast
  5199. * @param { Node } node 目标节点
  5200. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  5201. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5202. * @see UE.dom.Range:setStartAtFirst(Node)
  5203. * @return { UE.dom.Range } 当前range对象
  5204. */
  5205. setEndAtLast: function(node) {
  5206. return this.setEnd(
  5207. node,
  5208. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5209. )
  5210. },
  5211. /**
  5212. * 选中给定节点
  5213. * @method selectNode
  5214. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5215. * 而endOffset为startOffset+1
  5216. * @param { Node } node 需要选中的节点
  5217. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5218. * @example
  5219. * ```html
  5220. * <!-- 选区示例 -->
  5221. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5222. *
  5223. * <script>
  5224. *
  5225. * //执行操作
  5226. * range.selectNode( document.getElementsByTagName("i")[0] );
  5227. *
  5228. * //结果选区
  5229. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5230. *
  5231. * </script>
  5232. * ```
  5233. */
  5234. selectNode: function(node) {
  5235. return this.setStartBefore(node).setEndAfter(node)
  5236. },
  5237. /**
  5238. * 选中给定节点内部的所有节点
  5239. * @method selectNodeContents
  5240. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5241. * 而endOffset是该节点的子节点数。
  5242. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5243. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5244. * @example
  5245. * ```html
  5246. * <!-- 选区示例 -->
  5247. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5248. *
  5249. * <script>
  5250. *
  5251. * //执行操作
  5252. * range.selectNode( document.getElementsByTagName("b")[0] );
  5253. *
  5254. * //结果选区
  5255. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5256. *
  5257. * </script>
  5258. * ```
  5259. */
  5260. selectNodeContents: function(node) {
  5261. return this.setStart(node, 0).setEndAtLast(node)
  5262. },
  5263. /**
  5264. * clone当前Range对象
  5265. * @method cloneRange
  5266. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5267. * @return { UE.dom.Range } 当前range对象的一个副本
  5268. */
  5269. cloneRange: function() {
  5270. var me = this
  5271. return new Range(me.document)
  5272. .setStart(me.startContainer, me.startOffset)
  5273. .setEnd(me.endContainer, me.endOffset)
  5274. },
  5275. /**
  5276. * 向当前选区的结束处闭合选区
  5277. * @method collapse
  5278. * @return { UE.dom.Range } 当前range对象
  5279. * @example
  5280. * ```html
  5281. * <!-- 选区示例 -->
  5282. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5283. *
  5284. * <script>
  5285. *
  5286. * //执行操作
  5287. * range.collapse();
  5288. *
  5289. * //结果选区
  5290. * //“|”表示选区已闭合
  5291. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5292. *
  5293. * </script>
  5294. * ```
  5295. */
  5296. /**
  5297. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5298. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5299. * @method collapse
  5300. * @param { Boolean } toStart 是否向选区开始处闭合
  5301. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5302. * @see UE.dom.Range:collapse()
  5303. * @example
  5304. * ```html
  5305. * <!-- 选区示例 -->
  5306. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5307. *
  5308. * <script>
  5309. *
  5310. * //执行操作
  5311. * range.collapse( true );
  5312. *
  5313. * //结果选区
  5314. * //“|”表示选区已闭合
  5315. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5316. *
  5317. * </script>
  5318. * ```
  5319. */
  5320. collapse: function(toStart) {
  5321. var me = this
  5322. if (toStart) {
  5323. me.endContainer = me.startContainer
  5324. me.endOffset = me.startOffset
  5325. } else {
  5326. me.startContainer = me.endContainer
  5327. me.startOffset = me.endOffset
  5328. }
  5329. me.collapsed = true
  5330. return me
  5331. },
  5332. /**
  5333. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5334. * @method shrinkBoundary
  5335. * @return { UE.dom.Range } 当前range对象
  5336. * @example
  5337. * ```html
  5338. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5339. * ```
  5340. *
  5341. * @example
  5342. * ```html
  5343. * <!-- 选区示例 -->
  5344. * <b>x[xx</b><i>]xxx</i>
  5345. *
  5346. * <script>
  5347. *
  5348. * //执行收缩
  5349. * range.shrinkBoundary();
  5350. *
  5351. * //结果选区
  5352. * //<b>x[xx]</b><i>xxx</i>
  5353. * </script>
  5354. * ```
  5355. *
  5356. * @example
  5357. * ```html
  5358. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5359. * ```
  5360. */
  5361. /**
  5362. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5363. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5364. * @method shrinkBoundary
  5365. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5366. * @return { UE.dom.Range } 当前range对象
  5367. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5368. */
  5369. shrinkBoundary: function(ignoreEnd) {
  5370. var me = this,
  5371. child,
  5372. collapsed = me.collapsed
  5373. function check(node) {
  5374. return (
  5375. node.nodeType == 1 &&
  5376. !domUtils.isBookmarkNode(node) &&
  5377. !dtd.$empty[node.tagName] &&
  5378. !dtd.$nonChild[node.tagName]
  5379. )
  5380. }
  5381. while (
  5382. me.startContainer.nodeType == 1 && //是element
  5383. (child = me.startContainer.childNodes[me.startOffset]) && //子节点也是element
  5384. check(child)
  5385. ) {
  5386. me.setStart(child, 0)
  5387. }
  5388. if (collapsed) {
  5389. return me.collapse(true)
  5390. }
  5391. if (!ignoreEnd) {
  5392. while (
  5393. me.endContainer.nodeType == 1 && //是element
  5394. me.endOffset > 0 && //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5395. (child = me.endContainer.childNodes[me.endOffset - 1]) && //子节点也是element
  5396. check(child)
  5397. ) {
  5398. me.setEnd(child, child.childNodes.length)
  5399. }
  5400. }
  5401. return me
  5402. },
  5403. /**
  5404. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5405. * @method getCommonAncestor
  5406. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5407. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5408. * @example
  5409. * ```html
  5410. * //选区示例
  5411. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5412. * <script>
  5413. *
  5414. * var node = range.getCommonAncestor();
  5415. *
  5416. * //公共祖先节点是: b节点
  5417. * //输出: B
  5418. * console.log(node.tagName);
  5419. *
  5420. * </script>
  5421. * ```
  5422. */
  5423. /**
  5424. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5425. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5426. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5427. * @method getCommonAncestor
  5428. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5429. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5430. * @see UE.dom.Range:getCommonAncestor()
  5431. * @example
  5432. * ```html
  5433. * <body>
  5434. *
  5435. * <!-- 选区示例 -->
  5436. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5437. *
  5438. * <script>
  5439. *
  5440. * var node = range.getCommonAncestor( false );
  5441. *
  5442. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5443. * //output: B
  5444. * console.log( node.tagName );
  5445. *
  5446. * </script>
  5447. *
  5448. * </body>
  5449. * ```
  5450. */
  5451. /**
  5452. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5453. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5454. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5455. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5456. * @method getCommonAncestor
  5457. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5458. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5459. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5460. * @see UE.dom.Range:getCommonAncestor()
  5461. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5462. * @example
  5463. * ```html
  5464. * <body>
  5465. *
  5466. * <!-- 选区示例 -->
  5467. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5468. *
  5469. * <script>
  5470. *
  5471. * var node = range.getCommonAncestor( true, false );
  5472. *
  5473. * //output: SPAN
  5474. * console.log( node.tagName );
  5475. *
  5476. * </script>
  5477. *
  5478. * </body>
  5479. * ```
  5480. */
  5481. getCommonAncestor: function(includeSelf, ignoreTextNode) {
  5482. var me = this,
  5483. start = me.startContainer,
  5484. end = me.endContainer
  5485. if (start === end) {
  5486. if (includeSelf && selectOneNode(this)) {
  5487. start = start.childNodes[me.startOffset]
  5488. if (start.nodeType == 1) return start
  5489. }
  5490. //只有在上来就相等的情况下才会出现是文本的情况
  5491. return ignoreTextNode && start.nodeType == 3
  5492. ? start.parentNode
  5493. : start
  5494. }
  5495. return domUtils.getCommonAncestor(start, end)
  5496. },
  5497. /**
  5498. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5499. * @method trimBoundary
  5500. * @remind 该操作有可能会引起文本节点被切开
  5501. * @return { UE.dom.Range } 当前range对象
  5502. * @example
  5503. * ```html
  5504. *
  5505. * //选区示例
  5506. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5507. *
  5508. * <script>
  5509. * //未调整前, 选区的开始容器和结束都是文本节点
  5510. * //执行调整
  5511. * range.trimBoundary();
  5512. *
  5513. * //调整之后, 容器节点变成了i节点
  5514. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5515. * </script>
  5516. * ```
  5517. */
  5518. /**
  5519. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5520. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5521. * @method trimBoundary
  5522. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5523. * @return { UE.dom.Range } 当前range对象
  5524. * @example
  5525. * ```html
  5526. *
  5527. * //选区示例
  5528. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5529. *
  5530. * <script>
  5531. * //未调整前, 选区的开始容器和结束都是文本节点
  5532. * //执行调整
  5533. * range.trimBoundary( true );
  5534. *
  5535. * //调整之后, 开始容器节点变成了i节点
  5536. * //但是, 结束容器没有发生变化
  5537. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5538. * </script>
  5539. * ```
  5540. */
  5541. trimBoundary: function(ignoreEnd) {
  5542. this.txtToElmBoundary()
  5543. var start = this.startContainer,
  5544. offset = this.startOffset,
  5545. collapsed = this.collapsed,
  5546. end = this.endContainer
  5547. if (start.nodeType == 3) {
  5548. if (offset == 0) {
  5549. this.setStartBefore(start)
  5550. } else {
  5551. if (offset >= start.nodeValue.length) {
  5552. this.setStartAfter(start)
  5553. } else {
  5554. var textNode = domUtils.split(start, offset)
  5555. //跟新结束边界
  5556. if (start === end) {
  5557. this.setEnd(textNode, this.endOffset - offset)
  5558. } else if (start.parentNode === end) {
  5559. this.endOffset += 1
  5560. }
  5561. this.setStartBefore(textNode)
  5562. }
  5563. }
  5564. if (collapsed) {
  5565. return this.collapse(true)
  5566. }
  5567. }
  5568. if (!ignoreEnd) {
  5569. offset = this.endOffset
  5570. end = this.endContainer
  5571. if (end.nodeType == 3) {
  5572. if (offset == 0) {
  5573. this.setEndBefore(end)
  5574. } else {
  5575. offset < end.nodeValue.length && domUtils.split(end, offset)
  5576. this.setEndAfter(end)
  5577. }
  5578. }
  5579. }
  5580. return this
  5581. },
  5582. /**
  5583. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5584. * @method txtToElmBoundary
  5585. * @remind 该操作不会修改dom节点
  5586. * @return { UE.dom.Range } 当前range对象
  5587. */
  5588. /**
  5589. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5590. * ignoreCollapsed 的值决定是否执行该调整
  5591. * @method txtToElmBoundary
  5592. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5593. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5594. * @return { UE.dom.Range } 当前range对象
  5595. */
  5596. txtToElmBoundary: function(ignoreCollapsed) {
  5597. function adjust(r, c) {
  5598. var container = r[c + 'Container'],
  5599. offset = r[c + 'Offset']
  5600. if (container.nodeType == 3) {
  5601. if (!offset) {
  5602. r[
  5603. 'set' +
  5604. c.replace(/(\w)/, function(a) {
  5605. return a.toUpperCase()
  5606. }) +
  5607. 'Before'
  5608. ](container)
  5609. } else if (offset >= container.nodeValue.length) {
  5610. r[
  5611. 'set' +
  5612. c.replace(/(\w)/, function(a) {
  5613. return a.toUpperCase()
  5614. }) +
  5615. 'After'
  5616. ](container)
  5617. }
  5618. }
  5619. }
  5620. if (ignoreCollapsed || !this.collapsed) {
  5621. adjust(this, 'start')
  5622. adjust(this, 'end')
  5623. }
  5624. return this
  5625. },
  5626. /**
  5627. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5628. * @method insertNode
  5629. * @param { Node } node 需要插入的节点
  5630. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5631. * @return { UE.dom.Range } 当前range对象
  5632. */
  5633. insertNode: function(node) {
  5634. var first = node,
  5635. length = 1
  5636. if (node.nodeType == 11) {
  5637. first = node.firstChild
  5638. length = node.childNodes.length
  5639. }
  5640. this.trimBoundary(true)
  5641. var start = this.startContainer,
  5642. offset = this.startOffset
  5643. var nextNode = start.childNodes[offset]
  5644. if (nextNode) {
  5645. start.insertBefore(node, nextNode)
  5646. } else {
  5647. start.appendChild(node)
  5648. }
  5649. if (first.parentNode === this.endContainer) {
  5650. this.endOffset = this.endOffset + length
  5651. }
  5652. return this.setStartBefore(first)
  5653. },
  5654. /**
  5655. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5656. * @method setCursor
  5657. * @return { UE.dom.Range } 当前range对象
  5658. * @see UE.dom.Range:collapse()
  5659. */
  5660. /**
  5661. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5662. * @method setCursor
  5663. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5664. * 反之,则向开始容器方向闭合
  5665. * @return { UE.dom.Range } 当前range对象
  5666. * @see UE.dom.Range:collapse(Boolean)
  5667. */
  5668. setCursor: function(toEnd, noFillData) {
  5669. return this.collapse(!toEnd).select(noFillData)
  5670. },
  5671. /**
  5672. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5673. * @method createBookmark
  5674. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5675. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5676. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5677. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5678. * 返回的记录的类型为ID, 反之则为引用
  5679. */
  5680. createBookmark: function(serialize, same) {
  5681. var endNode,
  5682. startNode = this.document.createElement('span')
  5683. startNode.style.cssText = 'display:none;line-height:0px;'
  5684. startNode.appendChild(this.document.createTextNode('\u200D'))
  5685. startNode.id = '_baidu_bookmark_start_' + (same ? '' : guid++)
  5686. if (!this.collapsed) {
  5687. endNode = startNode.cloneNode(true)
  5688. endNode.id = '_baidu_bookmark_end_' + (same ? '' : guid++)
  5689. }
  5690. this.insertNode(startNode)
  5691. if (endNode) {
  5692. this.collapse()
  5693. .insertNode(endNode)
  5694. .setEndBefore(endNode)
  5695. }
  5696. this.setStartAfter(startNode)
  5697. return {
  5698. start: serialize ? startNode.id : startNode,
  5699. end: endNode ? (serialize ? endNode.id : endNode) : null,
  5700. id: serialize
  5701. }
  5702. },
  5703. /**
  5704. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5705. * @method moveToBookmark
  5706. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5707. * @return { UE.dom.Range } 当前range对象
  5708. * @see UE.dom.Range:createBookmark(Boolean)
  5709. */
  5710. moveToBookmark: function(bookmark) {
  5711. var start = bookmark.id
  5712. ? this.document.getElementById(bookmark.start)
  5713. : bookmark.start,
  5714. end =
  5715. bookmark.end && bookmark.id
  5716. ? this.document.getElementById(bookmark.end)
  5717. : bookmark.end
  5718. this.setStartBefore(start)
  5719. domUtils.remove(start)
  5720. if (end) {
  5721. this.setEndBefore(end)
  5722. domUtils.remove(end)
  5723. } else {
  5724. this.collapse(true)
  5725. }
  5726. return this
  5727. },
  5728. /**
  5729. * 调整range的边界,使其"放大"到最近的父节点
  5730. * @method enlarge
  5731. * @remind 会引起选区的变化
  5732. * @return { UE.dom.Range } 当前range对象
  5733. */
  5734. /**
  5735. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5736. * 要求扩大之后的父节点是block节点
  5737. * @method enlarge
  5738. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5739. * @return { UE.dom.Range } 当前range对象
  5740. */
  5741. enlarge: function(toBlock, stopFn) {
  5742. var isBody = domUtils.isBody,
  5743. pre,
  5744. node,
  5745. tmp = this.document.createTextNode('')
  5746. if (toBlock) {
  5747. node = this.startContainer
  5748. if (node.nodeType == 1) {
  5749. if (node.childNodes[this.startOffset]) {
  5750. pre = node = node.childNodes[this.startOffset]
  5751. } else {
  5752. node.appendChild(tmp)
  5753. pre = node = tmp
  5754. }
  5755. } else {
  5756. pre = node
  5757. }
  5758. while (1) {
  5759. if (domUtils.isBlockElm(node)) {
  5760. node = pre
  5761. while (
  5762. (pre = node.previousSibling) &&
  5763. !domUtils.isBlockElm(pre)
  5764. ) {
  5765. node = pre
  5766. }
  5767. this.setStartBefore(node)
  5768. break
  5769. }
  5770. pre = node
  5771. node = node.parentNode
  5772. }
  5773. node = this.endContainer
  5774. if (node.nodeType == 1) {
  5775. if ((pre = node.childNodes[this.endOffset])) {
  5776. node.insertBefore(tmp, pre)
  5777. } else {
  5778. node.appendChild(tmp)
  5779. }
  5780. pre = node = tmp
  5781. } else {
  5782. pre = node
  5783. }
  5784. while (1) {
  5785. if (domUtils.isBlockElm(node)) {
  5786. node = pre
  5787. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5788. node = pre
  5789. }
  5790. this.setEndAfter(node)
  5791. break
  5792. }
  5793. pre = node
  5794. node = node.parentNode
  5795. }
  5796. if (tmp.parentNode === this.endContainer) {
  5797. this.endOffset--
  5798. }
  5799. domUtils.remove(tmp)
  5800. }
  5801. // 扩展边界到最大
  5802. if (!this.collapsed) {
  5803. while (this.startOffset == 0) {
  5804. if (stopFn && stopFn(this.startContainer)) {
  5805. break
  5806. }
  5807. if (isBody(this.startContainer)) {
  5808. break
  5809. }
  5810. this.setStartBefore(this.startContainer)
  5811. }
  5812. while (
  5813. this.endOffset ==
  5814. (this.endContainer.nodeType == 1
  5815. ? this.endContainer.childNodes.length
  5816. : this.endContainer.nodeValue.length)
  5817. ) {
  5818. if (stopFn && stopFn(this.endContainer)) {
  5819. break
  5820. }
  5821. if (isBody(this.endContainer)) {
  5822. break
  5823. }
  5824. this.setEndAfter(this.endContainer)
  5825. }
  5826. }
  5827. return this
  5828. },
  5829. enlargeToBlockElm: function(ignoreEnd) {
  5830. while (!domUtils.isBlockElm(this.startContainer)) {
  5831. this.setStartBefore(this.startContainer)
  5832. }
  5833. if (!ignoreEnd) {
  5834. while (!domUtils.isBlockElm(this.endContainer)) {
  5835. this.setEndAfter(this.endContainer)
  5836. }
  5837. }
  5838. return this
  5839. },
  5840. /**
  5841. * 调整Range的边界,使其"缩小"到最合适的位置
  5842. * @method adjustmentBoundary
  5843. * @return { UE.dom.Range } 当前range对象
  5844. * @see UE.dom.Range:shrinkBoundary()
  5845. */
  5846. adjustmentBoundary: function() {
  5847. if (!this.collapsed) {
  5848. while (
  5849. !domUtils.isBody(this.startContainer) &&
  5850. this.startOffset ==
  5851. this.startContainer[
  5852. this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  5853. ].length &&
  5854. this.startContainer[
  5855. this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  5856. ].length
  5857. ) {
  5858. this.setStartAfter(this.startContainer)
  5859. }
  5860. while (
  5861. !domUtils.isBody(this.endContainer) &&
  5862. !this.endOffset &&
  5863. this.endContainer[
  5864. this.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  5865. ].length
  5866. ) {
  5867. this.setEndBefore(this.endContainer)
  5868. }
  5869. }
  5870. return this
  5871. },
  5872. /**
  5873. * 给range选区中的内容添加给定的inline标签
  5874. * @method applyInlineStyle
  5875. * @param { String } tagName 需要添加的标签名
  5876. * @example
  5877. * ```html
  5878. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5879. * ```
  5880. */
  5881. /**
  5882. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5883. * @method applyInlineStyle
  5884. * @param { String } tagName 需要添加的标签名
  5885. * @param { Object } attrs 跟随新添加的标签的属性
  5886. * @return { UE.dom.Range } 当前选区
  5887. * @example
  5888. * ```html
  5889. * <p>xxxx[xxxx]x</p>
  5890. *
  5891. * ==>
  5892. *
  5893. * <!-- 执行操作 -->
  5894. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5895. *
  5896. * ==>
  5897. *
  5898. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5899. * ```
  5900. */
  5901. applyInlineStyle: function(tagName, attrs, list) {
  5902. if (this.collapsed) return this
  5903. this.trimBoundary()
  5904. .enlarge(false, function(node) {
  5905. return node.nodeType == 1 && domUtils.isBlockElm(node)
  5906. })
  5907. .adjustmentBoundary()
  5908. var bookmark = this.createBookmark(),
  5909. end = bookmark.end,
  5910. filterFn = function(node) {
  5911. return node.nodeType == 1
  5912. ? node.tagName.toLowerCase() != 'br'
  5913. : !domUtils.isWhitespace(node)
  5914. },
  5915. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5916. node,
  5917. pre,
  5918. range = this.cloneRange()
  5919. while (
  5920. current &&
  5921. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  5922. ) {
  5923. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5924. range.setStartBefore(current)
  5925. node = current
  5926. while (
  5927. node &&
  5928. (node.nodeType == 3 || dtd[tagName][node.tagName]) &&
  5929. node !== end
  5930. ) {
  5931. pre = node
  5932. node = domUtils.getNextDomNode(
  5933. node,
  5934. node.nodeType == 1,
  5935. null,
  5936. function(parent) {
  5937. return dtd[tagName][parent.tagName]
  5938. }
  5939. )
  5940. }
  5941. var frag = range.setEndAfter(pre).extractContents(),
  5942. elm
  5943. if (list && list.length > 0) {
  5944. var level, top
  5945. top = level = list[0].cloneNode(false)
  5946. for (var i = 1, ci; (ci = list[i++]); ) {
  5947. level.appendChild(ci.cloneNode(false))
  5948. level = level.firstChild
  5949. }
  5950. elm = level
  5951. } else {
  5952. elm = range.document.createElement(tagName)
  5953. }
  5954. if (attrs) {
  5955. domUtils.setAttributes(elm, attrs)
  5956. }
  5957. elm.appendChild(frag)
  5958. range.insertNode(list ? top : elm)
  5959. //处理下滑线在a上的情况
  5960. var aNode
  5961. if (
  5962. tagName == 'span' &&
  5963. attrs.style &&
  5964. /text\-decoration/.test(attrs.style) &&
  5965. (aNode = domUtils.findParentByTagName(elm, 'a', true))
  5966. ) {
  5967. domUtils.setAttributes(aNode, attrs)
  5968. domUtils.remove(elm, true)
  5969. elm = aNode
  5970. } else {
  5971. domUtils.mergeSibling(elm)
  5972. domUtils.clearEmptySibling(elm)
  5973. }
  5974. //去除子节点相同的
  5975. domUtils.mergeChild(elm, attrs)
  5976. current = domUtils.getNextDomNode(elm, false, filterFn)
  5977. domUtils.mergeToParent(elm)
  5978. if (node === end) {
  5979. break
  5980. }
  5981. } else {
  5982. current = domUtils.getNextDomNode(current, true, filterFn)
  5983. }
  5984. }
  5985. return this.moveToBookmark(bookmark)
  5986. },
  5987. /**
  5988. * 移除当前选区内指定的inline标签,但保留其中的内容
  5989. * @method removeInlineStyle
  5990. * @param { String } tagName 需要移除的标签名
  5991. * @return { UE.dom.Range } 当前的range对象
  5992. * @example
  5993. * ```html
  5994. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5995. * ```
  5996. */
  5997. /**
  5998. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5999. * @method removeInlineStyle
  6000. * @param { Array } tagNameArr 需要移除的标签名的数组
  6001. * @return { UE.dom.Range } 当前的range对象
  6002. * @see UE.dom.Range:removeInlineStyle(String)
  6003. */
  6004. removeInlineStyle: function(tagNames) {
  6005. if (this.collapsed) return this
  6006. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames]
  6007. this.shrinkBoundary().adjustmentBoundary()
  6008. var start = this.startContainer,
  6009. end = this.endContainer
  6010. while (1) {
  6011. if (start.nodeType == 1) {
  6012. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  6013. break
  6014. }
  6015. if (start.tagName.toLowerCase() == 'body') {
  6016. start = null
  6017. break
  6018. }
  6019. }
  6020. start = start.parentNode
  6021. }
  6022. while (1) {
  6023. if (end.nodeType == 1) {
  6024. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  6025. break
  6026. }
  6027. if (end.tagName.toLowerCase() == 'body') {
  6028. end = null
  6029. break
  6030. }
  6031. }
  6032. end = end.parentNode
  6033. }
  6034. var bookmark = this.createBookmark(),
  6035. frag,
  6036. tmpRange
  6037. if (start) {
  6038. tmpRange = this.cloneRange()
  6039. .setEndBefore(bookmark.start)
  6040. .setStartBefore(start)
  6041. frag = tmpRange.extractContents()
  6042. tmpRange.insertNode(frag)
  6043. domUtils.clearEmptySibling(start, true)
  6044. start.parentNode.insertBefore(bookmark.start, start)
  6045. }
  6046. if (end) {
  6047. tmpRange = this.cloneRange()
  6048. .setStartAfter(bookmark.end)
  6049. .setEndAfter(end)
  6050. frag = tmpRange.extractContents()
  6051. tmpRange.insertNode(frag)
  6052. domUtils.clearEmptySibling(end, false, true)
  6053. end.parentNode.insertBefore(bookmark.end, end.nextSibling)
  6054. }
  6055. var current = domUtils.getNextDomNode(bookmark.start, false, function(
  6056. node
  6057. ) {
  6058. return node.nodeType == 1
  6059. }),
  6060. next
  6061. while (current && current !== bookmark.end) {
  6062. next = domUtils.getNextDomNode(current, true, function(node) {
  6063. return node.nodeType == 1
  6064. })
  6065. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  6066. domUtils.remove(current, true)
  6067. }
  6068. current = next
  6069. }
  6070. return this.moveToBookmark(bookmark)
  6071. },
  6072. /**
  6073. * 获取当前选中的自闭合的节点
  6074. * @method getClosedNode
  6075. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  6076. */
  6077. getClosedNode: function() {
  6078. var node
  6079. if (!this.collapsed) {
  6080. var range = this.cloneRange()
  6081. .adjustmentBoundary()
  6082. .shrinkBoundary()
  6083. if (selectOneNode(range)) {
  6084. var child = range.startContainer.childNodes[range.startOffset]
  6085. if (
  6086. child &&
  6087. child.nodeType == 1 &&
  6088. (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])
  6089. ) {
  6090. node = child
  6091. }
  6092. }
  6093. }
  6094. return node
  6095. },
  6096. /**
  6097. * 在页面上高亮range所表示的选区
  6098. * @method select
  6099. * @return { UE.dom.Range } 返回当前Range对象
  6100. */
  6101. //这里不区分ie9以上,trace:3824
  6102. select: browser.ie
  6103. ? function(noFillData, textRange) {
  6104. var nativeRange
  6105. if (!this.collapsed) this.shrinkBoundary()
  6106. var node = this.getClosedNode()
  6107. if (node && !textRange) {
  6108. try {
  6109. nativeRange = this.document.body.createControlRange()
  6110. nativeRange.addElement(node)
  6111. nativeRange.select()
  6112. } catch (e) {}
  6113. return this
  6114. }
  6115. var bookmark = this.createBookmark(),
  6116. start = bookmark.start,
  6117. end
  6118. nativeRange = this.document.body.createTextRange()
  6119. nativeRange.moveToElementText(start)
  6120. nativeRange.moveStart('character', 1)
  6121. if (!this.collapsed) {
  6122. var nativeRangeEnd = this.document.body.createTextRange()
  6123. end = bookmark.end
  6124. nativeRangeEnd.moveToElementText(end)
  6125. nativeRange.setEndPoint('EndToEnd', nativeRangeEnd)
  6126. } else {
  6127. if (!noFillData && this.startContainer.nodeType != 3) {
  6128. //使用<span>|x<span>固定住光标
  6129. var tmpText = this.document.createTextNode(fillChar),
  6130. tmp = this.document.createElement('span')
  6131. tmp.appendChild(this.document.createTextNode(fillChar))
  6132. start.parentNode.insertBefore(tmp, start)
  6133. start.parentNode.insertBefore(tmpText, start)
  6134. //当点b,i,u时,不能清除i上边的b
  6135. removeFillData(this.document, tmpText)
  6136. fillData = tmpText
  6137. mergeSibling(tmp, 'previousSibling')
  6138. mergeSibling(start, 'nextSibling')
  6139. nativeRange.moveStart('character', -1)
  6140. nativeRange.collapse(true)
  6141. }
  6142. }
  6143. this.moveToBookmark(bookmark)
  6144. tmp && domUtils.remove(tmp)
  6145. //IE在隐藏状态下不支持range操作,catch一下
  6146. try {
  6147. nativeRange.select()
  6148. } catch (e) {}
  6149. return this
  6150. }
  6151. : function(notInsertFillData) {
  6152. function checkOffset(rng) {
  6153. function check(node, offset, dir) {
  6154. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  6155. rng[dir + 'Offset'] = node.nodeValue.length
  6156. }
  6157. }
  6158. check(rng.startContainer, rng.startOffset, 'start')
  6159. check(rng.endContainer, rng.endOffset, 'end')
  6160. }
  6161. var win = domUtils.getWindow(this.document),
  6162. sel = win.getSelection(),
  6163. txtNode
  6164. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  6165. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  6166. browser.gecko ? this.document.body.focus() : win.focus()
  6167. if (sel) {
  6168. sel.removeAllRanges()
  6169. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  6170. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  6171. if (this.collapsed && !notInsertFillData) {
  6172. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  6173. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  6174. // var tmp = this.document.createTextNode('');
  6175. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  6176. // }
  6177. //
  6178. //处理光标落在文本节点的情况
  6179. //处理以下的情况
  6180. //<b>|xxxx</b>
  6181. //<b>xxxx</b>|xxxx
  6182. //xxxx<b>|</b>
  6183. var start = this.startContainer,
  6184. child = start
  6185. if (start.nodeType == 1) {
  6186. child = start.childNodes[this.startOffset]
  6187. }
  6188. if (
  6189. !(start.nodeType == 3 && this.startOffset) &&
  6190. (child
  6191. ? !child.previousSibling ||
  6192. child.previousSibling.nodeType != 3
  6193. : !start.lastChild || start.lastChild.nodeType != 3)
  6194. ) {
  6195. txtNode = this.document.createTextNode(fillChar)
  6196. //跟着前边走
  6197. this.insertNode(txtNode)
  6198. removeFillData(this.document, txtNode)
  6199. mergeSibling(txtNode, 'previousSibling')
  6200. mergeSibling(txtNode, 'nextSibling')
  6201. fillData = txtNode
  6202. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true)
  6203. }
  6204. }
  6205. var nativeRange = this.document.createRange()
  6206. if (
  6207. this.collapsed &&
  6208. browser.opera &&
  6209. this.startContainer.nodeType == 1
  6210. ) {
  6211. var child = this.startContainer.childNodes[this.startOffset]
  6212. if (!child) {
  6213. //往前靠拢
  6214. child = this.startContainer.lastChild
  6215. if (child && domUtils.isBr(child)) {
  6216. this.setStartBefore(child).collapse(true)
  6217. }
  6218. } else {
  6219. //向后靠拢
  6220. while (child && domUtils.isBlockElm(child)) {
  6221. if (child.nodeType == 1 && child.childNodes[0]) {
  6222. child = child.childNodes[0]
  6223. } else {
  6224. break
  6225. }
  6226. }
  6227. child && this.setStartBefore(child).collapse(true)
  6228. }
  6229. }
  6230. //是createAddress最后一位算的不准,现在这里进行微调
  6231. checkOffset(this)
  6232. nativeRange.setStart(this.startContainer, this.startOffset)
  6233. nativeRange.setEnd(this.endContainer, this.endOffset)
  6234. sel.addRange(nativeRange)
  6235. }
  6236. return this
  6237. },
  6238. /**
  6239. * 滚动到当前range开始的位置
  6240. * @method scrollToView
  6241. * @param { Window } win 当前range对象所属的window对象
  6242. * @return { UE.dom.Range } 当前Range对象
  6243. */
  6244. /**
  6245. * 滚动到距离当前range开始位置 offset 的位置处
  6246. * @method scrollToView
  6247. * @param { Window } win 当前range对象所属的window对象
  6248. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  6249. * @return { UE.dom.Range } 当前Range对象
  6250. */
  6251. scrollToView: function(win, offset) {
  6252. win = win ? window : domUtils.getWindow(this.document)
  6253. var me = this,
  6254. span = me.document.createElement('span')
  6255. //trace:717
  6256. span.innerHTML = '&nbsp;'
  6257. me.cloneRange().insertNode(span)
  6258. domUtils.scrollToView(span, win, offset)
  6259. domUtils.remove(span)
  6260. return me
  6261. },
  6262. /**
  6263. * 判断当前选区内容是否占位符
  6264. * @private
  6265. * @method inFillChar
  6266. * @return { Boolean } 如果是占位符返回true,否则返回false
  6267. */
  6268. inFillChar: function() {
  6269. var start = this.startContainer
  6270. if (
  6271. this.collapsed &&
  6272. start.nodeType == 3 &&
  6273. start.nodeValue.replace(new RegExp('^' + domUtils.fillChar), '')
  6274. .length +
  6275. 1 ==
  6276. start.nodeValue.length
  6277. ) {
  6278. return true
  6279. }
  6280. return false
  6281. },
  6282. /**
  6283. * 保存
  6284. * @method createAddress
  6285. * @private
  6286. * @return { Boolean } 返回开始和结束的位置
  6287. * @example
  6288. * ```html
  6289. * <body>
  6290. * <p>
  6291. * aaaa
  6292. * <em>
  6293. * <!-- 选区开始 -->
  6294. * bbbb
  6295. * <!-- 选区结束 -->
  6296. * </em>
  6297. * </p>
  6298. *
  6299. * <script>
  6300. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6301. * console.log( range.createAddress() );
  6302. * </script>
  6303. * </body>
  6304. * ```
  6305. */
  6306. createAddress: function(ignoreEnd, ignoreTxt) {
  6307. var addr = {},
  6308. me = this
  6309. function getAddress(isStart) {
  6310. var node = isStart ? me.startContainer : me.endContainer
  6311. var parents = domUtils.findParents(node, true, function(node) {
  6312. return !domUtils.isBody(node)
  6313. }),
  6314. addrs = []
  6315. for (var i = 0, ci; (ci = parents[i++]); ) {
  6316. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt))
  6317. }
  6318. var firstIndex = 0
  6319. if (ignoreTxt) {
  6320. if (node.nodeType == 3) {
  6321. var tmpNode = node.previousSibling
  6322. while (tmpNode && tmpNode.nodeType == 3) {
  6323. firstIndex += tmpNode.nodeValue.replace(fillCharReg, '').length
  6324. tmpNode = tmpNode.previousSibling
  6325. }
  6326. firstIndex += isStart ? me.startOffset : me.endOffset // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6327. } else {
  6328. node = node.childNodes[isStart ? me.startOffset : me.endOffset]
  6329. if (node) {
  6330. firstIndex = domUtils.getNodeIndex(node, ignoreTxt)
  6331. } else {
  6332. node = isStart ? me.startContainer : me.endContainer
  6333. var first = node.firstChild
  6334. while (first) {
  6335. if (domUtils.isFillChar(first)) {
  6336. first = first.nextSibling
  6337. continue
  6338. }
  6339. firstIndex++
  6340. if (first.nodeType == 3) {
  6341. while (first && first.nodeType == 3) {
  6342. first = first.nextSibling
  6343. }
  6344. } else {
  6345. first = first.nextSibling
  6346. }
  6347. }
  6348. }
  6349. }
  6350. } else {
  6351. firstIndex = isStart
  6352. ? domUtils.isFillChar(node)
  6353. ? 0
  6354. : me.startOffset
  6355. : me.endOffset
  6356. }
  6357. if (firstIndex < 0) {
  6358. firstIndex = 0
  6359. }
  6360. addrs.push(firstIndex)
  6361. return addrs
  6362. }
  6363. addr.startAddress = getAddress(true)
  6364. if (!ignoreEnd) {
  6365. addr.endAddress = me.collapsed
  6366. ? [].concat(addr.startAddress)
  6367. : getAddress()
  6368. }
  6369. return addr
  6370. },
  6371. /**
  6372. * 保存
  6373. * @method createAddress
  6374. * @private
  6375. * @return { Boolean } 返回开始和结束的位置
  6376. * @example
  6377. * ```html
  6378. * <body>
  6379. * <p>
  6380. * aaaa
  6381. * <em>
  6382. * <!-- 选区开始 -->
  6383. * bbbb
  6384. * <!-- 选区结束 -->
  6385. * </em>
  6386. * </p>
  6387. *
  6388. * <script>
  6389. * var range = editor.selection.getRange();
  6390. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6391. * range.select();
  6392. * //output: 'bbbb'
  6393. * console.log(editor.selection.getText());
  6394. * </script>
  6395. * </body>
  6396. * ```
  6397. */
  6398. moveToAddress: function(addr, ignoreEnd) {
  6399. var me = this
  6400. function getNode(address, isStart) {
  6401. var tmpNode = me.document.body,
  6402. parentNode,
  6403. offset
  6404. for (var i = 0, ci, l = address.length; i < l; i++) {
  6405. ci = address[i]
  6406. parentNode = tmpNode
  6407. tmpNode = tmpNode.childNodes[ci]
  6408. if (!tmpNode) {
  6409. offset = ci
  6410. break
  6411. }
  6412. }
  6413. if (isStart) {
  6414. if (tmpNode) {
  6415. me.setStartBefore(tmpNode)
  6416. } else {
  6417. me.setStart(parentNode, offset)
  6418. }
  6419. } else {
  6420. if (tmpNode) {
  6421. me.setEndBefore(tmpNode)
  6422. } else {
  6423. me.setEnd(parentNode, offset)
  6424. }
  6425. }
  6426. }
  6427. getNode(addr.startAddress, true)
  6428. !ignoreEnd && addr.endAddress && getNode(addr.endAddress)
  6429. return me
  6430. },
  6431. /**
  6432. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6433. * @method equals
  6434. * @param { UE.dom.Range } 需要判断的Range对象
  6435. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6436. */
  6437. equals: function(rng) {
  6438. for (var p in this) {
  6439. if (this.hasOwnProperty(p)) {
  6440. if (this[p] !== rng[p]) return false
  6441. }
  6442. }
  6443. return true
  6444. },
  6445. /**
  6446. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6447. * 作为其参数。
  6448. * @method traversal
  6449. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6450. * @return { UE.dom.Range } 当前range对象
  6451. * @example
  6452. * ```html
  6453. *
  6454. * <body>
  6455. *
  6456. * <!-- 选区开始 -->
  6457. * <span></span>
  6458. * <a></a>
  6459. * <!-- 选区结束 -->
  6460. * </body>
  6461. *
  6462. * <script>
  6463. *
  6464. * //output: <span></span><a></a>
  6465. * console.log( range.cloneContents() );
  6466. *
  6467. * range.traversal( function ( node ) {
  6468. *
  6469. * if ( node.nodeType === 1 ) {
  6470. * node.className = "test";
  6471. * }
  6472. *
  6473. * } );
  6474. *
  6475. * //output: <span class="test"></span><a class="test"></a>
  6476. * console.log( range.cloneContents() );
  6477. *
  6478. * </script>
  6479. * ```
  6480. */
  6481. /**
  6482. * 遍历range内的节点。
  6483. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6484. * 作为其参数。
  6485. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6486. * 发doFn函数的执行
  6487. * @method traversal
  6488. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6489. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6490. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6491. * 会触发doFn。
  6492. * @return { UE.dom.Range } 当前range对象
  6493. * @see UE.dom.Range:traversal(Function)
  6494. * @example
  6495. * ```html
  6496. *
  6497. * <body>
  6498. *
  6499. * <!-- 选区开始 -->
  6500. * <span></span>
  6501. * <a></a>
  6502. * <!-- 选区结束 -->
  6503. * </body>
  6504. *
  6505. * <script>
  6506. *
  6507. * //output: <span></span><a></a>
  6508. * console.log( range.cloneContents() );
  6509. *
  6510. * range.traversal( function ( node ) {
  6511. *
  6512. * node.className = "test";
  6513. *
  6514. * }, function ( node ) {
  6515. * return node.nodeType === 1;
  6516. * } );
  6517. *
  6518. * //output: <span class="test"></span><a class="test"></a>
  6519. * console.log( range.cloneContents() );
  6520. *
  6521. * </script>
  6522. * ```
  6523. */
  6524. traversal: function(doFn, filterFn) {
  6525. if (this.collapsed) return this
  6526. var bookmark = this.createBookmark(),
  6527. end = bookmark.end,
  6528. current = domUtils.getNextDomNode(bookmark.start, false, filterFn)
  6529. while (
  6530. current &&
  6531. current !== end &&
  6532. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  6533. ) {
  6534. var tmpNode = domUtils.getNextDomNode(current, false, filterFn)
  6535. doFn(current)
  6536. current = tmpNode
  6537. }
  6538. return this.moveToBookmark(bookmark)
  6539. }
  6540. }
  6541. })()
  6542. // core/Selection.js
  6543. /**
  6544. * 选集
  6545. * @file
  6546. * @module UE.dom
  6547. * @class Selection
  6548. * @since 1.2.6.1
  6549. */
  6550. /**
  6551. * 选区集合
  6552. * @unfile
  6553. * @module UE.dom
  6554. * @class Selection
  6555. */
  6556. ;(function() {
  6557. function getBoundaryInformation(range, start) {
  6558. var getIndex = domUtils.getNodeIndex
  6559. range = range.duplicate()
  6560. range.collapse(start)
  6561. var parent = range.parentElement()
  6562. //如果节点里没有子节点,直接退出
  6563. if (!parent.hasChildNodes()) {
  6564. return {container: parent, offset: 0}
  6565. }
  6566. var siblings = parent.children,
  6567. child,
  6568. testRange = range.duplicate(),
  6569. startIndex = 0,
  6570. endIndex = siblings.length - 1,
  6571. index = -1,
  6572. distance
  6573. while (startIndex <= endIndex) {
  6574. index = Math.floor((startIndex + endIndex) / 2)
  6575. child = siblings[index]
  6576. testRange.moveToElementText(child)
  6577. var position = testRange.compareEndPoints('StartToStart', range)
  6578. if (position > 0) {
  6579. endIndex = index - 1
  6580. } else if (position < 0) {
  6581. startIndex = index + 1
  6582. } else {
  6583. //trace:1043
  6584. return {container: parent, offset: getIndex(child)}
  6585. }
  6586. }
  6587. if (index == -1) {
  6588. testRange.moveToElementText(parent)
  6589. testRange.setEndPoint('StartToStart', range)
  6590. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length
  6591. siblings = parent.childNodes
  6592. if (!distance) {
  6593. child = siblings[siblings.length - 1]
  6594. return {container: child, offset: child.nodeValue.length}
  6595. }
  6596. var i = siblings.length
  6597. while (distance > 0) {
  6598. distance -= siblings[--i].nodeValue.length
  6599. }
  6600. return {container: siblings[i], offset: -distance}
  6601. }
  6602. testRange.collapse(position > 0)
  6603. testRange.setEndPoint(position > 0 ? 'StartToStart' : 'EndToStart', range)
  6604. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length
  6605. if (!distance) {
  6606. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName]
  6607. ? {
  6608. container: parent,
  6609. offset: getIndex(child) + (position > 0 ? 0 : 1)
  6610. }
  6611. : {
  6612. container: child,
  6613. offset: position > 0 ? 0 : child.childNodes.length
  6614. }
  6615. }
  6616. while (distance > 0) {
  6617. try {
  6618. var pre = child
  6619. child = child[position > 0 ? 'previousSibling' : 'nextSibling']
  6620. distance -= child.nodeValue.length
  6621. } catch (e) {
  6622. return {container: parent, offset: getIndex(pre)}
  6623. }
  6624. }
  6625. return {
  6626. container: child,
  6627. offset: position > 0 ? -distance : child.nodeValue.length + distance
  6628. }
  6629. }
  6630. /**
  6631. * 将ieRange转换为Range对象
  6632. * @param {Range} ieRange ieRange对象
  6633. * @param {Range} range Range对象
  6634. * @return {Range} range 返回转换后的Range对象
  6635. */
  6636. function transformIERangeToRange(ieRange, range) {
  6637. if (ieRange.item) {
  6638. range.selectNode(ieRange.item(0))
  6639. } else {
  6640. var bi = getBoundaryInformation(ieRange, true)
  6641. range.setStart(bi.container, bi.offset)
  6642. if (ieRange.compareEndPoints('StartToEnd', ieRange) != 0) {
  6643. bi = getBoundaryInformation(ieRange, false)
  6644. range.setEnd(bi.container, bi.offset)
  6645. }
  6646. }
  6647. return range
  6648. }
  6649. /**
  6650. * 获得ieRange
  6651. * @param {Selection} sel Selection对象
  6652. * @return {ieRange} 得到ieRange
  6653. */
  6654. function _getIERange(sel) {
  6655. var ieRange
  6656. //ie下有可能报错
  6657. try {
  6658. ieRange = sel.getNative().createRange()
  6659. } catch (e) {
  6660. return null
  6661. }
  6662. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement()
  6663. if ((el.ownerDocument || el) === sel.document) {
  6664. return ieRange
  6665. }
  6666. return null
  6667. }
  6668. var Selection = (dom.Selection = function(doc) {
  6669. var me = this,
  6670. iframe
  6671. me.document = doc
  6672. if (browser.ie9below) {
  6673. iframe = domUtils.getWindow(doc).frameElement
  6674. domUtils.on(iframe, 'beforedeactivate', function() {
  6675. me._bakIERange = me.getIERange()
  6676. })
  6677. domUtils.on(iframe, 'activate', function() {
  6678. try {
  6679. if (!_getIERange(me) && me._bakIERange) {
  6680. me._bakIERange.select()
  6681. }
  6682. } catch (ex) {}
  6683. me._bakIERange = null
  6684. })
  6685. }
  6686. iframe = doc = null
  6687. })
  6688. Selection.prototype = {
  6689. rangeInBody: function(rng, txtRange) {
  6690. var node =
  6691. browser.ie9below || txtRange
  6692. ? rng.item
  6693. ? rng.item()
  6694. : rng.parentElement()
  6695. : rng.startContainer
  6696. return (
  6697. node === this.document.body || domUtils.inDoc(node, this.document)
  6698. )
  6699. },
  6700. /**
  6701. * 获取原生seleciton对象
  6702. * @method getNative
  6703. * @return { Object } 获得selection对象
  6704. * @example
  6705. * ```javascript
  6706. * editor.selection.getNative();
  6707. * ```
  6708. */
  6709. getNative: function() {
  6710. var doc = this.document
  6711. try {
  6712. return !doc
  6713. ? null
  6714. : browser.ie9below
  6715. ? doc.selection
  6716. : domUtils.getWindow(doc).getSelection()
  6717. } catch (e) {
  6718. return null
  6719. }
  6720. },
  6721. /**
  6722. * 获得ieRange
  6723. * @method getIERange
  6724. * @return { Object } 返回ie原生的Range
  6725. * @example
  6726. * ```javascript
  6727. * editor.selection.getIERange();
  6728. * ```
  6729. */
  6730. getIERange: function() {
  6731. var ieRange = _getIERange(this)
  6732. if (!ieRange) {
  6733. if (this._bakIERange) {
  6734. return this._bakIERange
  6735. }
  6736. }
  6737. return ieRange
  6738. },
  6739. /**
  6740. * 缓存当前选区的range和选区的开始节点
  6741. * @method cache
  6742. */
  6743. cache: function() {
  6744. this.clear()
  6745. this._cachedRange = this.getRange()
  6746. this._cachedStartElement = this.getStart()
  6747. this._cachedStartElementPath = this.getStartElementPath()
  6748. },
  6749. /**
  6750. * 获取选区开始位置的父节点到body
  6751. * @method getStartElementPath
  6752. * @return { Array } 返回父节点集合
  6753. * @example
  6754. * ```javascript
  6755. * editor.selection.getStartElementPath();
  6756. * ```
  6757. */
  6758. getStartElementPath: function() {
  6759. if (this._cachedStartElementPath) {
  6760. return this._cachedStartElementPath
  6761. }
  6762. var start = this.getStart()
  6763. if (start) {
  6764. return domUtils.findParents(start, true, null, true)
  6765. }
  6766. return []
  6767. },
  6768. /**
  6769. * 清空缓存
  6770. * @method clear
  6771. */
  6772. clear: function() {
  6773. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null
  6774. },
  6775. /**
  6776. * 编辑器是否得到了选区
  6777. * @method isFocus
  6778. */
  6779. isFocus: function() {
  6780. try {
  6781. if (browser.ie9below) {
  6782. var nativeRange = _getIERange(this)
  6783. return !!(nativeRange && this.rangeInBody(nativeRange))
  6784. } else {
  6785. return !!this.getNative().rangeCount
  6786. }
  6787. } catch (e) {
  6788. return false
  6789. }
  6790. },
  6791. /**
  6792. * 获取选区对应的Range
  6793. * @method getRange
  6794. * @return { Object } 得到Range对象
  6795. * @example
  6796. * ```javascript
  6797. * editor.selection.getRange();
  6798. * ```
  6799. */
  6800. getRange: function() {
  6801. var me = this
  6802. function optimze(range) {
  6803. var child = me.document.body.firstChild,
  6804. collapsed = range.collapsed
  6805. while (child && child.firstChild) {
  6806. range.setStart(child, 0)
  6807. child = child.firstChild
  6808. }
  6809. if (!range.startContainer) {
  6810. range.setStart(me.document.body, 0)
  6811. }
  6812. if (collapsed) {
  6813. range.collapse(true)
  6814. }
  6815. }
  6816. if (me._cachedRange != null) {
  6817. return this._cachedRange
  6818. }
  6819. var range = new baidu.editor.dom.Range(me.document)
  6820. if (browser.ie9below) {
  6821. var nativeRange = me.getIERange()
  6822. if (nativeRange) {
  6823. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6824. try {
  6825. transformIERangeToRange(nativeRange, range)
  6826. } catch (e) {
  6827. optimze(range)
  6828. }
  6829. } else {
  6830. optimze(range)
  6831. }
  6832. } else {
  6833. var sel = me.getNative()
  6834. if (sel && sel.rangeCount) {
  6835. var firstRange = sel.getRangeAt(0)
  6836. var lastRange = sel.getRangeAt(sel.rangeCount - 1)
  6837. range
  6838. .setStart(firstRange.startContainer, firstRange.startOffset)
  6839. .setEnd(lastRange.endContainer, lastRange.endOffset)
  6840. if (
  6841. range.collapsed &&
  6842. domUtils.isBody(range.startContainer) &&
  6843. !range.startOffset
  6844. ) {
  6845. optimze(range)
  6846. }
  6847. } else {
  6848. //trace:1734 有可能已经不在dom树上了,标识的节点
  6849. if (
  6850. this._bakRange &&
  6851. domUtils.inDoc(this._bakRange.startContainer, this.document)
  6852. ) {
  6853. return this._bakRange
  6854. }
  6855. optimze(range)
  6856. }
  6857. }
  6858. return (this._bakRange = range)
  6859. },
  6860. /**
  6861. * 获取开始元素,用于状态反射
  6862. * @method getStart
  6863. * @return { Element } 获得开始元素
  6864. * @example
  6865. * ```javascript
  6866. * editor.selection.getStart();
  6867. * ```
  6868. */
  6869. getStart: function() {
  6870. if (this._cachedStartElement) {
  6871. return this._cachedStartElement
  6872. }
  6873. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6874. tmpRange,
  6875. start,
  6876. tmp,
  6877. parent
  6878. if (browser.ie9below) {
  6879. if (!range) {
  6880. //todo 给第一个值可能会有问题
  6881. return this.document.body.firstChild
  6882. }
  6883. //control元素
  6884. if (range.item) {
  6885. return range.item(0)
  6886. }
  6887. tmpRange = range.duplicate()
  6888. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6889. tmpRange.text.length > 0 && tmpRange.moveStart('character', 1)
  6890. tmpRange.collapse(1)
  6891. start = tmpRange.parentElement()
  6892. parent = tmp = range.parentElement()
  6893. while ((tmp = tmp.parentNode)) {
  6894. if (tmp == start) {
  6895. start = parent
  6896. break
  6897. }
  6898. }
  6899. } else {
  6900. range.shrinkBoundary()
  6901. start = range.startContainer
  6902. if (start.nodeType == 1 && start.hasChildNodes()) {
  6903. start =
  6904. start.childNodes[
  6905. Math.min(start.childNodes.length - 1, range.startOffset)
  6906. ]
  6907. }
  6908. if (start.nodeType == 3) {
  6909. return start.parentNode
  6910. }
  6911. }
  6912. return start
  6913. },
  6914. /**
  6915. * 得到选区中的文本
  6916. * @method getText
  6917. * @return { String } 选区中包含的文本
  6918. * @example
  6919. * ```javascript
  6920. * editor.selection.getText();
  6921. * ```
  6922. */
  6923. getText: function() {
  6924. var nativeSel, nativeRange
  6925. if (this.isFocus() && (nativeSel = this.getNative())) {
  6926. nativeRange = browser.ie9below
  6927. ? nativeSel.createRange()
  6928. : nativeSel.getRangeAt(0)
  6929. return browser.ie9below ? nativeRange.text : nativeRange.toString()
  6930. }
  6931. return ''
  6932. },
  6933. /**
  6934. * 清除选区
  6935. * @method clearRange
  6936. * @example
  6937. * ```javascript
  6938. * editor.selection.clearRange();
  6939. * ```
  6940. */
  6941. clearRange: function() {
  6942. this.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']()
  6943. }
  6944. }
  6945. })()
  6946. // core/Editor.js
  6947. /**
  6948. * 编辑器主类,包含编辑器提供的大部分公用接口
  6949. * @file
  6950. * @module UE
  6951. * @class Editor
  6952. * @since 1.2.6.1
  6953. */
  6954. /**
  6955. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6956. * @unfile
  6957. * @module UE
  6958. */
  6959. /**
  6960. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6961. * @unfile
  6962. * @module UE
  6963. * @class Editor
  6964. */
  6965. ;(function() {
  6966. var uid = 0,
  6967. _selectionChangeTimer
  6968. /**
  6969. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6970. * @private
  6971. * @method setValue
  6972. * @param { UE.Editor } editor 编辑器事例
  6973. */
  6974. function setValue(form, editor) {
  6975. var textarea
  6976. if (editor.textarea) {
  6977. if (utils.isString(editor.textarea)) {
  6978. for (
  6979. var i = 0,
  6980. ti,
  6981. tis = domUtils.getElementsByTagName(form, 'textarea');
  6982. (ti = tis[i++]);
  6983. ) {
  6984. if (ti.id == 'ueditor_textarea_' + editor.options.textarea) {
  6985. textarea = ti
  6986. break
  6987. }
  6988. }
  6989. } else {
  6990. textarea = editor.textarea
  6991. }
  6992. }
  6993. if (!textarea) {
  6994. form.appendChild(
  6995. (textarea = domUtils.createElement(document, 'textarea', {
  6996. name: editor.options.textarea,
  6997. id: 'ueditor_textarea_' + editor.options.textarea,
  6998. style: 'display:none'
  6999. }))
  7000. )
  7001. //不要产生多个textarea
  7002. editor.textarea = textarea
  7003. }
  7004. !textarea.getAttribute('name') &&
  7005. textarea.setAttribute('name', editor.options.textarea)
  7006. textarea.value = editor.hasContents()
  7007. ? editor.options.allHtmlEnabled
  7008. ? editor.getAllHtml()
  7009. : editor.getContent(null, null, true)
  7010. : ''
  7011. }
  7012. function loadPlugins(me) {
  7013. //初始化插件
  7014. for (var pi in UE.plugins) {
  7015. UE.plugins[pi].call(me)
  7016. }
  7017. }
  7018. function checkCurLang(I18N) {
  7019. for (var lang in I18N) {
  7020. return lang
  7021. }
  7022. }
  7023. function langReadied(me) {
  7024. me.langIsReady = true
  7025. me.fireEvent('langReady')
  7026. }
  7027. /**
  7028. * 编辑器准备就绪后会触发该事件
  7029. * @module UE
  7030. * @class Editor
  7031. * @event ready
  7032. * @remind render方法执行完成之后,会触发该事件
  7033. * @remind
  7034. * @example
  7035. * ```javascript
  7036. * editor.addListener( 'ready', function( editor ) {
  7037. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  7038. * } );
  7039. * ```
  7040. */
  7041. /**
  7042. * 执行destroy方法,会触发该事件
  7043. * @module UE
  7044. * @class Editor
  7045. * @event destroy
  7046. * @see UE.Editor:destroy()
  7047. */
  7048. /**
  7049. * 执行reset方法,会触发该事件
  7050. * @module UE
  7051. * @class Editor
  7052. * @event reset
  7053. * @see UE.Editor:reset()
  7054. */
  7055. /**
  7056. * 执行focus方法,会触发该事件
  7057. * @module UE
  7058. * @class Editor
  7059. * @event focus
  7060. * @see UE.Editor:focus(Boolean)
  7061. */
  7062. /**
  7063. * 语言加载完成会触发该事件
  7064. * @module UE
  7065. * @class Editor
  7066. * @event langReady
  7067. */
  7068. /**
  7069. * 运行命令之后会触发该命令
  7070. * @module UE
  7071. * @class Editor
  7072. * @event beforeExecCommand
  7073. */
  7074. /**
  7075. * 运行命令之后会触发该命令
  7076. * @module UE
  7077. * @class Editor
  7078. * @event afterExecCommand
  7079. */
  7080. /**
  7081. * 运行命令之前会触发该命令
  7082. * @module UE
  7083. * @class Editor
  7084. * @event firstBeforeExecCommand
  7085. */
  7086. /**
  7087. * 在getContent方法执行之前会触发该事件
  7088. * @module UE
  7089. * @class Editor
  7090. * @event beforeGetContent
  7091. * @see UE.Editor:getContent()
  7092. */
  7093. /**
  7094. * 在getContent方法执行之后会触发该事件
  7095. * @module UE
  7096. * @class Editor
  7097. * @event afterGetContent
  7098. * @see UE.Editor:getContent()
  7099. */
  7100. /**
  7101. * 在getAllHtml方法执行时会触发该事件
  7102. * @module UE
  7103. * @class Editor
  7104. * @event getAllHtml
  7105. * @see UE.Editor:getAllHtml()
  7106. */
  7107. /**
  7108. * 在setContent方法执行之前会触发该事件
  7109. * @module UE
  7110. * @class Editor
  7111. * @event beforeSetContent
  7112. * @see UE.Editor:setContent(String)
  7113. */
  7114. /**
  7115. * 在setContent方法执行之后会触发该事件
  7116. * @module UE
  7117. * @class Editor
  7118. * @event afterSetContent
  7119. * @see UE.Editor:setContent(String)
  7120. */
  7121. /**
  7122. * 每当编辑器内部选区发生改变时,将触发该事件
  7123. * @event selectionchange
  7124. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  7125. * @example
  7126. * ```javascript
  7127. * editor.addListener( 'selectionchange', function( editor ) {
  7128. * console.log('选区发生改变');
  7129. * }
  7130. */
  7131. /**
  7132. * 在所有selectionchange的监听函数执行之前,会触发该事件
  7133. * @module UE
  7134. * @class Editor
  7135. * @event beforeSelectionChange
  7136. * @see UE.Editor:selectionchange
  7137. */
  7138. /**
  7139. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  7140. * @module UE
  7141. * @class Editor
  7142. * @event afterSelectionChange
  7143. * @see UE.Editor:selectionchange
  7144. */
  7145. /**
  7146. * 编辑器内容发生改变时会触发该事件
  7147. * @module UE
  7148. * @class Editor
  7149. * @event contentChange
  7150. */
  7151. /**
  7152. * 以默认参数构建一个编辑器实例
  7153. * @constructor
  7154. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7155. * @example
  7156. * ```javascript
  7157. * var editor = new UE.Editor();
  7158. * editor.execCommand('blod');
  7159. * ```
  7160. * @see UE.Config
  7161. */
  7162. /**
  7163. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  7164. * @constructor
  7165. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7166. * @param { Object } setting 创建编辑器的参数
  7167. * @example
  7168. * ```javascript
  7169. * var editor = new UE.Editor();
  7170. * editor.execCommand('blod');
  7171. * ```
  7172. * @see UE.Config
  7173. */
  7174. var Editor = (UE.Editor = function(options) {
  7175. var me = this
  7176. me.uid = uid++
  7177. EventBase.call(me)
  7178. me.commands = {}
  7179. me.options = utils.extend(
  7180. utils.clone(options || {}),
  7181. UEDITOR_CONFIG,
  7182. true
  7183. )
  7184. me.shortcutkeys = {}
  7185. me.inputRules = []
  7186. me.outputRules = []
  7187. //设置默认的常用属性
  7188. me.setOpt(Editor.defaultOptions(me))
  7189. /* 尝试异步加载后台配置 */
  7190. me.loadServerConfig()
  7191. if (!utils.isEmptyObject(UE.I18N)) {
  7192. //修改默认的语言类型
  7193. me.options.lang = checkCurLang(UE.I18N)
  7194. UE.plugin.load(me)
  7195. langReadied(me)
  7196. } else {
  7197. utils.loadFile(
  7198. document,
  7199. {
  7200. src:
  7201. me.options.langPath +
  7202. me.options.lang +
  7203. '/' +
  7204. me.options.lang +
  7205. '.js',
  7206. tag: 'script',
  7207. type: 'text/javascript',
  7208. defer: 'defer'
  7209. },
  7210. function() {
  7211. UE.plugin.load(me)
  7212. langReadied(me)
  7213. }
  7214. )
  7215. }
  7216. UE.instants['ueditorInstant' + me.uid] = me
  7217. })
  7218. Editor.prototype = {
  7219. registerCommand: function(name, obj) {
  7220. this.commands[name] = obj
  7221. },
  7222. /**
  7223. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  7224. * @method ready
  7225. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  7226. * 立即触发该回调。
  7227. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  7228. * @example
  7229. * ```javascript
  7230. * editor.ready( function( editor ) {
  7231. * editor.setContent('初始化完毕');
  7232. * } );
  7233. * ```
  7234. * @see UE.Editor.event:ready
  7235. */
  7236. ready: function(fn) {
  7237. var me = this
  7238. if (fn) {
  7239. me.isReady ? fn.apply(me) : me.addListener('ready', fn)
  7240. }
  7241. },
  7242. /**
  7243. * 该方法是提供给插件里面使用,设置配置项默认值
  7244. * @method setOpt
  7245. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7246. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7247. * @param { String } key 编辑器的可接受的选项名称
  7248. * @param { * } val 该选项可接受的值
  7249. * @example
  7250. * ```javascript
  7251. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  7252. * ```
  7253. */
  7254. /**
  7255. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  7256. * @method setOpt
  7257. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7258. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7259. * @param { Object } options 将要设置的选项的键值对对象
  7260. * @example
  7261. * ```javascript
  7262. * editor.setOpt( {
  7263. * 'initContent': '欢迎使用编辑器'
  7264. * } );
  7265. * ```
  7266. */
  7267. setOpt: function(key, val) {
  7268. var obj = {}
  7269. if (utils.isString(key)) {
  7270. obj[key] = val
  7271. } else {
  7272. obj = key
  7273. }
  7274. utils.extend(this.options, obj, true)
  7275. },
  7276. getOpt: function(key) {
  7277. return this.options[key]
  7278. },
  7279. /**
  7280. * 销毁编辑器实例,使用textarea代替
  7281. * @method destroy
  7282. * @example
  7283. * ```javascript
  7284. * editor.destroy();
  7285. * ```
  7286. */
  7287. destroy: function() {
  7288. var me = this
  7289. me.fireEvent('destroy')
  7290. var container = me.container.parentNode
  7291. var textarea = me.textarea
  7292. if (!textarea) {
  7293. textarea = document.createElement('textarea')
  7294. container.parentNode.insertBefore(textarea, container)
  7295. } else {
  7296. textarea.style.display = ''
  7297. }
  7298. textarea.style.width = me.iframe.offsetWidth + 'px'
  7299. textarea.style.height = me.iframe.offsetHeight + 'px'
  7300. textarea.value = me.getContent()
  7301. textarea.id = me.key
  7302. container.innerHTML = ''
  7303. domUtils.remove(container)
  7304. var key = me.key
  7305. //trace:2004
  7306. for (var p in me) {
  7307. if (me.hasOwnProperty(p)) {
  7308. delete this[p]
  7309. }
  7310. }
  7311. UE.delEditor(key)
  7312. },
  7313. /**
  7314. * 渲染编辑器的DOM到指定容器
  7315. * @method render
  7316. * @param { String } containerId 指定一个容器ID
  7317. * @remind 执行该方法,会触发ready事件
  7318. * @warning 必须且只能调用一次
  7319. */
  7320. /**
  7321. * 渲染编辑器的DOM到指定容器
  7322. * @method render
  7323. * @param { Element } containerDom 直接指定容器对象
  7324. * @remind 执行该方法,会触发ready事件
  7325. * @warning 必须且只能调用一次
  7326. */
  7327. render: function(container) {
  7328. var me = this,
  7329. options = me.options,
  7330. getStyleValue = function(attr) {
  7331. return parseInt(domUtils.getComputedStyle(container, attr))
  7332. }
  7333. if (utils.isString(container)) {
  7334. container = document.getElementById(container)
  7335. }
  7336. if (container) {
  7337. if (options.initialFrameWidth) {
  7338. options.minFrameWidth = options.initialFrameWidth
  7339. } else {
  7340. options.minFrameWidth = options.initialFrameWidth =
  7341. container.offsetWidth
  7342. }
  7343. if (options.initialFrameHeight) {
  7344. options.minFrameHeight = options.initialFrameHeight
  7345. } else {
  7346. options.initialFrameHeight = options.minFrameHeight =
  7347. container.offsetHeight
  7348. }
  7349. container.style.width = /%$/.test(options.initialFrameWidth)
  7350. ? '100%'
  7351. : options.initialFrameWidth -
  7352. getStyleValue('padding-left') -
  7353. getStyleValue('padding-right') +
  7354. 'px'
  7355. container.style.height = /%$/.test(options.initialFrameHeight)
  7356. ? '100%'
  7357. : options.initialFrameHeight -
  7358. getStyleValue('padding-top') -
  7359. getStyleValue('padding-bottom') +
  7360. 'px'
  7361. container.style.zIndex = options.zIndex
  7362. var html =
  7363. (ie && browser.version < 9 ? '' : '<!DOCTYPE html>') +
  7364. "<html xmlns='http://www.w3.org/1999/xhtml' class='view' ><head>" +
  7365. "<style type='text/css'>" +
  7366. //设置四周的留边
  7367. '.view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n' +
  7368. //设置默认字体和字号
  7369. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7370. 'body{margin:8px;font-family:sans-serif;font-size:16px;}' +
  7371. //设置段落间距
  7372. 'p{margin:5px 0;}</style>' +
  7373. (options.iframeCssUrl
  7374. ? "<link rel='stylesheet' type='text/css' href='" +
  7375. utils.unhtml(options.iframeCssUrl) +
  7376. "'/>"
  7377. : '') +
  7378. (options.initialStyle
  7379. ? '<style>' + options.initialStyle + '</style>'
  7380. : '') +
  7381. "</head><body class='view' ></body>" +
  7382. "<script type='text/javascript' " +
  7383. (ie ? "defer='defer'" : '') +
  7384. " id='_initialScript'>" +
  7385. "setTimeout(function(){editor = window.parent.UE.instants['ueditorInstant" +
  7386. me.uid +
  7387. "'];editor._setup(document);},0);" +
  7388. "var _tmpScript = document.getElementById('_initialScript');_tmpScript.parentNode.removeChild(_tmpScript);</script></html>"
  7389. container.appendChild(
  7390. domUtils.createElement(document, 'iframe', {
  7391. id: 'ueditor_' + me.uid,
  7392. width: '100%',
  7393. height: '100%',
  7394. frameborder: '0',
  7395. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7396. // scrolling :'no',
  7397. src:
  7398. 'javascript:void(function(){document.open();' +
  7399. (options.customDomain && document.domain != location.hostname
  7400. ? 'document.domain="' + document.domain + '";'
  7401. : '') +
  7402. 'document.write("' +
  7403. html +
  7404. '");document.close();}())'
  7405. })
  7406. )
  7407. container.style.overflow = 'hidden'
  7408. //解决如果是给定的百分比,会导致高度算不对的问题
  7409. setTimeout(function() {
  7410. if (/%$/.test(options.initialFrameWidth)) {
  7411. options.minFrameWidth = options.initialFrameWidth =
  7412. container.offsetWidth
  7413. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7414. // container.style.width = options.initialFrameWidth + 'px';
  7415. }
  7416. if (/%$/.test(options.initialFrameHeight)) {
  7417. options.minFrameHeight = options.initialFrameHeight =
  7418. container.offsetHeight
  7419. container.style.height = options.initialFrameHeight + 'px'
  7420. }
  7421. })
  7422. }
  7423. },
  7424. /**
  7425. * 编辑器初始化
  7426. * @method _setup
  7427. * @private
  7428. * @param { Element } doc 编辑器Iframe中的文档对象
  7429. */
  7430. _setup: function(doc) {
  7431. var me = this,
  7432. options = me.options
  7433. if (ie) {
  7434. doc.body.disabled = true
  7435. doc.body.contentEditable = true
  7436. doc.body.disabled = false
  7437. } else {
  7438. doc.body.contentEditable = true
  7439. }
  7440. doc.body.spellcheck = false
  7441. me.document = doc
  7442. me.window = doc.defaultView || doc.parentWindow
  7443. me.iframe = me.window.frameElement
  7444. me.body = doc.body
  7445. me.selection = new dom.Selection(doc)
  7446. //gecko初始化就能得到range,无法判断isFocus了
  7447. var geckoSel
  7448. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7449. geckoSel.removeAllRanges()
  7450. }
  7451. this._initEvents()
  7452. //为form提交提供一个隐藏的textarea
  7453. for (
  7454. var form = this.iframe.parentNode;
  7455. !domUtils.isBody(form);
  7456. form = form.parentNode
  7457. ) {
  7458. if (form.tagName == 'FORM') {
  7459. me.form = form
  7460. if (me.options.autoSyncData) {
  7461. domUtils.on(me.window, 'blur', function() {
  7462. setValue(form, me)
  7463. })
  7464. } else {
  7465. domUtils.on(form, 'submit', function() {
  7466. setValue(this, me)
  7467. })
  7468. }
  7469. break
  7470. }
  7471. }
  7472. if (options.initialContent) {
  7473. if (options.autoClearinitialContent) {
  7474. var oldExecCommand = me.execCommand
  7475. me.execCommand = function() {
  7476. me.fireEvent('firstBeforeExecCommand')
  7477. return oldExecCommand.apply(me, arguments)
  7478. }
  7479. this._setDefaultContent(options.initialContent)
  7480. } else this.setContent(options.initialContent, false, true)
  7481. }
  7482. //编辑器不能为空内容
  7483. if (domUtils.isEmptyNode(me.body)) {
  7484. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  7485. }
  7486. //如果要求focus, 就把光标定位到内容开始
  7487. if (options.focus) {
  7488. setTimeout(function() {
  7489. me.focus(me.options.focusInEnd)
  7490. //如果自动清除开着,就不需要做selectionchange;
  7491. !me.options.autoClearinitialContent && me._selectionChange()
  7492. }, 0)
  7493. }
  7494. if (!me.container) {
  7495. me.container = this.iframe.parentNode
  7496. }
  7497. if (options.fullscreen && me.ui) {
  7498. me.ui.setFullScreen(true)
  7499. }
  7500. try {
  7501. me.document.execCommand('2D-position', false, false)
  7502. } catch (e) {}
  7503. try {
  7504. me.document.execCommand('enableInlineTableEditing', false, false)
  7505. } catch (e) {}
  7506. try {
  7507. me.document.execCommand('enableObjectResizing', false, false)
  7508. } catch (e) {}
  7509. //挂接快捷键
  7510. me._bindshortcutKeys()
  7511. me.isReady = 1
  7512. me.fireEvent('ready')
  7513. options.onready && options.onready.call(me)
  7514. if (!browser.ie9below) {
  7515. domUtils.on(me.window, ['blur', 'focus'], function(e) {
  7516. //chrome下会出现alt+tab切换时,导致选区位置不对
  7517. if (e.type == 'blur') {
  7518. me._bakRange = me.selection.getRange()
  7519. try {
  7520. me._bakNativeRange = me.selection.getNative().getRangeAt(0)
  7521. me.selection.getNative().removeAllRanges()
  7522. } catch (e) {
  7523. me._bakNativeRange = null
  7524. }
  7525. } else {
  7526. try {
  7527. me._bakRange && me._bakRange.select()
  7528. } catch (e) {}
  7529. }
  7530. })
  7531. }
  7532. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7533. if (browser.gecko && browser.version <= 10902) {
  7534. //修复ff3.6初始化进来,不能点击获得焦点
  7535. me.body.contentEditable = false
  7536. setTimeout(function() {
  7537. me.body.contentEditable = true
  7538. }, 100)
  7539. setInterval(function() {
  7540. me.body.style.height = me.iframe.offsetHeight - 20 + 'px'
  7541. }, 100)
  7542. }
  7543. !options.isShow && me.setHide()
  7544. options.readonly && me.setDisabled()
  7545. },
  7546. /**
  7547. * 同步数据到编辑器所在的form
  7548. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7549. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7550. * @method sync
  7551. * @example
  7552. * ```javascript
  7553. * editor.sync();
  7554. * form.sumbit(); //form变量已经指向了form元素
  7555. * ```
  7556. */
  7557. /**
  7558. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7559. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7560. * @method sync
  7561. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7562. */
  7563. sync: function(formId) {
  7564. var me = this,
  7565. form = formId
  7566. ? document.getElementById(formId)
  7567. : domUtils.findParent(
  7568. me.iframe.parentNode,
  7569. function(node) {
  7570. return node.tagName == 'FORM'
  7571. },
  7572. true
  7573. )
  7574. form && setValue(form, me)
  7575. },
  7576. /**
  7577. * 设置编辑器高度
  7578. * @method setHeight
  7579. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7580. * @param { Number } number 设置的高度值,纯数值,不带单位
  7581. * @example
  7582. * ```javascript
  7583. * editor.setHeight(number);
  7584. * ```
  7585. */
  7586. setHeight: function(height, notSetHeight) {
  7587. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7588. this.iframe.parentNode.style.height = height + 'px'
  7589. }
  7590. !notSetHeight &&
  7591. (this.options.minFrameHeight = this.options.initialFrameHeight = height)
  7592. this.body.style.height = height + 'px'
  7593. !notSetHeight && this.trigger('setHeight')
  7594. },
  7595. /**
  7596. * 为编辑器的编辑命令提供快捷键
  7597. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7598. * @method addshortcutkey
  7599. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7600. * @example
  7601. * ```javascript
  7602. * editor.addshortcutkey({
  7603. * "Bold" : "ctrl+66",//^B
  7604. * "Italic" : "ctrl+73", //^I
  7605. * });
  7606. * ```
  7607. */
  7608. /**
  7609. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7610. * @method addshortcutkey
  7611. * @param { String } cmd 触发快捷键时,响应的命令
  7612. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7613. * @example
  7614. * ```javascript
  7615. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7616. * ```
  7617. */
  7618. addshortcutkey: function(cmd, keys) {
  7619. var obj = {}
  7620. if (keys) {
  7621. obj[cmd] = keys
  7622. } else {
  7623. obj = cmd
  7624. }
  7625. utils.extend(this.shortcutkeys, obj)
  7626. },
  7627. /**
  7628. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7629. * @method _bindshortcutKeys
  7630. * @private
  7631. */
  7632. _bindshortcutKeys: function() {
  7633. var me = this,
  7634. shortcutkeys = this.shortcutkeys
  7635. me.addListener('keydown', function(type, e) {
  7636. var keyCode = e.keyCode || e.which
  7637. for (var i in shortcutkeys) {
  7638. var tmp = shortcutkeys[i].split(',')
  7639. for (var t = 0, ti; (ti = tmp[t++]); ) {
  7640. ti = ti.split(':')
  7641. var key = ti[0],
  7642. param = ti[1]
  7643. if (
  7644. /^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) ||
  7645. /^(\d+)$/.test(key)
  7646. ) {
  7647. if (
  7648. ((RegExp.$1 == 'ctrl' ? e.ctrlKey || e.metaKey : 0) &&
  7649. (RegExp.$2 != '' ? e[RegExp.$2.slice(1) + 'Key'] : 1) &&
  7650. keyCode == RegExp.$3) ||
  7651. keyCode == RegExp.$1
  7652. ) {
  7653. if (me.queryCommandState(i, param) != -1)
  7654. me.execCommand(i, param)
  7655. domUtils.preventDefault(e)
  7656. }
  7657. }
  7658. }
  7659. }
  7660. })
  7661. },
  7662. /**
  7663. * 获取编辑器的内容
  7664. * @method getContent
  7665. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7666. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7667. * @example
  7668. * ```javascript
  7669. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7670. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7671. * ```
  7672. */
  7673. /**
  7674. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7675. * @method getContent
  7676. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7677. * 代表当前编辑器的内容是否空,
  7678. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7679. * 经过内置过滤规则处理后的内容。
  7680. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7681. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7682. * @return { String } 编辑器的内容字符串
  7683. * @example
  7684. * ```javascript
  7685. * // editor 是一个编辑器的实例
  7686. * var content = editor.getContent( function ( editor ) {
  7687. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7688. * } );
  7689. * ```
  7690. */
  7691. getContent: function(cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7692. var me = this
  7693. if (cmd && utils.isFunction(cmd)) {
  7694. fn = cmd
  7695. cmd = ''
  7696. }
  7697. if (fn ? !fn() : !this.hasContents()) {
  7698. return ''
  7699. }
  7700. me.fireEvent('beforegetcontent')
  7701. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank)
  7702. me.filterOutputRule(root)
  7703. me.fireEvent('aftergetcontent', cmd, root)
  7704. return root.toHtml(formatter)
  7705. },
  7706. /**
  7707. * 取得完整的html代码,可以直接显示成完整的html文档
  7708. * @method getAllHtml
  7709. * @return { String } 编辑器的内容html文档字符串
  7710. * @eaxmple
  7711. * ```javascript
  7712. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7713. * ```
  7714. */
  7715. getAllHtml: function() {
  7716. var me = this,
  7717. headHtml = [],
  7718. html = ''
  7719. me.fireEvent('getAllHtml', headHtml)
  7720. if (browser.ie && browser.version > 8) {
  7721. var headHtmlForIE9 = ''
  7722. utils.each(me.document.styleSheets, function(si) {
  7723. headHtmlForIE9 += si.href
  7724. ? '<link rel="stylesheet" type="text/css" href="' +
  7725. si.href +
  7726. '" />'
  7727. : '<style>' + si.cssText + '</style>'
  7728. })
  7729. utils.each(me.document.getElementsByTagName('script'), function(si) {
  7730. headHtmlForIE9 += si.outerHTML
  7731. })
  7732. }
  7733. return (
  7734. '<html><head>' +
  7735. (me.options.charset
  7736. ? '<meta http-equiv="Content-Type" content="text/html; charset=' +
  7737. me.options.charset +
  7738. '"/>'
  7739. : '') +
  7740. (headHtmlForIE9 ||
  7741. me.document.getElementsByTagName('head')[0].innerHTML) +
  7742. headHtml.join('\n') +
  7743. '</head>' +
  7744. '<body ' +
  7745. (ie && browser.version < 9 ? 'class="view"' : '') +
  7746. '>' +
  7747. me.getContent(null, null, true) +
  7748. '</body></html>'
  7749. )
  7750. },
  7751. /**
  7752. * 得到编辑器的纯文本内容,但会保留段落格式
  7753. * @method getPlainTxt
  7754. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7755. * @example
  7756. * ```javascript
  7757. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7758. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7759. * ```
  7760. */
  7761. getPlainTxt: function() {
  7762. var reg = new RegExp(domUtils.fillChar, 'g'),
  7763. html = this.body.innerHTML.replace(/[\n\r]/g, '') //ie要先去了\n在处理
  7764. html = html
  7765. .replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n')
  7766. .replace(/<br\/?>/gi, '\n')
  7767. .replace(/<[^>/]+>/g, '')
  7768. .replace(/(\n)?<\/([^>]+)>/g, function(a, b, c) {
  7769. return dtd.$block[c] ? '\n' : b ? b : ''
  7770. })
  7771. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7772. return html
  7773. .replace(reg, '')
  7774. .replace(/\u00a0/g, ' ')
  7775. .replace(/&nbsp;/g, ' ')
  7776. },
  7777. /**
  7778. * 获取编辑器中的纯文本内容,没有段落格式
  7779. * @method getContentTxt
  7780. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7781. * @example
  7782. * ```javascript
  7783. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7784. * console.log(editor.getPlainTxt()); //输出:"12
  7785. * ```
  7786. */
  7787. getContentTxt: function() {
  7788. var reg = new RegExp(domUtils.fillChar, 'g')
  7789. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7790. return this.body[browser.ie ? 'innerText' : 'textContent']
  7791. .replace(reg, '')
  7792. .replace(/\u00a0/g, ' ')
  7793. },
  7794. /**
  7795. * 设置编辑器的内容,可修改编辑器当前的html内容
  7796. * @method setContent
  7797. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7798. * @warning 该方法会触发selectionchange事件
  7799. * @param { String } html 要插入的html内容
  7800. * @example
  7801. * ```javascript
  7802. * editor.getContent('<p>test</p>');
  7803. * ```
  7804. */
  7805. /**
  7806. * 设置编辑器的内容,可修改编辑器当前的html内容
  7807. * @method setContent
  7808. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7809. * @warning 该方法会触发selectionchange事件
  7810. * @param { String } html 要插入的html内容
  7811. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7812. * @example
  7813. * ```javascript
  7814. * //假设设置前的编辑器内容是 <p>old text</p>
  7815. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7816. * ```
  7817. */
  7818. setContent: function(html, isAppendTo, notFireSelectionchange) {
  7819. var me = this
  7820. me.fireEvent('beforesetcontent', html)
  7821. var root = UE.htmlparser(html)
  7822. me.filterInputRule(root)
  7823. html = root.toHtml()
  7824. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : '') + html
  7825. function isCdataDiv(node) {
  7826. return node.tagName == 'DIV' && node.getAttribute('cdata_tag')
  7827. }
  7828. //给文本或者inline节点套p标签
  7829. if (me.options.enterTag == 'p') {
  7830. var child = this.body.firstChild,
  7831. tmpNode
  7832. if (
  7833. !child ||
  7834. (child.nodeType == 1 &&
  7835. (dtd.$cdata[child.tagName] ||
  7836. isCdataDiv(child) ||
  7837. domUtils.isCustomeNode(child)) &&
  7838. child === this.body.lastChild)
  7839. ) {
  7840. this.body.innerHTML =
  7841. '<p>' +
  7842. (browser.ie ? '&nbsp;' : '<br/>') +
  7843. '</p>' +
  7844. this.body.innerHTML
  7845. } else {
  7846. var p = me.document.createElement('p')
  7847. while (child) {
  7848. while (
  7849. child &&
  7850. (child.nodeType == 3 ||
  7851. (child.nodeType == 1 &&
  7852. dtd.p[child.tagName] &&
  7853. !dtd.$cdata[child.tagName]))
  7854. ) {
  7855. tmpNode = child.nextSibling
  7856. p.appendChild(child)
  7857. child = tmpNode
  7858. }
  7859. if (p.firstChild) {
  7860. if (!child) {
  7861. me.body.appendChild(p)
  7862. break
  7863. } else {
  7864. child.parentNode.insertBefore(p, child)
  7865. p = me.document.createElement('p')
  7866. }
  7867. }
  7868. child = child.nextSibling
  7869. }
  7870. }
  7871. }
  7872. me.fireEvent('aftersetcontent')
  7873. me.fireEvent('contentchange')
  7874. !notFireSelectionchange && me._selectionChange()
  7875. //清除保存的选区
  7876. me._bakRange = me._bakIERange = me._bakNativeRange = null
  7877. //trace:1742 setContent后gecko能得到焦点问题
  7878. var geckoSel
  7879. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7880. geckoSel.removeAllRanges()
  7881. }
  7882. if (me.options.autoSyncData) {
  7883. me.form && setValue(me.form, me)
  7884. }
  7885. },
  7886. /**
  7887. * 让编辑器获得焦点,默认focus到编辑器头部
  7888. * @method focus
  7889. * @example
  7890. * ```javascript
  7891. * editor.focus()
  7892. * ```
  7893. */
  7894. /**
  7895. * 让编辑器获得焦点,toEnd确定focus位置
  7896. * @method focus
  7897. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7898. * @example
  7899. * ```javascript
  7900. * editor.focus(true)
  7901. * ```
  7902. */
  7903. focus: function(toEnd) {
  7904. try {
  7905. var me = this,
  7906. rng = me.selection.getRange()
  7907. if (toEnd) {
  7908. var node = me.body.lastChild
  7909. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7910. if (domUtils.isEmptyBlock(node)) {
  7911. rng.setStartAtFirst(node)
  7912. } else {
  7913. rng.setStartAtLast(node)
  7914. }
  7915. rng.collapse(true)
  7916. }
  7917. rng.setCursor(true)
  7918. } else {
  7919. if (
  7920. !rng.collapsed &&
  7921. domUtils.isBody(rng.startContainer) &&
  7922. rng.startOffset == 0
  7923. ) {
  7924. var node = me.body.firstChild
  7925. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7926. rng.setStartAtFirst(node).collapse(true)
  7927. }
  7928. }
  7929. rng.select(true)
  7930. }
  7931. this.fireEvent('focus selectionchange')
  7932. } catch (e) {}
  7933. },
  7934. isFocus: function() {
  7935. return this.selection.isFocus()
  7936. },
  7937. blur: function() {
  7938. var sel = this.selection.getNative()
  7939. if (sel.empty && browser.ie) {
  7940. var nativeRng = document.body.createTextRange()
  7941. nativeRng.moveToElementText(document.body)
  7942. nativeRng.collapse(true)
  7943. nativeRng.select()
  7944. sel.empty()
  7945. } else {
  7946. sel.removeAllRanges()
  7947. }
  7948. //this.fireEvent('blur selectionchange');
  7949. },
  7950. /**
  7951. * 初始化UE事件及部分事件代理
  7952. * @method _initEvents
  7953. * @private
  7954. */
  7955. _initEvents: function() {
  7956. var me = this,
  7957. doc = me.document,
  7958. win = me.window
  7959. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me)
  7960. domUtils.on(
  7961. doc,
  7962. [
  7963. 'click',
  7964. 'contextmenu',
  7965. 'mousedown',
  7966. 'keydown',
  7967. 'keyup',
  7968. 'keypress',
  7969. 'mouseup',
  7970. 'mouseover',
  7971. 'mouseout',
  7972. 'selectstart'
  7973. ],
  7974. me._proxyDomEvent
  7975. )
  7976. domUtils.on(win, ['focus', 'blur'], me._proxyDomEvent)
  7977. domUtils.on(me.body, 'drop', function(e) {
  7978. //阻止ff下默认的弹出新页面打开图片
  7979. if (browser.gecko && e.stopPropagation) {
  7980. e.stopPropagation()
  7981. }
  7982. me.fireEvent('contentchange')
  7983. })
  7984. domUtils.on(doc, ['mouseup', 'keydown'], function(evt) {
  7985. //特殊键不触发selectionchange
  7986. if (
  7987. evt.type == 'keydown' &&
  7988. (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)
  7989. ) {
  7990. return
  7991. }
  7992. if (evt.button == 2) return
  7993. me._selectionChange(250, evt)
  7994. })
  7995. },
  7996. /**
  7997. * 触发事件代理
  7998. * @method _proxyDomEvent
  7999. * @private
  8000. * @return { * } fireEvent的返回值
  8001. * @see UE.EventBase:fireEvent(String)
  8002. */
  8003. _proxyDomEvent: function(evt) {
  8004. if (
  8005. this.fireEvent(
  8006. 'before' + evt.type.replace(/^on/, '').toLowerCase()
  8007. ) === false
  8008. ) {
  8009. return false
  8010. }
  8011. if (this.fireEvent(evt.type.replace(/^on/, ''), evt) === false) {
  8012. return false
  8013. }
  8014. return this.fireEvent(
  8015. 'after' + evt.type.replace(/^on/, '').toLowerCase()
  8016. )
  8017. },
  8018. /**
  8019. * 变化选区
  8020. * @method _selectionChange
  8021. * @private
  8022. */
  8023. _selectionChange: function(delay, evt) {
  8024. var me = this
  8025. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  8026. // if ( !me.selection.isFocus() ){
  8027. // return;
  8028. // }
  8029. var hackForMouseUp = false
  8030. var mouseX, mouseY
  8031. if (browser.ie && browser.version < 9 && evt && evt.type == 'mouseup') {
  8032. var range = this.selection.getRange()
  8033. if (!range.collapsed) {
  8034. hackForMouseUp = true
  8035. mouseX = evt.clientX
  8036. mouseY = evt.clientY
  8037. }
  8038. }
  8039. clearTimeout(_selectionChangeTimer)
  8040. _selectionChangeTimer = setTimeout(function() {
  8041. if (!me.selection || !me.selection.getNative()) {
  8042. return
  8043. }
  8044. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  8045. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  8046. var ieRange
  8047. if (hackForMouseUp && me.selection.getNative().type == 'None') {
  8048. ieRange = me.document.body.createTextRange()
  8049. try {
  8050. ieRange.moveToPoint(mouseX, mouseY)
  8051. } catch (ex) {
  8052. ieRange = null
  8053. }
  8054. }
  8055. var bakGetIERange
  8056. if (ieRange) {
  8057. bakGetIERange = me.selection.getIERange
  8058. me.selection.getIERange = function() {
  8059. return ieRange
  8060. }
  8061. }
  8062. me.selection.cache()
  8063. if (bakGetIERange) {
  8064. me.selection.getIERange = bakGetIERange
  8065. }
  8066. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  8067. me.fireEvent('beforeselectionchange')
  8068. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  8069. me.fireEvent('selectionchange', !!evt)
  8070. me.fireEvent('afterselectionchange')
  8071. me.selection.clear()
  8072. }
  8073. }, delay || 50)
  8074. },
  8075. /**
  8076. * 执行编辑命令
  8077. * @method _callCmdFn
  8078. * @private
  8079. * @param { String } fnName 函数名称
  8080. * @param { * } args 传给命令函数的参数
  8081. * @return { * } 返回命令函数运行的返回值
  8082. */
  8083. _callCmdFn: function(fnName, args) {
  8084. var cmdName = args[0].toLowerCase(),
  8085. cmd,
  8086. cmdFn
  8087. cmd = this.commands[cmdName] || UE.commands[cmdName]
  8088. cmdFn = cmd && cmd[fnName]
  8089. //没有querycommandstate或者没有command的都默认返回0
  8090. if ((!cmd || !cmdFn) && fnName == 'queryCommandState') {
  8091. return 0
  8092. } else if (cmdFn) {
  8093. return cmdFn.apply(this, args)
  8094. }
  8095. },
  8096. /**
  8097. * 执行编辑命令cmdName,完成富文本编辑效果
  8098. * @method execCommand
  8099. * @param { String } cmdName 需要执行的命令
  8100. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8101. * @return { * } 返回命令函数运行的返回值
  8102. * @example
  8103. * ```javascript
  8104. * editor.execCommand(cmdName);
  8105. * ```
  8106. */
  8107. execCommand: function(cmdName) {
  8108. cmdName = cmdName.toLowerCase()
  8109. var me = this,
  8110. result,
  8111. cmd = me.commands[cmdName] || UE.commands[cmdName]
  8112. if (!cmd || !cmd.execCommand) {
  8113. return null
  8114. }
  8115. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  8116. me.__hasEnterExecCommand = true
  8117. if (me.queryCommandState.apply(me, arguments) != -1) {
  8118. me.fireEvent('saveScene')
  8119. me.fireEvent.apply(
  8120. me,
  8121. ['beforeexeccommand', cmdName].concat(arguments)
  8122. )
  8123. result = this._callCmdFn('execCommand', arguments)
  8124. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  8125. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  8126. me.fireEvent.apply(
  8127. me,
  8128. ['afterexeccommand', cmdName].concat(arguments)
  8129. )
  8130. me.fireEvent('saveScene')
  8131. }
  8132. me.__hasEnterExecCommand = false
  8133. } else {
  8134. result = this._callCmdFn('execCommand', arguments)
  8135. !me.__hasEnterExecCommand &&
  8136. !cmd.ignoreContentChange &&
  8137. !me._ignoreContentChange &&
  8138. me.fireEvent('contentchange')
  8139. }
  8140. !me.__hasEnterExecCommand &&
  8141. !cmd.ignoreContentChange &&
  8142. !me._ignoreContentChange &&
  8143. me._selectionChange()
  8144. return result
  8145. },
  8146. /**
  8147. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  8148. * @method queryCommandState
  8149. * @param { String } cmdName 需要查询的命令名称
  8150. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8151. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  8152. * @example
  8153. * ```javascript
  8154. * editor.queryCommandState(cmdName) => (-1|0|1)
  8155. * ```
  8156. * @see COMMAND.LIST
  8157. */
  8158. queryCommandState: function(cmdName) {
  8159. return this._callCmdFn('queryCommandState', arguments)
  8160. },
  8161. /**
  8162. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  8163. * @method queryCommandValue
  8164. * @param { String } cmdName 需要查询的命令名称
  8165. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8166. * @remind 只有部分插件有此方法
  8167. * @return { * } 返回每个命令特定的当前状态值
  8168. * @grammar editor.queryCommandValue(cmdName) => {*}
  8169. * @see COMMAND.LIST
  8170. */
  8171. queryCommandValue: function(cmdName) {
  8172. return this._callCmdFn('queryCommandValue', arguments)
  8173. },
  8174. /**
  8175. * 检查编辑区域中是否有内容
  8176. * @method hasContents
  8177. * @remind 默认有文本内容,或者有以下节点都不认为是空
  8178. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  8179. * @return { Boolean } 检查有内容返回true,否则返回false
  8180. * @example
  8181. * ```javascript
  8182. * editor.hasContents()
  8183. * ```
  8184. */
  8185. /**
  8186. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  8187. * @method hasContents
  8188. * @param { Array } tags 传入数组判断时用到的节点类型
  8189. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  8190. * @example
  8191. * ```javascript
  8192. * editor.hasContents(['span']);
  8193. * ```
  8194. */
  8195. hasContents: function(tags) {
  8196. if (tags) {
  8197. for (var i = 0, ci; (ci = tags[i++]); ) {
  8198. if (this.document.getElementsByTagName(ci).length > 0) {
  8199. return true
  8200. }
  8201. }
  8202. }
  8203. if (!domUtils.isEmptyBlock(this.body)) {
  8204. return true
  8205. }
  8206. //随时添加,定义的特殊标签如果存在,不能认为是空
  8207. tags = ['div']
  8208. for (i = 0; (ci = tags[i++]); ) {
  8209. var nodes = domUtils.getElementsByTagName(this.document, ci)
  8210. for (var n = 0, cn; (cn = nodes[n++]); ) {
  8211. if (domUtils.isCustomeNode(cn)) {
  8212. return true
  8213. }
  8214. }
  8215. }
  8216. return false
  8217. },
  8218. /**
  8219. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  8220. * @method reset
  8221. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  8222. * @example
  8223. * ```javascript
  8224. * editor.reset()
  8225. * ```
  8226. */
  8227. reset: function() {
  8228. this.fireEvent('reset')
  8229. },
  8230. /**
  8231. * 设置当前编辑区域可以编辑
  8232. * @method setEnabled
  8233. * @example
  8234. * ```javascript
  8235. * editor.setEnabled()
  8236. * ```
  8237. */
  8238. setEnabled: function() {
  8239. var me = this,
  8240. range
  8241. if (me.body.contentEditable == 'false') {
  8242. me.body.contentEditable = true
  8243. range = me.selection.getRange()
  8244. //有可能内容丢失了
  8245. try {
  8246. range.moveToBookmark(me.lastBk)
  8247. delete me.lastBk
  8248. } catch (e) {
  8249. range.setStartAtFirst(me.body).collapse(true)
  8250. }
  8251. range.select(true)
  8252. if (me.bkqueryCommandState) {
  8253. me.queryCommandState = me.bkqueryCommandState
  8254. delete me.bkqueryCommandState
  8255. }
  8256. if (me.bkqueryCommandValue) {
  8257. me.queryCommandValue = me.bkqueryCommandValue
  8258. delete me.bkqueryCommandValue
  8259. }
  8260. me.fireEvent('selectionchange')
  8261. }
  8262. },
  8263. enable: function() {
  8264. return this.setEnabled()
  8265. },
  8266. /** 设置当前编辑区域不可编辑
  8267. * @method setDisabled
  8268. */
  8269. /** 设置当前编辑区域不可编辑,except中的命令除外
  8270. * @method setDisabled
  8271. * @param { String } except 例外命令的字符串
  8272. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8273. * @example
  8274. * ```javascript
  8275. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  8276. * ```
  8277. */
  8278. /** 设置当前编辑区域不可编辑,except中的命令除外
  8279. * @method setDisabled
  8280. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  8281. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8282. * @example
  8283. * ```javascript
  8284. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  8285. * ```
  8286. */
  8287. setDisabled: function(except) {
  8288. var me = this
  8289. except = except ? (utils.isArray(except) ? except : [except]) : []
  8290. if (me.body.contentEditable == 'true') {
  8291. if (!me.lastBk) {
  8292. me.lastBk = me.selection.getRange().createBookmark(true)
  8293. }
  8294. me.body.contentEditable = false
  8295. me.bkqueryCommandState = me.queryCommandState
  8296. me.bkqueryCommandValue = me.queryCommandValue
  8297. me.queryCommandState = function(type) {
  8298. if (utils.indexOf(except, type) != -1) {
  8299. return me.bkqueryCommandState.apply(me, arguments)
  8300. }
  8301. return -1
  8302. }
  8303. me.queryCommandValue = function(type) {
  8304. if (utils.indexOf(except, type) != -1) {
  8305. return me.bkqueryCommandValue.apply(me, arguments)
  8306. }
  8307. return null
  8308. }
  8309. me.fireEvent('selectionchange')
  8310. }
  8311. },
  8312. disable: function(except) {
  8313. return this.setDisabled(except)
  8314. },
  8315. /**
  8316. * 设置默认内容
  8317. * @method _setDefaultContent
  8318. * @private
  8319. * @param { String } cont 要存入的内容
  8320. */
  8321. _setDefaultContent: (function() {
  8322. function clear() {
  8323. var me = this
  8324. if (me.document.getElementById('initContent')) {
  8325. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>'
  8326. me.removeListener('firstBeforeExecCommand focus', clear)
  8327. setTimeout(function() {
  8328. me.focus()
  8329. me._selectionChange()
  8330. }, 0)
  8331. }
  8332. }
  8333. return function(cont) {
  8334. var me = this
  8335. me.body.innerHTML = '<p id="initContent">' + cont + '</p>'
  8336. me.addListener('firstBeforeExecCommand focus', clear)
  8337. }
  8338. })(),
  8339. /**
  8340. * 显示编辑器
  8341. * @method setShow
  8342. * @example
  8343. * ```javascript
  8344. * editor.setShow()
  8345. * ```
  8346. */
  8347. setShow: function() {
  8348. var me = this,
  8349. range = me.selection.getRange()
  8350. if (me.container.style.display == 'none') {
  8351. //有可能内容丢失了
  8352. try {
  8353. range.moveToBookmark(me.lastBk)
  8354. delete me.lastBk
  8355. } catch (e) {
  8356. range.setStartAtFirst(me.body).collapse(true)
  8357. }
  8358. //ie下focus实效,所以做了个延迟
  8359. setTimeout(function() {
  8360. range.select(true)
  8361. }, 100)
  8362. me.container.style.display = ''
  8363. }
  8364. },
  8365. show: function() {
  8366. return this.setShow()
  8367. },
  8368. /**
  8369. * 隐藏编辑器
  8370. * @method setHide
  8371. * @example
  8372. * ```javascript
  8373. * editor.setHide()
  8374. * ```
  8375. */
  8376. setHide: function() {
  8377. var me = this
  8378. if (!me.lastBk) {
  8379. me.lastBk = me.selection.getRange().createBookmark(true)
  8380. }
  8381. me.container.style.display = 'none'
  8382. },
  8383. hide: function() {
  8384. return this.setHide()
  8385. },
  8386. /**
  8387. * 根据指定的路径,获取对应的语言资源
  8388. * @method getLang
  8389. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  8390. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  8391. * @example
  8392. * ```javascript
  8393. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  8394. * ```
  8395. */
  8396. getLang: function(path) {
  8397. var lang = UE.I18N[this.options.lang]
  8398. if (!lang) {
  8399. throw Error('not import language file')
  8400. }
  8401. path = (path || '').split('.')
  8402. for (var i = 0, ci; (ci = path[i++]); ) {
  8403. lang = lang[ci]
  8404. if (!lang) break
  8405. }
  8406. return lang
  8407. },
  8408. /**
  8409. * 计算编辑器html内容字符串的长度
  8410. * @method getContentLength
  8411. * @return { Number } 返回计算的长度
  8412. * @example
  8413. * ```javascript
  8414. * //编辑器html内容<p><strong>132</strong></p>
  8415. * editor.getContentLength() //返回27
  8416. * ```
  8417. */
  8418. /**
  8419. * 计算编辑器当前纯文本内容的长度
  8420. * @method getContentLength
  8421. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  8422. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  8423. * @example
  8424. * ```javascript
  8425. * //编辑器html内容<p><strong>132</strong></p>
  8426. * editor.getContentLength() //返回3
  8427. * ```
  8428. */
  8429. getContentLength: function(ingoneHtml, tagNames) {
  8430. var count = this.getContent(false, false, true).length
  8431. if (ingoneHtml) {
  8432. tagNames = (tagNames || []).concat(['hr', 'img', 'iframe'])
  8433. count = this.getContentTxt().replace(/[\t\r\n]+/g, '').length
  8434. for (var i = 0, ci; (ci = tagNames[i++]); ) {
  8435. count += this.document.getElementsByTagName(ci).length
  8436. }
  8437. }
  8438. return count
  8439. },
  8440. /**
  8441. * 注册输入过滤规则
  8442. * @method addInputRule
  8443. * @param { Function } rule 要添加的过滤规则
  8444. * @example
  8445. * ```javascript
  8446. * editor.addInputRule(function(root){
  8447. * $.each(root.getNodesByTagName('div'),function(i,node){
  8448. * node.tagName="p";
  8449. * });
  8450. * });
  8451. * ```
  8452. */
  8453. addInputRule: function(rule) {
  8454. this.inputRules.push(rule)
  8455. },
  8456. /**
  8457. * 执行注册的过滤规则
  8458. * @method filterInputRule
  8459. * @param { UE.uNode } root 要过滤的uNode节点
  8460. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  8461. * @example
  8462. * ```javascript
  8463. * editor.filterInputRule(editor.body);
  8464. * ```
  8465. * @see UE.Editor:addInputRule
  8466. */
  8467. filterInputRule: function(root) {
  8468. for (var i = 0, ci; (ci = this.inputRules[i++]); ) {
  8469. ci.call(this, root)
  8470. }
  8471. },
  8472. /**
  8473. * 注册输出过滤规则
  8474. * @method addOutputRule
  8475. * @param { Function } rule 要添加的过滤规则
  8476. * @example
  8477. * ```javascript
  8478. * editor.addOutputRule(function(root){
  8479. * $.each(root.getNodesByTagName('p'),function(i,node){
  8480. * node.tagName="div";
  8481. * });
  8482. * });
  8483. * ```
  8484. */
  8485. addOutputRule: function(rule) {
  8486. this.outputRules.push(rule)
  8487. },
  8488. /**
  8489. * 根据输出过滤规则,过滤编辑器内容
  8490. * @method filterOutputRule
  8491. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8492. * @param { UE.uNode } root 要过滤的uNode节点
  8493. * @example
  8494. * ```javascript
  8495. * editor.filterOutputRule(editor.body);
  8496. * ```
  8497. * @see UE.Editor:addOutputRule
  8498. */
  8499. filterOutputRule: function(root) {
  8500. for (var i = 0, ci; (ci = this.outputRules[i++]); ) {
  8501. ci.call(this, root)
  8502. }
  8503. },
  8504. /**
  8505. * 根据action名称获取请求的路径
  8506. * @method getActionUrl
  8507. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8508. * @param { String } action action名称
  8509. * @example
  8510. * ```javascript
  8511. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8512. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8513. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8514. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8515. * ```
  8516. */
  8517. getActionUrl: function(action) {
  8518. var actionName = this.getOpt(action) || action,
  8519. imageUrl = this.getOpt('imageUrl'),
  8520. serverUrl = this.getOpt('serverUrl')
  8521. if (!serverUrl && imageUrl) {
  8522. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2')
  8523. }
  8524. if (serverUrl) {
  8525. serverUrl =
  8526. serverUrl +
  8527. (serverUrl.indexOf('?') == -1 ? '?' : '&') +
  8528. 'action=' +
  8529. (actionName || '')
  8530. return utils.formatUrl(serverUrl)
  8531. } else {
  8532. return ''
  8533. }
  8534. }
  8535. }
  8536. utils.inherits(Editor, EventBase)
  8537. })()
  8538. // core/Editor.defaultoptions.js
  8539. //维护编辑器一下默认的不在插件中的配置项
  8540. UE.Editor.defaultOptions = function(editor) {
  8541. var _url = editor.options.UEDITOR_HOME_URL
  8542. return {
  8543. isShow: true,
  8544. initialContent: '',
  8545. initialStyle: '',
  8546. autoClearinitialContent: false,
  8547. iframeCssUrl: _url + 'themes/iframe.css',
  8548. textarea: 'editorValue',
  8549. focus: false,
  8550. focusInEnd: true,
  8551. autoClearEmptyNode: true,
  8552. fullscreen: false,
  8553. readonly: false,
  8554. zIndex: 9999,
  8555. imagePopup: true,
  8556. enterTag: 'p',
  8557. customDomain: false,
  8558. lang: 'zh-cn',
  8559. langPath: _url + 'lang/',
  8560. theme: 'default',
  8561. themePath: _url + 'themes/',
  8562. allHtmlEnabled: false,
  8563. scaleEnabled: false,
  8564. tableNativeEditInFF: false,
  8565. autoSyncData: true,
  8566. fileNameFormat: '{time}{rand:6}'
  8567. }
  8568. }
  8569. // core/loadconfig.js
  8570. ;(function() {
  8571. UE.Editor.prototype.loadServerConfig = function() {
  8572. var me = this
  8573. setTimeout(function() {
  8574. try {
  8575. me.options.imageUrl &&
  8576. me.setOpt(
  8577. 'serverUrl',
  8578. me.options.imageUrl.replace(
  8579. /^(.*[\/]).+([\.].+)$/,
  8580. '$1controller$2'
  8581. )
  8582. )
  8583. var configUrl = me.getActionUrl('config'),
  8584. //isJsonp = utils.isCrossDomainUrl(configUrl);
  8585. isJsonp = false
  8586. /* 发出ajax请求 */
  8587. me._serverConfigLoaded = false
  8588. configUrl &&
  8589. UE.ajax.request(configUrl, {
  8590. method: 'GET',
  8591. dataType: isJsonp ? 'jsonp' : '',
  8592. onsuccess: function(r) {
  8593. try {
  8594. var config = isJsonp ? r : eval('(' + r.responseText + ')')
  8595. utils.extend(me.options, config)
  8596. me.fireEvent('serverConfigLoaded')
  8597. me._serverConfigLoaded = true
  8598. } catch (e) {
  8599. showErrorMsg(me.getLang('loadconfigFormatError'))
  8600. }
  8601. },
  8602. onerror: function() {
  8603. showErrorMsg(me.getLang('loadconfigHttpError'))
  8604. }
  8605. })
  8606. } catch (e) {
  8607. showErrorMsg(me.getLang('loadconfigError'))
  8608. }
  8609. })
  8610. function showErrorMsg(msg) {
  8611. console && console.error(msg)
  8612. //me.fireEvent('showMessage', {
  8613. // 'title': msg,
  8614. // 'type': 'error'
  8615. //});
  8616. }
  8617. }
  8618. UE.Editor.prototype.isServerConfigLoaded = function() {
  8619. var me = this
  8620. return me._serverConfigLoaded || false
  8621. }
  8622. UE.Editor.prototype.afterConfigReady = function(handler) {
  8623. if (!handler || !utils.isFunction(handler)) return
  8624. var me = this
  8625. var readyHandler = function() {
  8626. handler.apply(me, arguments)
  8627. me.removeListener('serverConfigLoaded', readyHandler)
  8628. }
  8629. if (me.isServerConfigLoaded()) {
  8630. handler.call(me, 'serverConfigLoaded')
  8631. } else {
  8632. me.addListener('serverConfigLoaded', readyHandler)
  8633. }
  8634. }
  8635. })()
  8636. // core/ajax.js
  8637. /**
  8638. * @file
  8639. * @module UE.ajax
  8640. * @since 1.2.6.1
  8641. */
  8642. /**
  8643. * 提供对ajax请求的支持
  8644. * @module UE.ajax
  8645. */
  8646. UE.ajax = (function() {
  8647. //创建一个ajaxRequest对象
  8648. var fnStr = 'XMLHttpRequest()'
  8649. try {
  8650. new ActiveXObject('Msxml2.XMLHTTP')
  8651. fnStr = "ActiveXObject('Msxml2.XMLHTTP')"
  8652. } catch (e) {
  8653. try {
  8654. new ActiveXObject('Microsoft.XMLHTTP')
  8655. fnStr = "ActiveXObject('Microsoft.XMLHTTP')"
  8656. } catch (e) {}
  8657. }
  8658. var creatAjaxRequest = new Function('return new ' + fnStr)
  8659. /**
  8660. * 将json参数转化成适合ajax提交的参数列表
  8661. * @param json
  8662. */
  8663. function json2str(json) {
  8664. var strArr = []
  8665. for (var i in json) {
  8666. //忽略默认的几个参数
  8667. if (
  8668. i == 'method' ||
  8669. i == 'timeout' ||
  8670. i == 'async' ||
  8671. i == 'dataType' ||
  8672. i == 'callback'
  8673. )
  8674. continue
  8675. //忽略控制
  8676. if (json[i] == undefined || json[i] == null) continue
  8677. //传递过来的对象和函数不在提交之列
  8678. if (
  8679. !(
  8680. (typeof json[i]).toLowerCase() == 'function' ||
  8681. (typeof json[i]).toLowerCase() == 'object'
  8682. )
  8683. ) {
  8684. strArr.push(encodeURIComponent(i) + '=' + encodeURIComponent(json[i]))
  8685. } else if (utils.isArray(json[i])) {
  8686. //支持传数组内容
  8687. for (var j = 0; j < json[i].length; j++) {
  8688. strArr.push(
  8689. encodeURIComponent(i) + '[]=' + encodeURIComponent(json[i][j])
  8690. )
  8691. }
  8692. }
  8693. }
  8694. return strArr.join('&')
  8695. }
  8696. function doAjax(url, ajaxOptions) {
  8697. var xhr = creatAjaxRequest(),
  8698. //是否超时
  8699. timeIsOut = false,
  8700. //默认参数
  8701. defaultAjaxOptions = {
  8702. method: 'POST',
  8703. timeout: 5000,
  8704. async: true,
  8705. data: {}, //需要传递对象的话只能覆盖
  8706. onsuccess: function() {},
  8707. onerror: function() {}
  8708. }
  8709. if (typeof url === 'object') {
  8710. ajaxOptions = url
  8711. url = ajaxOptions.url
  8712. }
  8713. if (!xhr || !url) return
  8714. var ajaxOpts = ajaxOptions
  8715. ? utils.extend(defaultAjaxOptions, ajaxOptions)
  8716. : defaultAjaxOptions
  8717. var submitStr = json2str(ajaxOpts) // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8718. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8719. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8720. submitStr += (submitStr ? '&' : '') + json2str(ajaxOpts.data)
  8721. }
  8722. //超时检测
  8723. var timerID = setTimeout(function() {
  8724. if (xhr.readyState != 4) {
  8725. timeIsOut = true
  8726. xhr.abort()
  8727. clearTimeout(timerID)
  8728. }
  8729. }, ajaxOpts.timeout)
  8730. var method = ajaxOpts.method.toUpperCase()
  8731. var str =
  8732. url +
  8733. (url.indexOf('?') == -1 ? '?' : '&') +
  8734. (method == 'POST' ? '' : submitStr + '&noCache=' + +new Date())
  8735. xhr.open(method, str, ajaxOpts.async)
  8736. xhr.onreadystatechange = function() {
  8737. if (xhr.readyState == 4) {
  8738. if (!timeIsOut && xhr.status == 200) {
  8739. ajaxOpts.onsuccess(xhr)
  8740. } else {
  8741. ajaxOpts.onerror(xhr)
  8742. }
  8743. }
  8744. }
  8745. xhr.setRequestHeader('Authorization', getAuthorization())
  8746. if (method == 'POST') {
  8747. xhr.setRequestHeader(
  8748. 'Content-Type',
  8749. 'application/x-www-form-urlencoded'
  8750. )
  8751. xhr.send(submitStr)
  8752. } else {
  8753. xhr.send(null)
  8754. }
  8755. }
  8756. function doJsonp(url, opts) {
  8757. var successhandler = opts.onsuccess || function() {},
  8758. scr = document.createElement('SCRIPT'),
  8759. options = opts || {},
  8760. charset = options['charset'],
  8761. callbackField = options['jsonp'] || 'callback',
  8762. callbackFnName,
  8763. timeOut = options['timeOut'] || 0,
  8764. timer,
  8765. reg = new RegExp('(\\?|&)' + callbackField + '=([^&]*)'),
  8766. matches
  8767. if (utils.isFunction(successhandler)) {
  8768. callbackFnName =
  8769. 'bd__editor__' + Math.floor(Math.random() * 2147483648).toString(36)
  8770. window[callbackFnName] = getCallBack(0)
  8771. } else if (utils.isString(successhandler)) {
  8772. callbackFnName = successhandler
  8773. } else {
  8774. if ((matches = reg.exec(url))) {
  8775. callbackFnName = matches[2]
  8776. }
  8777. }
  8778. url = url.replace(reg, '\x241' + callbackField + '=' + callbackFnName)
  8779. if (url.search(reg) < 0) {
  8780. url +=
  8781. (url.indexOf('?') < 0 ? '?' : '&') +
  8782. callbackField +
  8783. '=' +
  8784. callbackFnName
  8785. }
  8786. var queryStr = json2str(opts) // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8787. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8788. if (!utils.isEmptyObject(opts.data)) {
  8789. queryStr += (queryStr ? '&' : '') + json2str(opts.data)
  8790. }
  8791. if (queryStr) {
  8792. url = url.replace(/\?/, '?' + queryStr + '&')
  8793. }
  8794. scr.onerror = getCallBack(1)
  8795. if (timeOut) {
  8796. timer = setTimeout(getCallBack(1), timeOut)
  8797. }
  8798. createScriptTag(scr, url, charset)
  8799. function createScriptTag(scr, url, charset) {
  8800. scr.setAttribute('type', 'text/javascript')
  8801. scr.setAttribute('defer', 'defer')
  8802. charset && scr.setAttribute('charset', charset)
  8803. scr.setAttribute('src', url)
  8804. document.getElementsByTagName('head')[0].appendChild(scr)
  8805. }
  8806. function getCallBack(onTimeOut) {
  8807. return function() {
  8808. try {
  8809. if (onTimeOut) {
  8810. options.onerror && options.onerror()
  8811. } else {
  8812. try {
  8813. clearTimeout(timer)
  8814. successhandler.apply(window, arguments)
  8815. } catch (e) {}
  8816. }
  8817. } catch (exception) {
  8818. options.onerror && options.onerror.call(window, exception)
  8819. } finally {
  8820. options.oncomplete && options.oncomplete.apply(window, arguments)
  8821. scr.parentNode && scr.parentNode.removeChild(scr)
  8822. window[callbackFnName] = null
  8823. try {
  8824. delete window[callbackFnName]
  8825. } catch (e) {}
  8826. }
  8827. }
  8828. }
  8829. }
  8830. return {
  8831. /**
  8832. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8833. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8834. * @method request
  8835. * @param { URLString } url ajax请求的url地址
  8836. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8837. * @example
  8838. * ```javascript
  8839. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8840. * UE.ajax.requeset( 'sayhello.php', {
  8841. *
  8842. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8843. * method: 'GET',
  8844. *
  8845. * //超时时间。 默认为5000, 单位是ms
  8846. * timeout: 10000,
  8847. *
  8848. * //是否是异步请求。 true为异步请求, false为同步请求
  8849. * async: true,
  8850. *
  8851. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8852. * data: {
  8853. * name: 'ueditor'
  8854. * },
  8855. *
  8856. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8857. * onsuccess: function ( xhr ) {
  8858. * console.log( xhr.responseText );
  8859. * },
  8860. *
  8861. * //请求失败或者超时后的回调。
  8862. * onerror: function ( xhr ) {
  8863. * alert( 'Ajax请求失败' );
  8864. * }
  8865. *
  8866. * } );
  8867. * ```
  8868. */
  8869. /**
  8870. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8871. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8872. * @method request
  8873. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8874. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8875. * @example
  8876. * ```javascript
  8877. *
  8878. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8879. * UE.ajax.requeset( 'sayhello.php', {
  8880. *
  8881. * //请求的地址, 该项是必须的。
  8882. * url: 'sayhello.php'
  8883. *
  8884. * } );
  8885. * ```
  8886. */
  8887. request: function(url, opts) {
  8888. if (opts && opts.dataType == 'jsonp') {
  8889. doJsonp(url, opts)
  8890. } else {
  8891. doAjax(url, opts)
  8892. }
  8893. },
  8894. getJSONP: function(url, data, fn) {
  8895. var opts = {
  8896. data: data,
  8897. oncomplete: fn
  8898. }
  8899. doJsonp(url, opts)
  8900. }
  8901. }
  8902. })()
  8903. // core/filterword.js
  8904. /**
  8905. * UE过滤word的静态方法
  8906. * @file
  8907. */
  8908. /**
  8909. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8910. * @module UE
  8911. */
  8912. /**
  8913. * 根据传入html字符串过滤word
  8914. * @module UE
  8915. * @since 1.2.6.1
  8916. * @method filterWord
  8917. * @param { String } html html字符串
  8918. * @return { String } 已过滤后的结果字符串
  8919. * @example
  8920. * ```javascript
  8921. * UE.filterWord(html);
  8922. * ```
  8923. */
  8924. var filterWord = (UE.filterWord = (function() {
  8925. //是否是word过来的内容
  8926. function isWordDocument(str) {
  8927. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/gi.test(
  8928. str
  8929. )
  8930. }
  8931. //去掉小数
  8932. function transUnit(v) {
  8933. v = v.replace(/[\d.]+\w+/g, function(m) {
  8934. return utils.transUnitToPx(m)
  8935. })
  8936. return v
  8937. }
  8938. function filterPasteWord(str) {
  8939. return (
  8940. str
  8941. .replace(/[\t\r\n]+/g, ' ')
  8942. .replace(/<!--[\s\S]*?-->/gi, '')
  8943. //转换图片
  8944. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function(str) {
  8945. //opera能自己解析出image所这里直接返回空
  8946. if (browser.opera) {
  8947. return ''
  8948. }
  8949. try {
  8950. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8951. if (/Bitmap/i.test(str)) {
  8952. return ''
  8953. }
  8954. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8955. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8956. src = str.match(/src=\s*"([^"]*)"/i)[1]
  8957. return (
  8958. '<img width="' +
  8959. transUnit(width) +
  8960. '" height="' +
  8961. transUnit(height) +
  8962. '" src="' +
  8963. src +
  8964. '" />'
  8965. )
  8966. } catch (e) {
  8967. return ''
  8968. }
  8969. })
  8970. //针对wps添加的多余标签处理
  8971. .replace(/<\/?div[^>]*>/g, '')
  8972. //去掉多余的属性
  8973. .replace(/v:\w+=(["']?)[^'"]+\1/g, '')
  8974. .replace(
  8975. /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi,
  8976. ''
  8977. )
  8978. .replace(
  8979. /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi,
  8980. '<p><strong>$1</strong></p>'
  8981. )
  8982. //去掉多余的属性
  8983. .replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/gi, function(
  8984. str,
  8985. name,
  8986. marks,
  8987. val
  8988. ) {
  8989. //保留list的标示
  8990. return name == 'class' && val == 'MsoListParagraph' ? str : ''
  8991. })
  8992. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8993. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function(a, b, c) {
  8994. return c.replace(/[\t\r\n ]+/g, ' ')
  8995. })
  8996. //处理style的问题
  8997. .replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function(
  8998. str,
  8999. tag,
  9000. tmp,
  9001. style
  9002. ) {
  9003. var n = [],
  9004. s = style
  9005. .replace(/^\s+|\s+$/, '')
  9006. .replace(/&#39;/g, "'")
  9007. .replace(/&quot;/gi, "'")
  9008. .replace(/[\d.]+(cm|pt)/g, function(str) {
  9009. return utils.transUnitToPx(str)
  9010. })
  9011. .split(/;\s*/g)
  9012. for (var i = 0, v; (v = s[i]); i++) {
  9013. var name,
  9014. value,
  9015. parts = v.split(':')
  9016. if (parts.length == 2) {
  9017. name = parts[0].toLowerCase()
  9018. value = parts[1].toLowerCase()
  9019. if (
  9020. (/^(background)\w*/.test(name) &&
  9021. value.replace(/(initial|\s)/g, '').length == 0) ||
  9022. (/^(margin)\w*/.test(name) && /^0\w+$/.test(value))
  9023. ) {
  9024. continue
  9025. }
  9026. switch (name) {
  9027. case 'mso-padding-alt':
  9028. case 'mso-padding-top-alt':
  9029. case 'mso-padding-right-alt':
  9030. case 'mso-padding-bottom-alt':
  9031. case 'mso-padding-left-alt':
  9032. case 'mso-margin-alt':
  9033. case 'mso-margin-top-alt':
  9034. case 'mso-margin-right-alt':
  9035. case 'mso-margin-bottom-alt':
  9036. case 'mso-margin-left-alt':
  9037. //ie下会出现挤到一起的情况
  9038. //case "mso-table-layout-alt":
  9039. case 'mso-height':
  9040. case 'mso-width':
  9041. case 'mso-vertical-align-alt':
  9042. //trace:1819 ff下会解析出padding在table上
  9043. if (!/<table/.test(tag))
  9044. n[i] =
  9045. name.replace(/^mso-|-alt$/g, '') +
  9046. ':' +
  9047. transUnit(value)
  9048. continue
  9049. case 'horiz-align':
  9050. n[i] = 'text-align:' + value
  9051. continue
  9052. case 'vert-align':
  9053. n[i] = 'vertical-align:' + value
  9054. continue
  9055. case 'font-color':
  9056. case 'mso-foreground':
  9057. n[i] = 'color:' + value
  9058. continue
  9059. case 'mso-background':
  9060. case 'mso-highlight':
  9061. n[i] = 'background:' + value
  9062. continue
  9063. case 'mso-default-height':
  9064. n[i] = 'min-height:' + transUnit(value)
  9065. continue
  9066. case 'mso-default-width':
  9067. n[i] = 'min-width:' + transUnit(value)
  9068. continue
  9069. case 'mso-padding-between-alt':
  9070. n[i] =
  9071. 'border-collapse:separate;border-spacing:' +
  9072. transUnit(value)
  9073. continue
  9074. case 'text-line-through':
  9075. if (value == 'single' || value == 'double') {
  9076. n[i] = 'text-decoration:line-through'
  9077. }
  9078. continue
  9079. case 'mso-zero-height':
  9080. if (value == 'yes') {
  9081. n[i] = 'display:none'
  9082. }
  9083. continue
  9084. // case 'background':
  9085. // break;
  9086. case 'margin':
  9087. if (!/[1-9]/.test(value)) {
  9088. continue
  9089. }
  9090. }
  9091. if (
  9092. /^(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(
  9093. name
  9094. ) ||
  9095. (/text\-indent|padding|margin/.test(name) &&
  9096. /\-[\d.]+/.test(value))
  9097. ) {
  9098. continue
  9099. }
  9100. n[i] = name + ':' + parts[1]
  9101. }
  9102. }
  9103. return (
  9104. tag +
  9105. (n.length
  9106. ? ' style="' + n.join(';').replace(/;{2,}/g, ';') + '"'
  9107. : '')
  9108. )
  9109. })
  9110. )
  9111. }
  9112. return function(html) {
  9113. return isWordDocument(html) ? filterPasteWord(html) : html
  9114. }
  9115. })())
  9116. // core/node.js
  9117. /**
  9118. * 编辑器模拟的节点类
  9119. * @file
  9120. * @module UE
  9121. * @class uNode
  9122. * @since 1.2.6.1
  9123. */
  9124. /**
  9125. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9126. * @unfile
  9127. * @module UE
  9128. */
  9129. ;(function() {
  9130. /**
  9131. * 编辑器模拟的节点类
  9132. * @unfile
  9133. * @module UE
  9134. * @class uNode
  9135. */
  9136. /**
  9137. * 通过一个键值对,创建一个uNode对象
  9138. * @constructor
  9139. * @param { Object } attr 传入要创建的uNode的初始属性
  9140. * @example
  9141. * ```javascript
  9142. * var node = new uNode({
  9143. * type:'element',
  9144. * tagName:'span',
  9145. * attrs:{style:'font-size:14px;'}
  9146. * }
  9147. * ```
  9148. */
  9149. var uNode = (UE.uNode = function(obj) {
  9150. this.type = obj.type
  9151. this.data = obj.data
  9152. this.tagName = obj.tagName
  9153. this.parentNode = obj.parentNode
  9154. this.attrs = obj.attrs || {}
  9155. this.children = obj.children
  9156. })
  9157. var notTransAttrs = {
  9158. href: 1,
  9159. src: 1,
  9160. _src: 1,
  9161. _href: 1,
  9162. cdata_data: 1
  9163. }
  9164. var notTransTagName = {
  9165. style: 1,
  9166. script: 1
  9167. }
  9168. var indentChar = ' ',
  9169. breakChar = '\n'
  9170. function insertLine(arr, current, begin) {
  9171. arr.push(breakChar)
  9172. return current + (begin ? 1 : -1)
  9173. }
  9174. function insertIndent(arr, current) {
  9175. //插入缩进
  9176. for (var i = 0; i < current; i++) {
  9177. arr.push(indentChar)
  9178. }
  9179. }
  9180. //创建uNode的静态方法
  9181. //支持标签和html
  9182. uNode.createElement = function(html) {
  9183. if (/[<>]/.test(html)) {
  9184. return UE.htmlparser(html).children[0]
  9185. } else {
  9186. return new uNode({
  9187. type: 'element',
  9188. children: [],
  9189. tagName: html
  9190. })
  9191. }
  9192. }
  9193. uNode.createText = function(data, noTrans) {
  9194. return new UE.uNode({
  9195. type: 'text',
  9196. data: noTrans ? data : utils.unhtml(data || '')
  9197. })
  9198. }
  9199. function nodeToHtml(node, arr, formatter, current) {
  9200. switch (node.type) {
  9201. case 'root':
  9202. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9203. //插入新行
  9204. if (
  9205. formatter &&
  9206. ci.type == 'element' &&
  9207. !dtd.$inlineWithA[ci.tagName] &&
  9208. i > 1
  9209. ) {
  9210. insertLine(arr, current, true)
  9211. insertIndent(arr, current)
  9212. }
  9213. nodeToHtml(ci, arr, formatter, current)
  9214. }
  9215. break
  9216. case 'text':
  9217. isText(node, arr)
  9218. break
  9219. case 'element':
  9220. isElement(node, arr, formatter, current)
  9221. break
  9222. case 'comment':
  9223. isComment(node, arr, formatter)
  9224. }
  9225. return arr
  9226. }
  9227. function isText(node, arr) {
  9228. if (node.parentNode.tagName == 'pre') {
  9229. //源码模式下输入html标签,不能做转换处理,直接输出
  9230. arr.push(node.data)
  9231. } else {
  9232. arr.push(
  9233. notTransTagName[node.parentNode.tagName]
  9234. ? utils.html(node.data)
  9235. : node.data.replace(/[ ]{2}/g, ' &nbsp;')
  9236. )
  9237. }
  9238. }
  9239. function isElement(node, arr, formatter, current) {
  9240. var attrhtml = ''
  9241. if (node.attrs) {
  9242. attrhtml = []
  9243. var attrs = node.attrs
  9244. for (var a in attrs) {
  9245. //这里就针对
  9246. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  9247. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  9248. //有可能做的不够
  9249. attrhtml.push(
  9250. a +
  9251. (attrs[a] !== undefined
  9252. ? '="' +
  9253. (notTransAttrs[a]
  9254. ? utils.html(attrs[a]).replace(/["]/g, function(a) {
  9255. return '&quot;'
  9256. })
  9257. : utils.unhtml(attrs[a])) +
  9258. '"'
  9259. : '')
  9260. )
  9261. }
  9262. attrhtml = attrhtml.join(' ')
  9263. }
  9264. arr.push(
  9265. '<' +
  9266. node.tagName +
  9267. (attrhtml ? ' ' + attrhtml : '') +
  9268. (dtd.$empty[node.tagName] ? '/' : '') +
  9269. '>'
  9270. )
  9271. //插入新行
  9272. if (
  9273. formatter &&
  9274. !dtd.$inlineWithA[node.tagName] &&
  9275. node.tagName != 'pre'
  9276. ) {
  9277. if (node.children && node.children.length) {
  9278. current = insertLine(arr, current, true)
  9279. insertIndent(arr, current)
  9280. }
  9281. }
  9282. if (node.children && node.children.length) {
  9283. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9284. if (
  9285. formatter &&
  9286. ci.type == 'element' &&
  9287. !dtd.$inlineWithA[ci.tagName] &&
  9288. i > 1
  9289. ) {
  9290. insertLine(arr, current)
  9291. insertIndent(arr, current)
  9292. }
  9293. nodeToHtml(ci, arr, formatter, current)
  9294. }
  9295. }
  9296. if (!dtd.$empty[node.tagName]) {
  9297. if (
  9298. formatter &&
  9299. !dtd.$inlineWithA[node.tagName] &&
  9300. node.tagName != 'pre'
  9301. ) {
  9302. if (node.children && node.children.length) {
  9303. current = insertLine(arr, current)
  9304. insertIndent(arr, current)
  9305. }
  9306. }
  9307. arr.push('</' + node.tagName + '>')
  9308. }
  9309. }
  9310. function isComment(node, arr) {
  9311. arr.push('<!--' + node.data + '-->')
  9312. }
  9313. function getNodeById(root, id) {
  9314. var node
  9315. if (root.type == 'element' && root.getAttr('id') == id) {
  9316. return root
  9317. }
  9318. if (root.children && root.children.length) {
  9319. for (var i = 0, ci; (ci = root.children[i++]); ) {
  9320. if ((node = getNodeById(ci, id))) {
  9321. return node
  9322. }
  9323. }
  9324. }
  9325. }
  9326. function getNodesByTagName(node, tagName, arr) {
  9327. if (node.type == 'element' && node.tagName == tagName) {
  9328. arr.push(node)
  9329. }
  9330. if (node.children && node.children.length) {
  9331. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9332. getNodesByTagName(ci, tagName, arr)
  9333. }
  9334. }
  9335. }
  9336. function nodeTraversal(root, fn) {
  9337. if (root.children && root.children.length) {
  9338. for (var i = 0, ci; (ci = root.children[i]); ) {
  9339. nodeTraversal(ci, fn)
  9340. //ci被替换的情况,这里就不再走 fn了
  9341. if (ci.parentNode) {
  9342. if (ci.children && ci.children.length) {
  9343. fn(ci)
  9344. }
  9345. if (ci.parentNode) i++
  9346. }
  9347. }
  9348. } else {
  9349. fn(root)
  9350. }
  9351. }
  9352. uNode.prototype = {
  9353. /**
  9354. * 当前节点对象,转换成html文本
  9355. * @method toHtml
  9356. * @return { String } 返回转换后的html字符串
  9357. * @example
  9358. * ```javascript
  9359. * node.toHtml();
  9360. * ```
  9361. */
  9362. /**
  9363. * 当前节点对象,转换成html文本
  9364. * @method toHtml
  9365. * @param { Boolean } formatter 是否格式化返回值
  9366. * @return { String } 返回转换后的html字符串
  9367. * @example
  9368. * ```javascript
  9369. * node.toHtml( true );
  9370. * ```
  9371. */
  9372. toHtml: function(formatter) {
  9373. var arr = []
  9374. nodeToHtml(this, arr, formatter, 0)
  9375. return arr.join('')
  9376. },
  9377. /**
  9378. * 获取节点的html内容
  9379. * @method innerHTML
  9380. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9381. * @return { String } 返回节点的html内容
  9382. * @example
  9383. * ```javascript
  9384. * var htmlstr = node.innerHTML();
  9385. * ```
  9386. */
  9387. /**
  9388. * 设置节点的html内容
  9389. * @method innerHTML
  9390. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9391. * @param { String } htmlstr 传入要设置的html内容
  9392. * @return { UE.uNode } 返回节点本身
  9393. * @example
  9394. * ```javascript
  9395. * node.innerHTML('<span>text</span>');
  9396. * ```
  9397. */
  9398. innerHTML: function(htmlstr) {
  9399. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9400. return this
  9401. }
  9402. if (utils.isString(htmlstr)) {
  9403. if (this.children) {
  9404. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9405. ci.parentNode = null
  9406. }
  9407. }
  9408. this.children = []
  9409. var tmpRoot = UE.htmlparser(htmlstr)
  9410. for (var i = 0, ci; (ci = tmpRoot.children[i++]); ) {
  9411. this.children.push(ci)
  9412. ci.parentNode = this
  9413. }
  9414. return this
  9415. } else {
  9416. var tmpRoot = new UE.uNode({
  9417. type: 'root',
  9418. children: this.children
  9419. })
  9420. return tmpRoot.toHtml()
  9421. }
  9422. },
  9423. /**
  9424. * 获取节点的纯文本内容
  9425. * @method innerText
  9426. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9427. * @return { String } 返回节点的存文本内容
  9428. * @example
  9429. * ```javascript
  9430. * var textStr = node.innerText();
  9431. * ```
  9432. */
  9433. /**
  9434. * 设置节点的纯文本内容
  9435. * @method innerText
  9436. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9437. * @param { String } textStr 传入要设置的文本内容
  9438. * @return { UE.uNode } 返回节点本身
  9439. * @example
  9440. * ```javascript
  9441. * node.innerText('<span>text</span>');
  9442. * ```
  9443. */
  9444. innerText: function(textStr, noTrans) {
  9445. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9446. return this
  9447. }
  9448. if (textStr) {
  9449. if (this.children) {
  9450. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9451. ci.parentNode = null
  9452. }
  9453. }
  9454. this.children = []
  9455. this.appendChild(uNode.createText(textStr, noTrans))
  9456. return this
  9457. } else {
  9458. return this.toHtml().replace(/<[^>]+>/g, '')
  9459. }
  9460. },
  9461. /**
  9462. * 获取当前对象的data属性
  9463. * @method getData
  9464. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  9465. * @example
  9466. * ```javascript
  9467. * node.getData();
  9468. * ```
  9469. */
  9470. getData: function() {
  9471. if (this.type == 'element') return ''
  9472. return this.data
  9473. },
  9474. /**
  9475. * 获取当前节点下的第一个子节点
  9476. * @method firstChild
  9477. * @return { UE.uNode } 返回第一个子节点
  9478. * @example
  9479. * ```javascript
  9480. * node.firstChild(); //返回第一个子节点
  9481. * ```
  9482. */
  9483. firstChild: function() {
  9484. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9485. // return this;
  9486. // }
  9487. return this.children ? this.children[0] : null
  9488. },
  9489. /**
  9490. * 获取当前节点下的最后一个子节点
  9491. * @method lastChild
  9492. * @return { UE.uNode } 返回最后一个子节点
  9493. * @example
  9494. * ```javascript
  9495. * node.lastChild(); //返回最后一个子节点
  9496. * ```
  9497. */
  9498. lastChild: function() {
  9499. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  9500. // return this;
  9501. // }
  9502. return this.children ? this.children[this.children.length - 1] : null
  9503. },
  9504. /**
  9505. * 获取和当前节点有相同父亲节点的前一个节点
  9506. * @method previousSibling
  9507. * @return { UE.uNode } 返回前一个节点
  9508. * @example
  9509. * ```javascript
  9510. * node.children[2].previousSibling(); //返回子节点node.children[1]
  9511. * ```
  9512. */
  9513. previousSibling: function() {
  9514. var parent = this.parentNode
  9515. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9516. if (ci === this) {
  9517. return i == 0 ? null : parent.children[i - 1]
  9518. }
  9519. }
  9520. },
  9521. /**
  9522. * 获取和当前节点有相同父亲节点的后一个节点
  9523. * @method nextSibling
  9524. * @return { UE.uNode } 返回后一个节点,找不到返回null
  9525. * @example
  9526. * ```javascript
  9527. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  9528. * ```
  9529. */
  9530. nextSibling: function() {
  9531. var parent = this.parentNode
  9532. for (var i = 0, ci; (ci = parent.children[i++]); ) {
  9533. if (ci === this) {
  9534. return parent.children[i]
  9535. }
  9536. }
  9537. },
  9538. /**
  9539. * 用新的节点替换当前节点
  9540. * @method replaceChild
  9541. * @param { UE.uNode } target 要替换成该节点参数
  9542. * @param { UE.uNode } source 要被替换掉的节点
  9543. * @return { UE.uNode } 返回替换之后的节点对象
  9544. * @example
  9545. * ```javascript
  9546. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  9547. * ```
  9548. */
  9549. replaceChild: function(target, source) {
  9550. if (this.children) {
  9551. if (target.parentNode) {
  9552. target.parentNode.removeChild(target)
  9553. }
  9554. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9555. if (ci === source) {
  9556. this.children.splice(i, 1, target)
  9557. source.parentNode = null
  9558. target.parentNode = this
  9559. return target
  9560. }
  9561. }
  9562. }
  9563. },
  9564. /**
  9565. * 在节点的子节点列表最后位置插入一个节点
  9566. * @method appendChild
  9567. * @param { UE.uNode } node 要插入的节点
  9568. * @return { UE.uNode } 返回刚插入的子节点
  9569. * @example
  9570. * ```javascript
  9571. * node.appendChild( newNode ); //在node内插入子节点newNode
  9572. * ```
  9573. */
  9574. appendChild: function(node) {
  9575. if (
  9576. this.type == 'root' ||
  9577. (this.type == 'element' && !dtd.$empty[this.tagName])
  9578. ) {
  9579. if (!this.children) {
  9580. this.children = []
  9581. }
  9582. if (node.parentNode) {
  9583. node.parentNode.removeChild(node)
  9584. }
  9585. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9586. if (ci === node) {
  9587. this.children.splice(i, 1)
  9588. break
  9589. }
  9590. }
  9591. this.children.push(node)
  9592. node.parentNode = this
  9593. return node
  9594. }
  9595. },
  9596. /**
  9597. * 在传入节点的前面插入一个节点
  9598. * @method insertBefore
  9599. * @param { UE.uNode } target 要插入的节点
  9600. * @param { UE.uNode } source 在该参数节点前面插入
  9601. * @return { UE.uNode } 返回刚插入的子节点
  9602. * @example
  9603. * ```javascript
  9604. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9605. * ```
  9606. */
  9607. insertBefore: function(target, source) {
  9608. if (this.children) {
  9609. if (target.parentNode) {
  9610. target.parentNode.removeChild(target)
  9611. }
  9612. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9613. if (ci === source) {
  9614. this.children.splice(i, 0, target)
  9615. target.parentNode = this
  9616. return target
  9617. }
  9618. }
  9619. }
  9620. },
  9621. /**
  9622. * 在传入节点的后面插入一个节点
  9623. * @method insertAfter
  9624. * @param { UE.uNode } target 要插入的节点
  9625. * @param { UE.uNode } source 在该参数节点后面插入
  9626. * @return { UE.uNode } 返回刚插入的子节点
  9627. * @example
  9628. * ```javascript
  9629. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9630. * ```
  9631. */
  9632. insertAfter: function(target, source) {
  9633. if (this.children) {
  9634. if (target.parentNode) {
  9635. target.parentNode.removeChild(target)
  9636. }
  9637. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9638. if (ci === source) {
  9639. this.children.splice(i + 1, 0, target)
  9640. target.parentNode = this
  9641. return target
  9642. }
  9643. }
  9644. }
  9645. },
  9646. /**
  9647. * 从当前节点的子节点列表中,移除节点
  9648. * @method removeChild
  9649. * @param { UE.uNode } node 要移除的节点引用
  9650. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9651. * @return { * } 返回刚移除的子节点
  9652. * @example
  9653. * ```javascript
  9654. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9655. * ```
  9656. */
  9657. removeChild: function(node, keepChildren) {
  9658. if (this.children) {
  9659. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9660. if (ci === node) {
  9661. this.children.splice(i, 1)
  9662. ci.parentNode = null
  9663. if (keepChildren && ci.children && ci.children.length) {
  9664. for (var j = 0, cj; (cj = ci.children[j]); j++) {
  9665. this.children.splice(i + j, 0, cj)
  9666. cj.parentNode = this
  9667. }
  9668. }
  9669. return ci
  9670. }
  9671. }
  9672. }
  9673. },
  9674. /**
  9675. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9676. * @method getAttr
  9677. * @param { String } attrName 要获取的属性名称
  9678. * @return { * } 返回attrs对象下的属性值
  9679. * @example
  9680. * ```javascript
  9681. * node.getAttr('title');
  9682. * ```
  9683. */
  9684. getAttr: function(attrName) {
  9685. return this.attrs && this.attrs[attrName.toLowerCase()]
  9686. },
  9687. /**
  9688. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9689. * @method setAttr
  9690. * @param { String } attrName 要设置的属性名称
  9691. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9692. * @return { * } 返回attrs对象下的属性值
  9693. * @example
  9694. * ```javascript
  9695. * node.setAttr('title','标题');
  9696. * ```
  9697. */
  9698. setAttr: function(attrName, attrVal) {
  9699. if (!attrName) {
  9700. delete this.attrs
  9701. return
  9702. }
  9703. if (!this.attrs) {
  9704. this.attrs = {}
  9705. }
  9706. if (utils.isObject(attrName)) {
  9707. for (var a in attrName) {
  9708. if (!attrName[a]) {
  9709. delete this.attrs[a]
  9710. } else {
  9711. this.attrs[a.toLowerCase()] = attrName[a]
  9712. }
  9713. }
  9714. } else {
  9715. if (!attrVal) {
  9716. delete this.attrs[attrName]
  9717. } else {
  9718. this.attrs[attrName.toLowerCase()] = attrVal
  9719. }
  9720. }
  9721. },
  9722. /**
  9723. * 获取当前节点在父节点下的位置索引
  9724. * @method getIndex
  9725. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9726. * @example
  9727. * ```javascript
  9728. * node.getIndex();
  9729. * ```
  9730. */
  9731. getIndex: function() {
  9732. var parent = this.parentNode
  9733. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9734. if (ci === this) {
  9735. return i
  9736. }
  9737. }
  9738. return -1
  9739. },
  9740. /**
  9741. * 在当前节点下,根据id查找节点
  9742. * @method getNodeById
  9743. * @param { String } id 要查找的id
  9744. * @return { UE.uNode } 返回找到的节点
  9745. * @example
  9746. * ```javascript
  9747. * node.getNodeById('textId');
  9748. * ```
  9749. */
  9750. getNodeById: function(id) {
  9751. var node
  9752. if (this.children && this.children.length) {
  9753. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9754. if ((node = getNodeById(ci, id))) {
  9755. return node
  9756. }
  9757. }
  9758. }
  9759. },
  9760. /**
  9761. * 在当前节点下,根据元素名称查找节点列表
  9762. * @method getNodesByTagName
  9763. * @param { String } tagNames 要查找的元素名称
  9764. * @return { Array } 返回找到的节点列表
  9765. * @example
  9766. * ```javascript
  9767. * node.getNodesByTagName('span');
  9768. * ```
  9769. */
  9770. getNodesByTagName: function(tagNames) {
  9771. tagNames = utils
  9772. .trim(tagNames)
  9773. .replace(/[ ]{2,}/g, ' ')
  9774. .split(' ')
  9775. var arr = [],
  9776. me = this
  9777. utils.each(tagNames, function(tagName) {
  9778. if (me.children && me.children.length) {
  9779. for (var i = 0, ci; (ci = me.children[i++]); ) {
  9780. getNodesByTagName(ci, tagName, arr)
  9781. }
  9782. }
  9783. })
  9784. return arr
  9785. },
  9786. /**
  9787. * 根据样式名称,获取节点的样式值
  9788. * @method getStyle
  9789. * @param { String } name 要获取的样式名称
  9790. * @return { String } 返回样式值
  9791. * @example
  9792. * ```javascript
  9793. * node.getStyle('font-size');
  9794. * ```
  9795. */
  9796. getStyle: function(name) {
  9797. var cssStyle = this.getAttr('style')
  9798. if (!cssStyle) {
  9799. return ''
  9800. }
  9801. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)', 'i')
  9802. var match = cssStyle.match(reg)
  9803. if (match && match[0]) {
  9804. return match[2]
  9805. }
  9806. return ''
  9807. },
  9808. /**
  9809. * 给节点设置样式
  9810. * @method setStyle
  9811. * @param { String } name 要设置的的样式名称
  9812. * @param { String } val 要设置的的样值
  9813. * @example
  9814. * ```javascript
  9815. * node.setStyle('font-size', '12px');
  9816. * ```
  9817. */
  9818. setStyle: function(name, val) {
  9819. function exec(name, val) {
  9820. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi')
  9821. cssStyle = cssStyle.replace(reg, '$1')
  9822. if (val) {
  9823. cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
  9824. }
  9825. }
  9826. var cssStyle = this.getAttr('style')
  9827. if (!cssStyle) {
  9828. cssStyle = ''
  9829. }
  9830. if (utils.isObject(name)) {
  9831. for (var a in name) {
  9832. exec(a, name[a])
  9833. }
  9834. } else {
  9835. exec(name, val)
  9836. }
  9837. this.setAttr('style', utils.trim(cssStyle))
  9838. },
  9839. /**
  9840. * 传入一个函数,递归遍历当前节点下的所有节点
  9841. * @method traversal
  9842. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9843. * @example
  9844. * ```javascript
  9845. * traversal(node, function(){
  9846. * console.log(node.type);
  9847. * });
  9848. * ```
  9849. */
  9850. traversal: function(fn) {
  9851. if (this.children && this.children.length) {
  9852. nodeTraversal(this, fn)
  9853. }
  9854. return this
  9855. }
  9856. }
  9857. })()
  9858. // core/htmlparser.js
  9859. /**
  9860. * html字符串转换成uNode节点
  9861. * @file
  9862. * @module UE
  9863. * @since 1.2.6.1
  9864. */
  9865. /**
  9866. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9867. * @unfile
  9868. * @module UE
  9869. */
  9870. /**
  9871. * html字符串转换成uNode节点的静态方法
  9872. * @method htmlparser
  9873. * @param { String } htmlstr 要转换的html代码
  9874. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9875. * @return { uNode } 给定的html片段转换形成的uNode对象
  9876. * @example
  9877. * ```javascript
  9878. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9879. * ```
  9880. */
  9881. var htmlparser = (UE.htmlparser = function(htmlstr, ignoreBlank) {
  9882. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9883. //先去掉了,加上的原因忘了,这里先记录
  9884. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9885. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g
  9886. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9887. var allowEmptyTags = {
  9888. b: 1,
  9889. code: 1,
  9890. i: 1,
  9891. u: 1,
  9892. strike: 1,
  9893. s: 1,
  9894. tt: 1,
  9895. strong: 1,
  9896. q: 1,
  9897. samp: 1,
  9898. em: 1,
  9899. span: 1,
  9900. sub: 1,
  9901. img: 1,
  9902. sup: 1,
  9903. font: 1,
  9904. big: 1,
  9905. small: 1,
  9906. iframe: 1,
  9907. a: 1,
  9908. br: 1,
  9909. pre: 1
  9910. }
  9911. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '')
  9912. if (!ignoreBlank) {
  9913. htmlstr = htmlstr.replace(
  9914. new RegExp(
  9915. '[\\r\\t\\n' +
  9916. (ignoreBlank ? '' : ' ') +
  9917. ']*</?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n' +
  9918. (ignoreBlank ? '' : ' ') +
  9919. ']*',
  9920. 'g'
  9921. ),
  9922. function(a, b) {
  9923. //br暂时单独处理
  9924. if (b && allowEmptyTags[b.toLowerCase()]) {
  9925. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, '')
  9926. }
  9927. return a
  9928. .replace(
  9929. new RegExp('^[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+'),
  9930. ''
  9931. )
  9932. .replace(
  9933. new RegExp('[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+$'),
  9934. ''
  9935. )
  9936. }
  9937. )
  9938. }
  9939. var notTransAttrs = {
  9940. href: 1,
  9941. src: 1
  9942. }
  9943. var uNode = UE.uNode,
  9944. needParentNode = {
  9945. td: 'tr',
  9946. tr: ['tbody', 'thead', 'tfoot'],
  9947. tbody: 'table',
  9948. th: 'tr',
  9949. thead: 'table',
  9950. tfoot: 'table',
  9951. caption: 'table',
  9952. li: ['ul', 'ol'],
  9953. dt: 'dl',
  9954. dd: 'dl',
  9955. option: 'select'
  9956. },
  9957. needChild = {
  9958. ol: 'li',
  9959. ul: 'li'
  9960. }
  9961. function text(parent, data) {
  9962. if (needChild[parent.tagName]) {
  9963. var tmpNode = uNode.createElement(needChild[parent.tagName])
  9964. parent.appendChild(tmpNode)
  9965. tmpNode.appendChild(uNode.createText(data))
  9966. parent = tmpNode
  9967. } else {
  9968. parent.appendChild(uNode.createText(data))
  9969. }
  9970. }
  9971. function element(parent, tagName, htmlattr) {
  9972. var needParentTag
  9973. if ((needParentTag = needParentNode[tagName])) {
  9974. var tmpParent = parent,
  9975. hasParent
  9976. while (tmpParent.type != 'root') {
  9977. if (
  9978. utils.isArray(needParentTag)
  9979. ? utils.indexOf(needParentTag, tmpParent.tagName) != -1
  9980. : needParentTag == tmpParent.tagName
  9981. ) {
  9982. parent = tmpParent
  9983. hasParent = true
  9984. break
  9985. }
  9986. tmpParent = tmpParent.parentNode
  9987. }
  9988. if (!hasParent) {
  9989. parent = element(
  9990. parent,
  9991. utils.isArray(needParentTag) ? needParentTag[0] : needParentTag
  9992. )
  9993. }
  9994. }
  9995. //按dtd处理嵌套
  9996. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  9997. // parent = parent.parentNode;
  9998. var elm = new uNode({
  9999. parentNode: parent,
  10000. type: 'element',
  10001. tagName: tagName.toLowerCase(),
  10002. //是自闭合的处理一下
  10003. children: dtd.$empty[tagName] ? null : []
  10004. })
  10005. //如果属性存在,处理属性
  10006. if (htmlattr) {
  10007. var attrs = {},
  10008. match
  10009. while ((match = re_attr.exec(htmlattr))) {
  10010. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()]
  10011. ? match[2] || match[3] || match[4]
  10012. : utils.unhtml(match[2] || match[3] || match[4])
  10013. }
  10014. elm.attrs = attrs
  10015. }
  10016. //trace:3970
  10017. // //如果parent下不能放elm
  10018. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  10019. // parent = parent.parentNode;
  10020. // elm.parentNode = parent;
  10021. // }
  10022. parent.children.push(elm)
  10023. //如果是自闭合节点返回父亲节点
  10024. return dtd.$empty[tagName] ? parent : elm
  10025. }
  10026. function comment(parent, data) {
  10027. parent.children.push(
  10028. new uNode({
  10029. type: 'comment',
  10030. data: data,
  10031. parentNode: parent
  10032. })
  10033. )
  10034. }
  10035. var match,
  10036. currentIndex = 0,
  10037. nextIndex = 0
  10038. //设置根节点
  10039. var root = new uNode({
  10040. type: 'root',
  10041. children: []
  10042. })
  10043. var currentParent = root
  10044. while ((match = re_tag.exec(htmlstr))) {
  10045. currentIndex = match.index
  10046. try {
  10047. if (currentIndex > nextIndex) {
  10048. //text node
  10049. text(currentParent, htmlstr.slice(nextIndex, currentIndex))
  10050. }
  10051. if (match[3]) {
  10052. if (dtd.$cdata[currentParent.tagName]) {
  10053. text(currentParent, match[0])
  10054. } else {
  10055. //start tag
  10056. currentParent = element(
  10057. currentParent,
  10058. match[3].toLowerCase(),
  10059. match[4]
  10060. )
  10061. }
  10062. } else if (match[1]) {
  10063. if (currentParent.type != 'root') {
  10064. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  10065. text(currentParent, match[0])
  10066. } else {
  10067. var tmpParent = currentParent
  10068. while (
  10069. currentParent.type == 'element' &&
  10070. currentParent.tagName != match[1].toLowerCase()
  10071. ) {
  10072. currentParent = currentParent.parentNode
  10073. if (currentParent.type == 'root') {
  10074. currentParent = tmpParent
  10075. throw 'break'
  10076. }
  10077. }
  10078. //end tag
  10079. currentParent = currentParent.parentNode
  10080. }
  10081. }
  10082. } else if (match[2]) {
  10083. //comment
  10084. comment(currentParent, match[2])
  10085. }
  10086. } catch (e) {}
  10087. nextIndex = re_tag.lastIndex
  10088. }
  10089. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  10090. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  10091. if (nextIndex < htmlstr.length) {
  10092. text(currentParent, htmlstr.slice(nextIndex))
  10093. }
  10094. return root
  10095. })
  10096. // core/filternode.js
  10097. /**
  10098. * UE过滤节点的静态方法
  10099. * @file
  10100. */
  10101. /**
  10102. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  10103. * @module UE
  10104. */
  10105. /**
  10106. * 根据传入节点和过滤规则过滤相应节点
  10107. * @module UE
  10108. * @since 1.2.6.1
  10109. * @method filterNode
  10110. * @param { Object } root 指定root节点
  10111. * @param { Object } rules 过滤规则json对象
  10112. * @example
  10113. * ```javascript
  10114. * UE.filterNode(root,editor.options.filterRules);
  10115. * ```
  10116. */
  10117. var filterNode = (UE.filterNode = (function() {
  10118. function filterNode(node, rules) {
  10119. switch (node.type) {
  10120. case 'text':
  10121. break
  10122. case 'element':
  10123. var val
  10124. if ((val = rules[node.tagName])) {
  10125. if (val === '-') {
  10126. node.parentNode.removeChild(node)
  10127. } else if (utils.isFunction(val)) {
  10128. var parentNode = node.parentNode,
  10129. index = node.getIndex()
  10130. val(node)
  10131. if (node.parentNode) {
  10132. if (node.children) {
  10133. for (var i = 0, ci; (ci = node.children[i]); ) {
  10134. filterNode(ci, rules)
  10135. if (ci.parentNode) {
  10136. i++
  10137. }
  10138. }
  10139. }
  10140. } else {
  10141. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10142. filterNode(ci, rules)
  10143. if (ci.parentNode) {
  10144. i++
  10145. }
  10146. }
  10147. }
  10148. } else {
  10149. var attrs = val['$']
  10150. if (attrs && node.attrs) {
  10151. var tmpAttrs = {},
  10152. tmpVal
  10153. for (var a in attrs) {
  10154. tmpVal = node.getAttr(a)
  10155. //todo 只先对style单独处理
  10156. if (a == 'style' && utils.isArray(attrs[a])) {
  10157. var tmpCssStyle = []
  10158. utils.each(attrs[a], function(v) {
  10159. var tmp
  10160. if ((tmp = node.getStyle(v))) {
  10161. tmpCssStyle.push(v + ':' + tmp)
  10162. }
  10163. })
  10164. tmpVal = tmpCssStyle.join(';')
  10165. }
  10166. if (tmpVal) {
  10167. tmpAttrs[a] = tmpVal
  10168. }
  10169. }
  10170. node.attrs = tmpAttrs
  10171. }
  10172. if (node.children) {
  10173. for (var i = 0, ci; (ci = node.children[i]); ) {
  10174. filterNode(ci, rules)
  10175. if (ci.parentNode) {
  10176. i++
  10177. }
  10178. }
  10179. }
  10180. }
  10181. } else {
  10182. //如果不在名单里扣出子节点并删除该节点,cdata除外
  10183. if (dtd.$cdata[node.tagName]) {
  10184. node.parentNode.removeChild(node)
  10185. } else {
  10186. var parentNode = node.parentNode,
  10187. index = node.getIndex()
  10188. node.parentNode.removeChild(node, true)
  10189. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10190. filterNode(ci, rules)
  10191. if (ci.parentNode) {
  10192. i++
  10193. }
  10194. }
  10195. }
  10196. }
  10197. break
  10198. case 'comment':
  10199. node.parentNode.removeChild(node)
  10200. }
  10201. }
  10202. return function(root, rules) {
  10203. if (utils.isEmptyObject(rules)) {
  10204. return root
  10205. }
  10206. var val
  10207. if ((val = rules['-'])) {
  10208. utils.each(val.split(' '), function(k) {
  10209. rules[k] = '-'
  10210. })
  10211. }
  10212. for (var i = 0, ci; (ci = root.children[i]); ) {
  10213. filterNode(ci, rules)
  10214. if (ci.parentNode) {
  10215. i++
  10216. }
  10217. }
  10218. return root
  10219. }
  10220. })())
  10221. // core/plugin.js
  10222. /**
  10223. * Created with JetBrains PhpStorm.
  10224. * User: campaign
  10225. * Date: 10/8/13
  10226. * Time: 6:15 PM
  10227. * To change this template use File | Settings | File Templates.
  10228. */
  10229. UE.plugin = (function() {
  10230. var _plugins = {}
  10231. return {
  10232. register: function(pluginName, fn, oldOptionName, afterDisabled) {
  10233. if (oldOptionName && utils.isFunction(oldOptionName)) {
  10234. afterDisabled = oldOptionName
  10235. oldOptionName = null
  10236. }
  10237. _plugins[pluginName] = {
  10238. optionName: oldOptionName || pluginName,
  10239. execFn: fn,
  10240. //当插件被禁用时执行
  10241. afterDisabled: afterDisabled
  10242. }
  10243. },
  10244. load: function(editor) {
  10245. utils.each(_plugins, function(plugin) {
  10246. var _export = plugin.execFn.call(editor)
  10247. if (editor.options[plugin.optionName] !== false) {
  10248. if (_export) {
  10249. //后边需要再做扩展
  10250. utils.each(_export, function(v, k) {
  10251. switch (k.toLowerCase()) {
  10252. case 'shortcutkey':
  10253. editor.addshortcutkey(v)
  10254. break
  10255. case 'bindevents':
  10256. utils.each(v, function(fn, eventName) {
  10257. editor.addListener(eventName, fn)
  10258. })
  10259. break
  10260. case 'bindmultievents':
  10261. utils.each(utils.isArray(v) ? v : [v], function(event) {
  10262. var types = utils.trim(event.type).split(/\s+/)
  10263. utils.each(types, function(eventName) {
  10264. editor.addListener(eventName, event.handler)
  10265. })
  10266. })
  10267. break
  10268. case 'commands':
  10269. utils.each(v, function(execFn, execName) {
  10270. editor.commands[execName] = execFn
  10271. })
  10272. break
  10273. case 'outputrule':
  10274. editor.addOutputRule(v)
  10275. break
  10276. case 'inputrule':
  10277. editor.addInputRule(v)
  10278. break
  10279. case 'defaultoptions':
  10280. editor.setOpt(v)
  10281. }
  10282. })
  10283. }
  10284. } else if (plugin.afterDisabled) {
  10285. plugin.afterDisabled.call(editor)
  10286. }
  10287. })
  10288. //向下兼容
  10289. utils.each(UE.plugins, function(plugin) {
  10290. plugin.call(editor)
  10291. })
  10292. },
  10293. run: function(pluginName, editor) {
  10294. var plugin = _plugins[pluginName]
  10295. if (plugin) {
  10296. plugin.exeFn.call(editor)
  10297. }
  10298. }
  10299. }
  10300. })()
  10301. // core/keymap.js
  10302. var keymap = (UE.keymap = {
  10303. Backspace: 8,
  10304. Tab: 9,
  10305. Enter: 13,
  10306. Shift: 16,
  10307. Control: 17,
  10308. Alt: 18,
  10309. CapsLock: 20,
  10310. Esc: 27,
  10311. Spacebar: 32,
  10312. PageUp: 33,
  10313. PageDown: 34,
  10314. End: 35,
  10315. Home: 36,
  10316. Left: 37,
  10317. Up: 38,
  10318. Right: 39,
  10319. Down: 40,
  10320. Insert: 45,
  10321. Del: 46,
  10322. NumLock: 144,
  10323. Cmd: 91,
  10324. '=': 187,
  10325. '-': 189,
  10326. b: 66,
  10327. i: 73,
  10328. //回退
  10329. z: 90,
  10330. y: 89,
  10331. //粘贴
  10332. v: 86,
  10333. x: 88,
  10334. s: 83,
  10335. n: 78
  10336. })
  10337. // core/localstorage.js
  10338. //存储媒介封装
  10339. var LocalStorage = (UE.LocalStorage = (function() {
  10340. var storage = window.localStorage || getUserData() || null,
  10341. LOCAL_FILE = 'localStorage'
  10342. return {
  10343. saveLocalData: function(key, data) {
  10344. if (storage && data) {
  10345. storage.setItem(key, data)
  10346. return true
  10347. }
  10348. return false
  10349. },
  10350. getLocalData: function(key) {
  10351. if (storage) {
  10352. return storage.getItem(key)
  10353. }
  10354. return null
  10355. },
  10356. removeItem: function(key) {
  10357. storage && storage.removeItem(key)
  10358. }
  10359. }
  10360. function getUserData() {
  10361. var container = document.createElement('div')
  10362. container.style.display = 'none'
  10363. if (!container.addBehavior) {
  10364. return null
  10365. }
  10366. container.addBehavior('#default#userdata')
  10367. return {
  10368. getItem: function(key) {
  10369. var result = null
  10370. try {
  10371. document.body.appendChild(container)
  10372. container.load(LOCAL_FILE)
  10373. result = container.getAttribute(key)
  10374. document.body.removeChild(container)
  10375. } catch (e) {}
  10376. return result
  10377. },
  10378. setItem: function(key, value) {
  10379. document.body.appendChild(container)
  10380. container.setAttribute(key, value)
  10381. container.save(LOCAL_FILE)
  10382. document.body.removeChild(container)
  10383. },
  10384. //// 暂时没有用到
  10385. //clear: function () {
  10386. //
  10387. // var expiresTime = new Date();
  10388. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  10389. // document.body.appendChild(container);
  10390. // container.expires = expiresTime.toUTCString();
  10391. // container.save(LOCAL_FILE);
  10392. // document.body.removeChild(container);
  10393. //
  10394. //},
  10395. removeItem: function(key) {
  10396. document.body.appendChild(container)
  10397. container.removeAttribute(key)
  10398. container.save(LOCAL_FILE)
  10399. document.body.removeChild(container)
  10400. }
  10401. }
  10402. }
  10403. })())
  10404. ;(function() {
  10405. var ROOTKEY = 'ueditor_preference'
  10406. UE.Editor.prototype.setPreferences = function(key, value) {
  10407. var obj = {}
  10408. if (utils.isString(key)) {
  10409. obj[key] = value
  10410. } else {
  10411. obj = key
  10412. }
  10413. var data = LocalStorage.getLocalData(ROOTKEY)
  10414. if (data && (data = utils.str2json(data))) {
  10415. utils.extend(data, obj)
  10416. } else {
  10417. data = obj
  10418. }
  10419. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data))
  10420. }
  10421. UE.Editor.prototype.getPreferences = function(key) {
  10422. var data = LocalStorage.getLocalData(ROOTKEY)
  10423. if (data && (data = utils.str2json(data))) {
  10424. return key ? data[key] : data
  10425. }
  10426. return null
  10427. }
  10428. UE.Editor.prototype.removePreferences = function(key) {
  10429. var data = LocalStorage.getLocalData(ROOTKEY)
  10430. if (data && (data = utils.str2json(data))) {
  10431. data[key] = undefined
  10432. delete data[key]
  10433. }
  10434. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data))
  10435. }
  10436. })()
  10437. // plugins/defaultfilter.js
  10438. ///import core
  10439. ///plugin 编辑器默认的过滤转换机制
  10440. UE.plugins['defaultfilter'] = function() {
  10441. var me = this
  10442. me.setOpt({
  10443. allowDivTransToP: true,
  10444. disabledTableInTable: true
  10445. })
  10446. //默认的过滤处理
  10447. //进入编辑器的内容处理
  10448. me.addInputRule(function(root) {
  10449. var allowDivTransToP = this.options.allowDivTransToP
  10450. var val
  10451. function tdParent(node) {
  10452. while (node && node.type == 'element') {
  10453. if (node.tagName == 'td') {
  10454. return true
  10455. }
  10456. node = node.parentNode
  10457. }
  10458. return false
  10459. }
  10460. //进行默认的处理
  10461. root.traversal(function(node) {
  10462. if (node.type == 'element') {
  10463. if (
  10464. !dtd.$cdata[node.tagName] &&
  10465. me.options.autoClearEmptyNode &&
  10466. dtd.$inline[node.tagName] &&
  10467. !dtd.$empty[node.tagName] &&
  10468. (!node.attrs || utils.isEmptyObject(node.attrs))
  10469. ) {
  10470. if (!node.firstChild()) node.parentNode.removeChild(node)
  10471. else if (
  10472. node.tagName == 'span' &&
  10473. (!node.attrs || utils.isEmptyObject(node.attrs))
  10474. ) {
  10475. node.parentNode.removeChild(node, true)
  10476. }
  10477. return
  10478. }
  10479. switch (node.tagName) {
  10480. case 'style':
  10481. case 'script':
  10482. node.setAttr({
  10483. cdata_tag: node.tagName,
  10484. cdata_data: node.innerHTML() || '',
  10485. _ue_custom_node_: 'true'
  10486. })
  10487. node.tagName = 'div'
  10488. node.innerHTML('')
  10489. break
  10490. case 'a':
  10491. if ((val = node.getAttr('href'))) {
  10492. node.setAttr('_href', val)
  10493. }
  10494. break
  10495. case 'img':
  10496. //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  10497. if ((val = node.getAttr('src'))) {
  10498. if (/^data:/.test(val)) {
  10499. node.parentNode.removeChild(node)
  10500. break
  10501. }
  10502. }
  10503. node.setAttr('_src', node.getAttr('src'))
  10504. node.setAttr('src', '')
  10505. break
  10506. case 'span':
  10507. if (browser.webkit && (val = node.getStyle('white-space'))) {
  10508. if (/nowrap|normal/.test(val)) {
  10509. node.setStyle('white-space', '')
  10510. if (
  10511. me.options.autoClearEmptyNode &&
  10512. utils.isEmptyObject(node.attrs)
  10513. ) {
  10514. node.parentNode.removeChild(node, true)
  10515. }
  10516. }
  10517. }
  10518. val = node.getAttr('id')
  10519. if (val && /^_baidu_bookmark_/i.test(val)) {
  10520. node.parentNode.removeChild(node)
  10521. }
  10522. break
  10523. case 'p':
  10524. if ((val = node.getAttr('align'))) {
  10525. node.setAttr('align')
  10526. node.setStyle('text-align', val)
  10527. }
  10528. //trace:3431
  10529. // var cssStyle = node.getAttr('style');
  10530. // if (cssStyle) {
  10531. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  10532. // node.setAttr('style', cssStyle)
  10533. //
  10534. // }
  10535. //p标签不允许嵌套
  10536. utils.each(node.children, function(n) {
  10537. if (n.type == 'element' && n.tagName == 'p') {
  10538. var next = n.nextSibling()
  10539. node.parentNode.insertAfter(n, node)
  10540. var last = n
  10541. while (next) {
  10542. var tmp = next.nextSibling()
  10543. node.parentNode.insertAfter(next, last)
  10544. last = next
  10545. next = tmp
  10546. }
  10547. return false
  10548. }
  10549. })
  10550. if (!node.firstChild()) {
  10551. node.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  10552. }
  10553. break
  10554. case 'div':
  10555. if (node.getAttr('cdata_tag')) {
  10556. break
  10557. }
  10558. //针对代码这里不处理插入代码的div
  10559. val = node.getAttr('class')
  10560. if (val && /^line number\d+/.test(val)) {
  10561. break
  10562. }
  10563. if (!allowDivTransToP) {
  10564. break
  10565. }
  10566. var tmpNode,
  10567. p = UE.uNode.createElement('p')
  10568. while ((tmpNode = node.firstChild())) {
  10569. if (
  10570. tmpNode.type == 'text' ||
  10571. !UE.dom.dtd.$block[tmpNode.tagName]
  10572. ) {
  10573. p.appendChild(tmpNode)
  10574. } else {
  10575. if (p.firstChild()) {
  10576. node.parentNode.insertBefore(p, node)
  10577. p = UE.uNode.createElement('p')
  10578. } else {
  10579. node.parentNode.insertBefore(tmpNode, node)
  10580. }
  10581. }
  10582. }
  10583. if (p.firstChild()) {
  10584. node.parentNode.insertBefore(p, node)
  10585. }
  10586. node.parentNode.removeChild(node)
  10587. break
  10588. case 'dl':
  10589. node.tagName = 'ul'
  10590. break
  10591. case 'dt':
  10592. case 'dd':
  10593. node.tagName = 'li'
  10594. break
  10595. case 'li':
  10596. var className = node.getAttr('class')
  10597. if (!className || !/list\-/.test(className)) {
  10598. node.setAttr()
  10599. }
  10600. var tmpNodes = node.getNodesByTagName('ol ul')
  10601. UE.utils.each(tmpNodes, function(n) {
  10602. node.parentNode.insertAfter(n, node)
  10603. })
  10604. break
  10605. case 'td':
  10606. case 'th':
  10607. case 'caption':
  10608. if (!node.children || !node.children.length) {
  10609. node.appendChild(
  10610. browser.ie11below
  10611. ? UE.uNode.createText(' ')
  10612. : UE.uNode.createElement('br')
  10613. )
  10614. }
  10615. break
  10616. case 'table':
  10617. if (me.options.disabledTableInTable && tdParent(node)) {
  10618. node.parentNode.insertBefore(
  10619. UE.uNode.createText(node.innerText()),
  10620. node
  10621. )
  10622. node.parentNode.removeChild(node)
  10623. }
  10624. }
  10625. }
  10626. // if(node.type == 'comment'){
  10627. // node.parentNode.removeChild(node);
  10628. // }
  10629. })
  10630. })
  10631. //从编辑器出去的内容处理
  10632. me.addOutputRule(function(root) {
  10633. var val
  10634. var fileReg = /^.*?\/downloadFile\?fileId=([^&]+).*?$/,
  10635. hrefReg = /^javascript:(\w+);$/
  10636. root.traversal(function(node) {
  10637. if (node.type == 'element') {
  10638. if (
  10639. me.options.autoClearEmptyNode &&
  10640. dtd.$inline[node.tagName] &&
  10641. !dtd.$empty[node.tagName] &&
  10642. (!node.attrs || utils.isEmptyObject(node.attrs))
  10643. ) {
  10644. if (!node.firstChild()) node.parentNode.removeChild(node)
  10645. else if (
  10646. node.tagName == 'span' &&
  10647. (!node.attrs || utils.isEmptyObject(node.attrs))
  10648. ) {
  10649. node.parentNode.removeChild(node, true)
  10650. }
  10651. return
  10652. }
  10653. switch (node.tagName) {
  10654. case 'div':
  10655. if ((val = node.getAttr('cdata_tag'))) {
  10656. node.tagName = val
  10657. node.appendChild(
  10658. UE.uNode.createText(node.getAttr('cdata_data'))
  10659. )
  10660. node.setAttr({
  10661. cdata_tag: '',
  10662. cdata_data: '',
  10663. _ue_custom_node_: ''
  10664. })
  10665. }
  10666. break
  10667. case 'a':
  10668. if ((val = node.getAttr('_href'))) {
  10669. node.setAttr({
  10670. href: utils.html(val),
  10671. _href: ''
  10672. })
  10673. }
  10674. if (fileReg.test(node.getAttr('href'))) {
  10675. var hrefVal = node.getAttr('href'),
  10676. match = fileReg.exec(hrefVal)
  10677. utils.each(
  10678. domUtils.getElementsByTagName(me.body, 'a'),
  10679. function(a) {
  10680. if (a && a.href == hrefVal) {
  10681. a.href = hrefVal
  10682. }
  10683. }
  10684. )
  10685. }
  10686. if (hrefReg.test(node.getAttr('href'))) {
  10687. var hrefVal = node.getAttr('href'),
  10688. match = hrefReg.exec(hrefVal)
  10689. utils.each(
  10690. domUtils.getElementsByTagName(me.body, 'a'),
  10691. function(a) {
  10692. if (a && a.href == hrefVal) {
  10693. a.onclick = e => {
  10694. handlerPreviewFile(match[1])
  10695. // 阻止a标签的默认行为
  10696. e.preventDefault()
  10697. }
  10698. }
  10699. }
  10700. )
  10701. }
  10702. break
  10703. case 'span':
  10704. val = node.getAttr('id')
  10705. if (val && /^_baidu_bookmark_/i.test(val)) {
  10706. node.parentNode.removeChild(node)
  10707. }
  10708. break
  10709. case 'img':
  10710. if ((val = node.getAttr('_src'))) {
  10711. utils.each(
  10712. domUtils.getElementsByTagName(me.body, 'img'),
  10713. function(img) {
  10714. if (
  10715. img &&
  10716. !domUtils.hasClass(img, 'loadingclass') &&
  10717. !img.hasAttribute('loaded') &&
  10718. img.getAttribute('_src') == val
  10719. ) {
  10720. img.setAttribute('loaded', 'true')
  10721. // 1.如果是用平台上传的图片,则构建请求来加载
  10722. if (fileReg.test(val)) {
  10723. const request = new XMLHttpRequest()
  10724. request.responseType = 'blob'
  10725. request.open('get', val, true)
  10726. request.setRequestHeader(
  10727. 'Authorization',
  10728. getAuthorization()
  10729. )
  10730. request.onreadystatechange = () => {
  10731. if (
  10732. request.readyState == XMLHttpRequest.DONE &&
  10733. request.status == 200
  10734. ) {
  10735. img.src = URL.createObjectURL(request.response)
  10736. img.onload = () => {
  10737. // 触发内容更新事件来更新高度
  10738. me.fireEvent('contentchange')
  10739. URL.revokeObjectURL(img.src)
  10740. }
  10741. }
  10742. }
  10743. request.send(null)
  10744. } else {
  10745. // 2.否则直接加载
  10746. img.src = val
  10747. }
  10748. }
  10749. }
  10750. )
  10751. node.setAttr({
  10752. src: node.getAttr('_src'),
  10753. _src: ''
  10754. })
  10755. }
  10756. }
  10757. }
  10758. })
  10759. })
  10760. }
  10761. // plugins/inserthtml.js
  10762. /**
  10763. * 插入html字符串插件
  10764. * @file
  10765. * @since 1.2.6.1
  10766. */
  10767. /**
  10768. * 插入html代码
  10769. * @command inserthtml
  10770. * @method execCommand
  10771. * @param { String } cmd 命令字符串
  10772. * @param { String } html 插入的html字符串
  10773. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10774. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10775. * @example
  10776. * ```javascript
  10777. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10778. * //执行命令,插入<b>CC</b>
  10779. * //插入后的效果 xxx<b>CC</b>xxx
  10780. * //<p>xx|xxx</p> 当前选区为闭合状态
  10781. * //插入<p>CC</p>
  10782. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10783. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10784. * //插入 xxxx
  10785. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10786. * ```
  10787. */
  10788. UE.commands['inserthtml'] = {
  10789. execCommand: function(command, html, notNeedFilter) {
  10790. var me = this,
  10791. range,
  10792. div
  10793. if (!html) {
  10794. return
  10795. }
  10796. if (me.fireEvent('beforeinserthtml', html) === true) {
  10797. return
  10798. }
  10799. range = me.selection.getRange()
  10800. div = range.document.createElement('div')
  10801. div.style.display = 'inline'
  10802. if (!notNeedFilter) {
  10803. var root = UE.htmlparser(html)
  10804. //如果给了过滤规则就先进行过滤
  10805. if (me.options.filterRules) {
  10806. UE.filterNode(root, me.options.filterRules)
  10807. }
  10808. //执行默认的处理
  10809. me.filterInputRule(root)
  10810. html = root.toHtml()
  10811. }
  10812. div.innerHTML = utils.trim(html)
  10813. if (!range.collapsed) {
  10814. var tmpNode = range.startContainer
  10815. if (domUtils.isFillChar(tmpNode)) {
  10816. range.setStartBefore(tmpNode)
  10817. }
  10818. tmpNode = range.endContainer
  10819. if (domUtils.isFillChar(tmpNode)) {
  10820. range.setEndAfter(tmpNode)
  10821. }
  10822. range.txtToElmBoundary()
  10823. //结束边界可能放到了br的前边,要把br包含进来
  10824. // x[xxx]<br/>
  10825. if (range.endContainer && range.endContainer.nodeType == 1) {
  10826. tmpNode = range.endContainer.childNodes[range.endOffset]
  10827. if (tmpNode && domUtils.isBr(tmpNode)) {
  10828. range.setEndAfter(tmpNode)
  10829. }
  10830. }
  10831. if (range.startOffset == 0) {
  10832. tmpNode = range.startContainer
  10833. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  10834. tmpNode = range.endContainer
  10835. if (
  10836. range.endOffset ==
  10837. (tmpNode.nodeType == 3
  10838. ? tmpNode.nodeValue.length
  10839. : tmpNode.childNodes.length) &&
  10840. domUtils.isBoundaryNode(tmpNode, 'lastChild')
  10841. ) {
  10842. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  10843. range.setStart(me.body.firstChild, 0).collapse(true)
  10844. }
  10845. }
  10846. }
  10847. !range.collapsed && range.deleteContents()
  10848. if (range.startContainer.nodeType == 1) {
  10849. var child = range.startContainer.childNodes[range.startOffset],
  10850. pre
  10851. if (
  10852. child &&
  10853. domUtils.isBlockElm(child) &&
  10854. (pre = child.previousSibling) &&
  10855. domUtils.isBlockElm(pre)
  10856. ) {
  10857. range.setEnd(pre, pre.childNodes.length).collapse()
  10858. while (child.firstChild) {
  10859. pre.appendChild(child.firstChild)
  10860. }
  10861. domUtils.remove(child)
  10862. }
  10863. }
  10864. }
  10865. var child,
  10866. parent,
  10867. pre,
  10868. tmp,
  10869. hadBreak = 0,
  10870. nextNode
  10871. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10872. if (range.inFillChar()) {
  10873. child = range.startContainer
  10874. if (domUtils.isFillChar(child)) {
  10875. range.setStartBefore(child).collapse(true)
  10876. domUtils.remove(child)
  10877. } else if (domUtils.isFillChar(child, true)) {
  10878. child.nodeValue = child.nodeValue.replace(fillCharReg, '')
  10879. range.startOffset--
  10880. range.collapsed && range.collapse(true)
  10881. }
  10882. }
  10883. //列表单独处理
  10884. var li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  10885. if (li) {
  10886. var next, last
  10887. while ((child = div.firstChild)) {
  10888. //针对hr单独处理一下先
  10889. while (
  10890. child &&
  10891. (child.nodeType == 3 ||
  10892. !domUtils.isBlockElm(child) ||
  10893. child.tagName == 'HR')
  10894. ) {
  10895. next = child.nextSibling
  10896. range.insertNode(child).collapse()
  10897. last = child
  10898. child = next
  10899. }
  10900. if (child) {
  10901. if (/^(ol|ul)$/i.test(child.tagName)) {
  10902. while (child.firstChild) {
  10903. last = child.firstChild
  10904. domUtils.insertAfter(li, child.firstChild)
  10905. li = li.nextSibling
  10906. }
  10907. domUtils.remove(child)
  10908. } else {
  10909. var tmpLi
  10910. next = child.nextSibling
  10911. tmpLi = me.document.createElement('li')
  10912. domUtils.insertAfter(li, tmpLi)
  10913. tmpLi.appendChild(child)
  10914. last = child
  10915. child = next
  10916. li = tmpLi
  10917. }
  10918. }
  10919. }
  10920. li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  10921. if (domUtils.isEmptyBlock(li)) {
  10922. domUtils.remove(li)
  10923. }
  10924. if (last) {
  10925. range
  10926. .setStartAfter(last)
  10927. .collapse(true)
  10928. .select(true)
  10929. }
  10930. } else {
  10931. while ((child = div.firstChild)) {
  10932. if (hadBreak) {
  10933. var p = me.document.createElement('p')
  10934. while (
  10935. child &&
  10936. (child.nodeType == 3 || !dtd.$block[child.tagName])
  10937. ) {
  10938. nextNode = child.nextSibling
  10939. p.appendChild(child)
  10940. child = nextNode
  10941. }
  10942. if (p.firstChild) {
  10943. child = p
  10944. }
  10945. }
  10946. range.insertNode(child)
  10947. nextNode = child.nextSibling
  10948. if (
  10949. !hadBreak &&
  10950. child.nodeType == domUtils.NODE_ELEMENT &&
  10951. domUtils.isBlockElm(child)
  10952. ) {
  10953. parent = domUtils.findParent(child, function(node) {
  10954. return domUtils.isBlockElm(node)
  10955. })
  10956. if (
  10957. parent &&
  10958. parent.tagName.toLowerCase() != 'body' &&
  10959. !(
  10960. dtd[parent.tagName][child.nodeName] &&
  10961. child.parentNode === parent
  10962. )
  10963. ) {
  10964. if (!dtd[parent.tagName][child.nodeName]) {
  10965. pre = parent
  10966. } else {
  10967. tmp = child.parentNode
  10968. while (tmp !== parent) {
  10969. pre = tmp
  10970. tmp = tmp.parentNode
  10971. }
  10972. }
  10973. domUtils.breakParent(child, pre || tmp)
  10974. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10975. var pre = child.previousSibling
  10976. domUtils.trimWhiteTextNode(pre)
  10977. if (!pre.childNodes.length) {
  10978. domUtils.remove(pre)
  10979. }
  10980. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10981. if (
  10982. !browser.ie &&
  10983. (next = child.nextSibling) &&
  10984. domUtils.isBlockElm(next) &&
  10985. next.lastChild &&
  10986. !domUtils.isBr(next.lastChild)
  10987. ) {
  10988. next.appendChild(me.document.createElement('br'))
  10989. }
  10990. hadBreak = 1
  10991. }
  10992. }
  10993. var next = child.nextSibling
  10994. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10995. range.setStart(next, 0).collapse(true)
  10996. break
  10997. }
  10998. range.setEndAfter(child).collapse()
  10999. }
  11000. child = range.startContainer
  11001. if (nextNode && domUtils.isBr(nextNode)) {
  11002. domUtils.remove(nextNode)
  11003. }
  11004. //用chrome可能有空白展位符
  11005. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  11006. if ((nextNode = child.nextSibling)) {
  11007. domUtils.remove(child)
  11008. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  11009. range
  11010. .setStart(nextNode, 0)
  11011. .collapse(true)
  11012. .shrinkBoundary()
  11013. }
  11014. } else {
  11015. try {
  11016. child.innerHTML = browser.ie ? domUtils.fillChar : '<br/>'
  11017. } catch (e) {
  11018. range.setStartBefore(child)
  11019. domUtils.remove(child)
  11020. }
  11021. }
  11022. }
  11023. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  11024. try {
  11025. range.select(true)
  11026. } catch (e) {}
  11027. }
  11028. setTimeout(function() {
  11029. range = me.selection.getRange()
  11030. range.scrollToView(
  11031. me.autoHeightEnabled,
  11032. me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0
  11033. )
  11034. me.fireEvent('afterinserthtml', html)
  11035. }, 200)
  11036. }
  11037. }
  11038. // plugins/autotypeset.js
  11039. /**
  11040. * 自动排版
  11041. * @file
  11042. * @since 1.2.6.1
  11043. */
  11044. /**
  11045. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  11046. * @command autotypeset
  11047. * @method execCommand
  11048. * @param { String } cmd 命令字符串
  11049. * @example
  11050. * ```javascript
  11051. * editor.execCommand( 'autotypeset' );
  11052. * ```
  11053. */
  11054. UE.plugins['autotypeset'] = function() {
  11055. this.setOpt({
  11056. autotypeset: {
  11057. mergeEmptyline: true, //合并空行
  11058. removeClass: true, //去掉冗余的class
  11059. removeEmptyline: false, //去掉空行
  11060. textAlign: 'left', //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  11061. imageBlockLine: 'center', //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  11062. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  11063. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  11064. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  11065. removeEmptyNode: false, // 去掉空节点
  11066. //可以去掉的标签
  11067. removeTagNames: utils.extend({div: 1}, dtd.$removeEmpty),
  11068. indent: false, // 行首缩进
  11069. indentValue: '2em', //行首缩进的大小
  11070. bdc2sb: false,
  11071. tobdc: false
  11072. }
  11073. })
  11074. var me = this,
  11075. opt = me.options.autotypeset,
  11076. remainClass = {
  11077. selectTdClass: 1,
  11078. pagebreak: 1,
  11079. anchorclass: 1
  11080. },
  11081. remainTag = {
  11082. li: 1
  11083. },
  11084. tags = {
  11085. div: 1,
  11086. p: 1,
  11087. //trace:2183 这些也认为是行
  11088. blockquote: 1,
  11089. center: 1,
  11090. h1: 1,
  11091. h2: 1,
  11092. h3: 1,
  11093. h4: 1,
  11094. h5: 1,
  11095. h6: 1,
  11096. span: 1
  11097. },
  11098. highlightCont
  11099. //升级了版本,但配置项目里没有autotypeset
  11100. if (!opt) {
  11101. return
  11102. }
  11103. readLocalOpts()
  11104. function isLine(node, notEmpty) {
  11105. if (!node || node.nodeType == 3) return 0
  11106. if (domUtils.isBr(node)) return 1
  11107. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  11108. if (
  11109. (highlightCont && highlightCont.contains(node)) ||
  11110. node.getAttribute('pagebreak')
  11111. ) {
  11112. return 0
  11113. }
  11114. return notEmpty
  11115. ? !domUtils.isEmptyBlock(node)
  11116. : domUtils.isEmptyBlock(
  11117. node,
  11118. new RegExp('[\\s' + domUtils.fillChar + ']', 'g')
  11119. )
  11120. }
  11121. }
  11122. function removeNotAttributeSpan(node) {
  11123. if (!node.style.cssText) {
  11124. domUtils.removeAttributes(node, ['style'])
  11125. if (
  11126. node.tagName.toLowerCase() == 'span' &&
  11127. domUtils.hasNoAttributes(node)
  11128. ) {
  11129. domUtils.remove(node, true)
  11130. }
  11131. }
  11132. }
  11133. function autotype(type, html) {
  11134. var me = this,
  11135. cont
  11136. if (html) {
  11137. if (!opt.pasteFilter) {
  11138. return
  11139. }
  11140. cont = me.document.createElement('div')
  11141. cont.innerHTML = html.html
  11142. } else {
  11143. cont = me.document.body
  11144. }
  11145. var nodes = domUtils.getElementsByTagName(cont, '*')
  11146. // 行首缩进,段落方向,段间距,段内间距
  11147. for (var i = 0, ci; (ci = nodes[i++]); ) {
  11148. if (me.fireEvent('excludeNodeinautotype', ci) === true) {
  11149. continue
  11150. }
  11151. //font-size
  11152. if (opt.clearFontSize && ci.style.fontSize) {
  11153. domUtils.removeStyle(ci, 'font-size')
  11154. removeNotAttributeSpan(ci)
  11155. }
  11156. //font-family
  11157. if (opt.clearFontFamily && ci.style.fontFamily) {
  11158. domUtils.removeStyle(ci, 'font-family')
  11159. removeNotAttributeSpan(ci)
  11160. }
  11161. if (isLine(ci)) {
  11162. //合并空行
  11163. if (opt.mergeEmptyline) {
  11164. var next = ci.nextSibling,
  11165. tmpNode,
  11166. isBr = domUtils.isBr(ci)
  11167. while (isLine(next)) {
  11168. tmpNode = next
  11169. next = tmpNode.nextSibling
  11170. if (isBr && (!next || (next && !domUtils.isBr(next)))) {
  11171. break
  11172. }
  11173. domUtils.remove(tmpNode)
  11174. }
  11175. }
  11176. //去掉空行,保留占位的空行
  11177. if (
  11178. opt.removeEmptyline &&
  11179. domUtils.inDoc(ci, cont) &&
  11180. !remainTag[ci.parentNode.tagName.toLowerCase()]
  11181. ) {
  11182. if (domUtils.isBr(ci)) {
  11183. next = ci.nextSibling
  11184. if (next && !domUtils.isBr(next)) {
  11185. continue
  11186. }
  11187. }
  11188. domUtils.remove(ci)
  11189. continue
  11190. }
  11191. }
  11192. if (isLine(ci, true) && ci.tagName != 'SPAN') {
  11193. if (opt.indent) {
  11194. ci.style.textIndent = opt.indentValue
  11195. }
  11196. if (opt.textAlign) {
  11197. ci.style.textAlign = opt.textAlign
  11198. }
  11199. // if(opt.lineHeight)
  11200. // ci.style.lineHeight = opt.lineHeight + 'cm';
  11201. }
  11202. //去掉class,保留的class不去掉
  11203. if (
  11204. opt.removeClass &&
  11205. ci.className &&
  11206. !remainClass[ci.className.toLowerCase()]
  11207. ) {
  11208. if (highlightCont && highlightCont.contains(ci)) {
  11209. continue
  11210. }
  11211. domUtils.removeAttributes(ci, ['class'])
  11212. }
  11213. //表情不处理
  11214. if (
  11215. opt.imageBlockLine &&
  11216. ci.tagName.toLowerCase() == 'img' &&
  11217. !ci.getAttribute('emotion')
  11218. ) {
  11219. if (html) {
  11220. var img = ci
  11221. switch (opt.imageBlockLine) {
  11222. case 'left':
  11223. case 'right':
  11224. case 'none':
  11225. var pN = img.parentNode,
  11226. tmpNode,
  11227. pre,
  11228. next
  11229. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  11230. pN = pN.parentNode
  11231. }
  11232. tmpNode = pN
  11233. if (
  11234. tmpNode.tagName == 'P' &&
  11235. domUtils.getStyle(tmpNode, 'text-align') == 'center'
  11236. ) {
  11237. if (
  11238. !domUtils.isBody(tmpNode) &&
  11239. domUtils.getChildCount(tmpNode, function(node) {
  11240. return (
  11241. !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11242. )
  11243. }) == 1
  11244. ) {
  11245. pre = tmpNode.previousSibling
  11246. next = tmpNode.nextSibling
  11247. if (
  11248. pre &&
  11249. next &&
  11250. pre.nodeType == 1 &&
  11251. next.nodeType == 1 &&
  11252. pre.tagName == next.tagName &&
  11253. domUtils.isBlockElm(pre)
  11254. ) {
  11255. pre.appendChild(tmpNode.firstChild)
  11256. while (next.firstChild) {
  11257. pre.appendChild(next.firstChild)
  11258. }
  11259. domUtils.remove(tmpNode)
  11260. domUtils.remove(next)
  11261. } else {
  11262. domUtils.setStyle(tmpNode, 'text-align', '')
  11263. }
  11264. }
  11265. }
  11266. domUtils.setStyle(img, 'float', opt.imageBlockLine)
  11267. break
  11268. case 'center':
  11269. if (me.queryCommandValue('imagefloat') != 'center') {
  11270. pN = img.parentNode
  11271. domUtils.setStyle(img, 'float', 'none')
  11272. tmpNode = img
  11273. while (
  11274. pN &&
  11275. domUtils.getChildCount(pN, function(node) {
  11276. return (
  11277. !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11278. )
  11279. }) == 1 &&
  11280. (dtd.$inline[pN.tagName] || pN.tagName == 'A')
  11281. ) {
  11282. tmpNode = pN
  11283. pN = pN.parentNode
  11284. }
  11285. var pNode = me.document.createElement('p')
  11286. domUtils.setAttributes(pNode, {
  11287. style: 'text-align:center'
  11288. })
  11289. tmpNode.parentNode.insertBefore(pNode, tmpNode)
  11290. pNode.appendChild(tmpNode)
  11291. domUtils.setStyle(tmpNode, 'float', '')
  11292. }
  11293. }
  11294. } else {
  11295. var range = me.selection.getRange()
  11296. range.selectNode(ci).select()
  11297. me.execCommand('imagefloat', opt.imageBlockLine)
  11298. }
  11299. }
  11300. //去掉冗余的标签
  11301. if (opt.removeEmptyNode) {
  11302. if (
  11303. opt.removeTagNames[ci.tagName.toLowerCase()] &&
  11304. domUtils.hasNoAttributes(ci) &&
  11305. domUtils.isEmptyBlock(ci)
  11306. ) {
  11307. domUtils.remove(ci)
  11308. }
  11309. }
  11310. }
  11311. if (opt.tobdc) {
  11312. var root = UE.htmlparser(cont.innerHTML)
  11313. root.traversal(function(node) {
  11314. if (node.type == 'text') {
  11315. node.data = ToDBC(node.data)
  11316. }
  11317. })
  11318. cont.innerHTML = root.toHtml()
  11319. }
  11320. if (opt.bdc2sb) {
  11321. var root = UE.htmlparser(cont.innerHTML)
  11322. root.traversal(function(node) {
  11323. if (node.type == 'text') {
  11324. node.data = DBC2SB(node.data)
  11325. }
  11326. })
  11327. cont.innerHTML = root.toHtml()
  11328. }
  11329. if (html) {
  11330. html.html = cont.innerHTML
  11331. }
  11332. }
  11333. if (opt.pasteFilter) {
  11334. me.addListener('beforepaste', autotype)
  11335. }
  11336. function DBC2SB(str) {
  11337. var result = ''
  11338. for (var i = 0; i < str.length; i++) {
  11339. var code = str.charCodeAt(i) //获取当前字符的unicode编码
  11340. if (code >= 65281 && code <= 65373) {
  11341. //在这个unicode编码范围中的是所有的英文字母已经各种字符
  11342. result += String.fromCharCode(str.charCodeAt(i) - 65248) //把全角字符的unicode编码转换为对应半角字符的unicode码
  11343. } else if (code == 12288) {
  11344. //空格
  11345. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32)
  11346. } else {
  11347. result += str.charAt(i)
  11348. }
  11349. }
  11350. return result
  11351. }
  11352. function ToDBC(txtstring) {
  11353. txtstring = utils.html(txtstring)
  11354. var tmp = ''
  11355. var mark = '' /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  11356. for (var i = 0; i < txtstring.length; i++) {
  11357. if (txtstring.charCodeAt(i) == 32) {
  11358. tmp = tmp + String.fromCharCode(12288)
  11359. } else if (txtstring.charCodeAt(i) < 127) {
  11360. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248)
  11361. } else {
  11362. tmp += txtstring.charAt(i)
  11363. }
  11364. }
  11365. return tmp
  11366. }
  11367. function readLocalOpts() {
  11368. var cookieOpt = me.getPreferences('autotypeset')
  11369. utils.extend(me.options.autotypeset, cookieOpt)
  11370. }
  11371. me.commands['autotypeset'] = {
  11372. execCommand: function() {
  11373. me.removeListener('beforepaste', autotype)
  11374. if (opt.pasteFilter) {
  11375. me.addListener('beforepaste', autotype)
  11376. }
  11377. autotype.call(me)
  11378. }
  11379. }
  11380. }
  11381. // plugins/autosubmit.js
  11382. /**
  11383. * 快捷键提交
  11384. * @file
  11385. * @since 1.2.6.1
  11386. */
  11387. /**
  11388. * 提交表单
  11389. * @command autosubmit
  11390. * @method execCommand
  11391. * @param { String } cmd 命令字符串
  11392. * @example
  11393. * ```javascript
  11394. * editor.execCommand( 'autosubmit' );
  11395. * ```
  11396. */
  11397. UE.plugin.register('autosubmit', function() {
  11398. return {
  11399. shortcutkey: {
  11400. autosubmit: 'ctrl+13' //手动提交
  11401. },
  11402. commands: {
  11403. autosubmit: {
  11404. execCommand: function() {
  11405. var me = this,
  11406. form = domUtils.findParentByTagName(me.iframe, 'form', false)
  11407. if (form) {
  11408. if (me.fireEvent('beforesubmit') === false) {
  11409. return
  11410. }
  11411. me.sync()
  11412. form.submit()
  11413. }
  11414. }
  11415. }
  11416. }
  11417. }
  11418. })
  11419. // plugins/background.js
  11420. /**
  11421. * 背景插件,为UEditor提供设置背景功能
  11422. * @file
  11423. * @since 1.2.6.1
  11424. */
  11425. UE.plugin.register('background', function() {
  11426. var me = this,
  11427. cssRuleId = 'editor_background',
  11428. isSetColored,
  11429. reg = new RegExp('body[\\s]*\\{(.+)\\}', 'i')
  11430. function stringToObj(str) {
  11431. var obj = {},
  11432. styles = str.split(';')
  11433. utils.each(styles, function(v) {
  11434. var index = v.indexOf(':'),
  11435. key = utils.trim(v.substr(0, index)).toLowerCase()
  11436. key && (obj[key] = utils.trim(v.substr(index + 1) || ''))
  11437. })
  11438. return obj
  11439. }
  11440. function setBackground(obj) {
  11441. if (obj) {
  11442. var styles = []
  11443. for (var name in obj) {
  11444. if (obj.hasOwnProperty(name)) {
  11445. styles.push(name + ':' + obj[name] + '; ')
  11446. }
  11447. }
  11448. utils.cssRule(
  11449. cssRuleId,
  11450. styles.length ? 'body{' + styles.join('') + '}' : '',
  11451. me.document
  11452. )
  11453. } else {
  11454. utils.cssRule(cssRuleId, '', me.document)
  11455. }
  11456. }
  11457. //重写editor.hasContent方法
  11458. var orgFn = me.hasContents
  11459. me.hasContents = function() {
  11460. if (me.queryCommandValue('background')) {
  11461. return true
  11462. }
  11463. return orgFn.apply(me, arguments)
  11464. }
  11465. return {
  11466. bindEvents: {
  11467. getAllHtml: function(type, headHtml) {
  11468. var body = this.body,
  11469. su = domUtils.getComputedStyle(body, 'background-image'),
  11470. url = ''
  11471. if (su.indexOf(me.options.imagePath) > 0) {
  11472. url = su
  11473. .substring(su.indexOf(me.options.imagePath), su.length - 1)
  11474. .replace(/"|\(|\)/gi, '')
  11475. } else {
  11476. url = su != 'none' ? su.replace(/url\("?|"?\)/gi, '') : ''
  11477. }
  11478. var html = '<style type="text/css">body{'
  11479. var bgObj = {
  11480. 'background-color':
  11481. domUtils.getComputedStyle(body, 'background-color') || '#ffffff',
  11482. 'background-image': url ? 'url(' + url + ')' : '',
  11483. 'background-repeat':
  11484. domUtils.getComputedStyle(body, 'background-repeat') || '',
  11485. 'background-position': browser.ie
  11486. ? domUtils.getComputedStyle(body, 'background-position-x') +
  11487. ' ' +
  11488. domUtils.getComputedStyle(body, 'background-position-y')
  11489. : domUtils.getComputedStyle(body, 'background-position'),
  11490. height: domUtils.getComputedStyle(body, 'height')
  11491. }
  11492. for (var name in bgObj) {
  11493. if (bgObj.hasOwnProperty(name)) {
  11494. html += name + ':' + bgObj[name] + '; '
  11495. }
  11496. }
  11497. html += '}</style> '
  11498. headHtml.push(html)
  11499. },
  11500. aftersetcontent: function() {
  11501. if (isSetColored == false) setBackground()
  11502. }
  11503. },
  11504. inputRule: function(root) {
  11505. isSetColored = false
  11506. utils.each(root.getNodesByTagName('p'), function(p) {
  11507. var styles = p.getAttr('data-background')
  11508. if (styles) {
  11509. isSetColored = true
  11510. setBackground(stringToObj(styles))
  11511. p.parentNode.removeChild(p)
  11512. }
  11513. })
  11514. },
  11515. outputRule: function(root) {
  11516. var me = this,
  11517. styles = (utils.cssRule(cssRuleId, me.document) || '')
  11518. .replace(/[\n\r]+/g, '')
  11519. .match(reg)
  11520. if (styles) {
  11521. root.appendChild(
  11522. UE.uNode.createElement(
  11523. '<p style="display:none;" data-background="' +
  11524. utils.trim(styles[1].replace(/"/g, '').replace(/[\s]+/g, ' ')) +
  11525. '"><br/></p>'
  11526. )
  11527. )
  11528. }
  11529. },
  11530. commands: {
  11531. background: {
  11532. execCommand: function(cmd, obj) {
  11533. setBackground(obj)
  11534. },
  11535. queryCommandValue: function() {
  11536. var me = this,
  11537. styles = (utils.cssRule(cssRuleId, me.document) || '')
  11538. .replace(/[\n\r]+/g, '')
  11539. .match(reg)
  11540. return styles ? stringToObj(styles[1]) : null
  11541. },
  11542. notNeedUndo: true
  11543. }
  11544. }
  11545. }
  11546. })
  11547. // plugins/image.js
  11548. /**
  11549. * 图片插入、排版插件
  11550. * @file
  11551. * @since 1.2.6.1
  11552. */
  11553. /**
  11554. * 图片对齐方式
  11555. * @command imagefloat
  11556. * @method execCommand
  11557. * @remind 值center为独占一行居中
  11558. * @param { String } cmd 命令字符串
  11559. * @param { String } align 对齐方式,可传left、right、none、center
  11560. * @remaind center表示图片独占一行
  11561. * @example
  11562. * ```javascript
  11563. * editor.execCommand( 'imagefloat', 'center' );
  11564. * ```
  11565. */
  11566. /**
  11567. * 如果选区所在位置是图片区域
  11568. * @command imagefloat
  11569. * @method queryCommandValue
  11570. * @param { String } cmd 命令字符串
  11571. * @return { String } 返回图片对齐方式
  11572. * @example
  11573. * ```javascript
  11574. * editor.queryCommandValue( 'imagefloat' );
  11575. * ```
  11576. */
  11577. UE.commands['imagefloat'] = {
  11578. execCommand: function(cmd, align) {
  11579. var me = this,
  11580. range = me.selection.getRange()
  11581. if (!range.collapsed) {
  11582. var img = range.getClosedNode()
  11583. if (img && img.tagName == 'IMG') {
  11584. switch (align) {
  11585. case 'left':
  11586. case 'right':
  11587. case 'none':
  11588. var pN = img.parentNode,
  11589. tmpNode,
  11590. pre,
  11591. next
  11592. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  11593. pN = pN.parentNode
  11594. }
  11595. tmpNode = pN
  11596. if (
  11597. tmpNode.tagName == 'P' &&
  11598. domUtils.getStyle(tmpNode, 'text-align') == 'center'
  11599. ) {
  11600. if (
  11601. !domUtils.isBody(tmpNode) &&
  11602. domUtils.getChildCount(tmpNode, function(node) {
  11603. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11604. }) == 1
  11605. ) {
  11606. pre = tmpNode.previousSibling
  11607. next = tmpNode.nextSibling
  11608. if (
  11609. pre &&
  11610. next &&
  11611. pre.nodeType == 1 &&
  11612. next.nodeType == 1 &&
  11613. pre.tagName == next.tagName &&
  11614. domUtils.isBlockElm(pre)
  11615. ) {
  11616. pre.appendChild(tmpNode.firstChild)
  11617. while (next.firstChild) {
  11618. pre.appendChild(next.firstChild)
  11619. }
  11620. domUtils.remove(tmpNode)
  11621. domUtils.remove(next)
  11622. } else {
  11623. domUtils.setStyle(tmpNode, 'text-align', '')
  11624. }
  11625. }
  11626. range.selectNode(img).select()
  11627. }
  11628. domUtils.setStyle(img, 'float', align == 'none' ? '' : align)
  11629. if (align == 'none') {
  11630. domUtils.removeAttributes(img, 'align')
  11631. }
  11632. break
  11633. case 'center':
  11634. if (me.queryCommandValue('imagefloat') != 'center') {
  11635. pN = img.parentNode
  11636. domUtils.setStyle(img, 'float', '')
  11637. domUtils.removeAttributes(img, 'align')
  11638. tmpNode = img
  11639. while (
  11640. pN &&
  11641. domUtils.getChildCount(pN, function(node) {
  11642. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11643. }) == 1 &&
  11644. (dtd.$inline[pN.tagName] || pN.tagName == 'A')
  11645. ) {
  11646. tmpNode = pN
  11647. pN = pN.parentNode
  11648. }
  11649. range.setStartBefore(tmpNode).setCursor(false)
  11650. pN = me.document.createElement('div')
  11651. pN.appendChild(tmpNode)
  11652. domUtils.setStyle(tmpNode, 'float', '')
  11653. me.execCommand(
  11654. 'insertHtml',
  11655. '<p id="_img_parent_tmp" style="text-align:center">' +
  11656. pN.innerHTML +
  11657. '</p>'
  11658. )
  11659. tmpNode = me.document.getElementById('_img_parent_tmp')
  11660. tmpNode.removeAttribute('id')
  11661. tmpNode = tmpNode.firstChild
  11662. range.selectNode(tmpNode).select()
  11663. //去掉后边多余的元素
  11664. next = tmpNode.parentNode.nextSibling
  11665. if (next && domUtils.isEmptyNode(next)) {
  11666. domUtils.remove(next)
  11667. }
  11668. }
  11669. break
  11670. }
  11671. }
  11672. }
  11673. },
  11674. queryCommandValue: function() {
  11675. var range = this.selection.getRange(),
  11676. startNode,
  11677. floatStyle
  11678. if (range.collapsed) {
  11679. return 'none'
  11680. }
  11681. startNode = range.getClosedNode()
  11682. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  11683. floatStyle =
  11684. domUtils.getComputedStyle(startNode, 'float') ||
  11685. startNode.getAttribute('align')
  11686. if (floatStyle == 'none') {
  11687. floatStyle =
  11688. domUtils.getComputedStyle(startNode.parentNode, 'text-align') ==
  11689. 'center'
  11690. ? 'center'
  11691. : floatStyle
  11692. }
  11693. return {
  11694. left: 1,
  11695. right: 1,
  11696. center: 1
  11697. }[floatStyle]
  11698. ? floatStyle
  11699. : 'none'
  11700. }
  11701. return 'none'
  11702. },
  11703. queryCommandState: function() {
  11704. var range = this.selection.getRange(),
  11705. startNode
  11706. if (range.collapsed) return -1
  11707. startNode = range.getClosedNode()
  11708. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  11709. return 0
  11710. }
  11711. return -1
  11712. }
  11713. }
  11714. /**
  11715. * 插入图片
  11716. * @command insertimage
  11717. * @method execCommand
  11718. * @param { String } cmd 命令字符串
  11719. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  11720. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  11721. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  11722. * @example
  11723. * ```javascript
  11724. * editor.execCommand( 'insertimage', {
  11725. * src:'a/b/c.jpg',
  11726. * width:'100',
  11727. * height:'100'
  11728. * } );
  11729. * ```
  11730. * @example
  11731. * ```javascript
  11732. * editor.execCommand( 'insertimage', [{
  11733. * src:'a/b/c.jpg',
  11734. * width:'100',
  11735. * height:'100'
  11736. * },{
  11737. * src:'a/b/d.jpg',
  11738. * width:'100',
  11739. * height:'100'
  11740. * }] );
  11741. * ```
  11742. */
  11743. UE.commands['insertimage'] = {
  11744. execCommand: function(cmd, opt) {
  11745. opt = utils.isArray(opt) ? opt : [opt]
  11746. if (!opt.length) {
  11747. return
  11748. }
  11749. var me = this,
  11750. range = me.selection.getRange(),
  11751. img = range.getClosedNode()
  11752. if (me.fireEvent('beforeinsertimage', opt) === true) {
  11753. return
  11754. }
  11755. function unhtmlData(imgCi) {
  11756. utils.each('width,height,border,hspace,vspace'.split(','), function(
  11757. item
  11758. ) {
  11759. if (imgCi[item]) {
  11760. imgCi[item] = parseInt(imgCi[item], 10) || 0
  11761. }
  11762. })
  11763. utils.each('src,_src'.split(','), function(item) {
  11764. if (imgCi[item]) {
  11765. imgCi[item] = utils.unhtmlForUrl(imgCi[item])
  11766. }
  11767. })
  11768. utils.each('title,alt'.split(','), function(item) {
  11769. if (imgCi[item]) {
  11770. imgCi[item] = utils.unhtml(imgCi[item])
  11771. }
  11772. })
  11773. }
  11774. if (
  11775. img &&
  11776. /img/i.test(img.tagName) &&
  11777. (img.className != 'edui-faked-video' ||
  11778. img.className.indexOf('edui-upload-video') != -1) &&
  11779. !img.getAttribute('word_img')
  11780. ) {
  11781. var first = opt.shift()
  11782. var floatStyle = first['floatStyle']
  11783. delete first['floatStyle']
  11784. //// img.style.border = (first.border||0) +"px solid #000";
  11785. //// img.style.margin = (first.margin||0) +"px";
  11786. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  11787. domUtils.setAttributes(img, first)
  11788. me.execCommand('imagefloat', floatStyle)
  11789. if (opt.length > 0) {
  11790. range.setStartAfter(img).setCursor(false, true)
  11791. me.execCommand('insertimage', opt)
  11792. }
  11793. } else {
  11794. var html = [],
  11795. str = '',
  11796. ci
  11797. ci = opt[0]
  11798. if (opt.length == 1) {
  11799. unhtmlData(ci)
  11800. str =
  11801. '<img src="' +
  11802. ci.src +
  11803. '" ' +
  11804. (ci._src ? ' _src="' + ci._src + '" ' : '') +
  11805. (ci.width ? 'width="' + ci.width + '" ' : '') +
  11806. (ci.height ? ' height="' + ci.height + '" ' : '') +
  11807. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right'
  11808. ? ' style="float:' + ci['floatStyle'] + ';"'
  11809. : '') +
  11810. (ci.title && ci.title != '' ? ' title="' + ci.title + '"' : '') +
  11811. (ci.border && ci.border != '0'
  11812. ? ' border="' + ci.border + '"'
  11813. : '') +
  11814. (ci.alt && ci.alt != '' ? ' alt="' + ci.alt + '"' : '') +
  11815. (ci.hspace && ci.hspace != '0'
  11816. ? ' hspace = "' + ci.hspace + '"'
  11817. : '') +
  11818. (ci.vspace && ci.vspace != '0'
  11819. ? ' vspace = "' + ci.vspace + '"'
  11820. : '') +
  11821. '/>'
  11822. if (ci['floatStyle'] == 'center') {
  11823. str = '<p style="text-align: center">' + str + '</p>'
  11824. }
  11825. html.push(str)
  11826. } else {
  11827. for (var i = 0; (ci = opt[i++]); ) {
  11828. unhtmlData(ci)
  11829. str =
  11830. '<p ' +
  11831. (ci['floatStyle'] == 'center'
  11832. ? 'style="text-align: center" '
  11833. : '') +
  11834. '><img src="' +
  11835. ci.src +
  11836. '" ' +
  11837. (ci.width ? 'width="' + ci.width + '" ' : '') +
  11838. (ci._src ? ' _src="' + ci._src + '" ' : '') +
  11839. (ci.height ? ' height="' + ci.height + '" ' : '') +
  11840. ' style="' +
  11841. (ci['floatStyle'] && ci['floatStyle'] != 'center'
  11842. ? 'float:' + ci['floatStyle'] + ';'
  11843. : '') +
  11844. (ci.border || '') +
  11845. '" ' +
  11846. (ci.title ? ' title="' + ci.title + '"' : '') +
  11847. ' /></p>'
  11848. html.push(str)
  11849. }
  11850. }
  11851. me.execCommand('insertHtml', html.join(''))
  11852. }
  11853. me.fireEvent('afterinsertimage', opt)
  11854. }
  11855. }
  11856. // plugins/justify.js
  11857. /**
  11858. * 段落格式
  11859. * @file
  11860. * @since 1.2.6.1
  11861. */
  11862. /**
  11863. * 段落对齐方式
  11864. * @command justify
  11865. * @method execCommand
  11866. * @param { String } cmd 命令字符串
  11867. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  11868. * @example
  11869. * ```javascript
  11870. * editor.execCommand( 'justify', 'center' );
  11871. * ```
  11872. */
  11873. /**
  11874. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  11875. * @command justify
  11876. * @method queryCommandValue
  11877. * @param { String } cmd 命令字符串
  11878. * @return { String } 返回段落对齐方式
  11879. * @example
  11880. * ```javascript
  11881. * editor.queryCommandValue( 'justify' );
  11882. * ```
  11883. */
  11884. UE.plugins['justify'] = function() {
  11885. var me = this,
  11886. block = domUtils.isBlockElm,
  11887. defaultValue = {
  11888. left: 1,
  11889. right: 1,
  11890. center: 1,
  11891. justify: 1
  11892. },
  11893. doJustify = function(range, style) {
  11894. var bookmark = range.createBookmark(),
  11895. filterFn = function(node) {
  11896. return node.nodeType == 1
  11897. ? node.tagName.toLowerCase() != 'br' &&
  11898. !domUtils.isBookmarkNode(node)
  11899. : !domUtils.isWhitespace(node)
  11900. }
  11901. range.enlarge(true)
  11902. var bookmark2 = range.createBookmark(),
  11903. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  11904. tmpRange = range.cloneRange(),
  11905. tmpNode
  11906. while (
  11907. current &&
  11908. !(
  11909. domUtils.getPosition(current, bookmark2.end) &
  11910. domUtils.POSITION_FOLLOWING
  11911. )
  11912. ) {
  11913. if (current.nodeType == 3 || !block(current)) {
  11914. tmpRange.setStartBefore(current)
  11915. while (current && current !== bookmark2.end && !block(current)) {
  11916. tmpNode = current
  11917. current = domUtils.getNextDomNode(current, false, null, function(
  11918. node
  11919. ) {
  11920. return !block(node)
  11921. })
  11922. }
  11923. tmpRange.setEndAfter(tmpNode)
  11924. var common = tmpRange.getCommonAncestor()
  11925. if (!domUtils.isBody(common) && block(common)) {
  11926. domUtils.setStyles(
  11927. common,
  11928. utils.isString(style) ? {'text-align': style} : style
  11929. )
  11930. current = common
  11931. } else {
  11932. var p = range.document.createElement('p')
  11933. domUtils.setStyles(
  11934. p,
  11935. utils.isString(style) ? {'text-align': style} : style
  11936. )
  11937. var frag = tmpRange.extractContents()
  11938. p.appendChild(frag)
  11939. tmpRange.insertNode(p)
  11940. current = p
  11941. }
  11942. current = domUtils.getNextDomNode(current, false, filterFn)
  11943. } else {
  11944. current = domUtils.getNextDomNode(current, true, filterFn)
  11945. }
  11946. }
  11947. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  11948. }
  11949. UE.commands['justify'] = {
  11950. execCommand: function(cmdName, align) {
  11951. var range = this.selection.getRange(),
  11952. txt
  11953. //闭合时单独处理
  11954. if (range.collapsed) {
  11955. txt = this.document.createTextNode('p')
  11956. range.insertNode(txt)
  11957. }
  11958. doJustify(range, align)
  11959. if (txt) {
  11960. range.setStartBefore(txt).collapse(true)
  11961. domUtils.remove(txt)
  11962. }
  11963. range.select()
  11964. return true
  11965. },
  11966. queryCommandValue: function() {
  11967. var startNode = this.selection.getStart(),
  11968. value = domUtils.getComputedStyle(startNode, 'text-align')
  11969. return defaultValue[value] ? value : 'left'
  11970. },
  11971. queryCommandState: function() {
  11972. var start = this.selection.getStart(),
  11973. cell =
  11974. start &&
  11975. domUtils.findParentByTagName(start, ['td', 'th', 'caption'], true)
  11976. return cell ? -1 : 0
  11977. }
  11978. }
  11979. }
  11980. // plugins/font.js
  11981. /**
  11982. * 字体颜色,背景色,字号,字体,下划线,删除线
  11983. * @file
  11984. * @since 1.2.6.1
  11985. */
  11986. /**
  11987. * 字体颜色
  11988. * @command forecolor
  11989. * @method execCommand
  11990. * @param { String } cmd 命令字符串
  11991. * @param { String } value 色值(必须十六进制)
  11992. * @example
  11993. * ```javascript
  11994. * editor.execCommand( 'forecolor', '#000' );
  11995. * ```
  11996. */
  11997. /**
  11998. * 返回选区字体颜色
  11999. * @command forecolor
  12000. * @method queryCommandValue
  12001. * @param { String } cmd 命令字符串
  12002. * @return { String } 返回字体颜色
  12003. * @example
  12004. * ```javascript
  12005. * editor.queryCommandValue( 'forecolor' );
  12006. * ```
  12007. */
  12008. /**
  12009. * 字体背景颜色
  12010. * @command backcolor
  12011. * @method execCommand
  12012. * @param { String } cmd 命令字符串
  12013. * @param { String } value 色值(必须十六进制)
  12014. * @example
  12015. * ```javascript
  12016. * editor.execCommand( 'backcolor', '#000' );
  12017. * ```
  12018. */
  12019. /**
  12020. * 返回选区字体颜色
  12021. * @command backcolor
  12022. * @method queryCommandValue
  12023. * @param { String } cmd 命令字符串
  12024. * @return { String } 返回字体背景颜色
  12025. * @example
  12026. * ```javascript
  12027. * editor.queryCommandValue( 'backcolor' );
  12028. * ```
  12029. */
  12030. /**
  12031. * 字体大小
  12032. * @command fontsize
  12033. * @method execCommand
  12034. * @param { String } cmd 命令字符串
  12035. * @param { String } value 字体大小
  12036. * @example
  12037. * ```javascript
  12038. * editor.execCommand( 'fontsize', '14px' );
  12039. * ```
  12040. */
  12041. /**
  12042. * 返回选区字体大小
  12043. * @command fontsize
  12044. * @method queryCommandValue
  12045. * @param { String } cmd 命令字符串
  12046. * @return { String } 返回字体大小
  12047. * @example
  12048. * ```javascript
  12049. * editor.queryCommandValue( 'fontsize' );
  12050. * ```
  12051. */
  12052. /**
  12053. * 字体样式
  12054. * @command fontfamily
  12055. * @method execCommand
  12056. * @param { String } cmd 命令字符串
  12057. * @param { String } value 字体样式
  12058. * @example
  12059. * ```javascript
  12060. * editor.execCommand( 'fontfamily', '微软雅黑' );
  12061. * ```
  12062. */
  12063. /**
  12064. * 返回选区字体样式
  12065. * @command fontfamily
  12066. * @method queryCommandValue
  12067. * @param { String } cmd 命令字符串
  12068. * @return { String } 返回字体样式
  12069. * @example
  12070. * ```javascript
  12071. * editor.queryCommandValue( 'fontfamily' );
  12072. * ```
  12073. */
  12074. /**
  12075. * 字体下划线,与删除线互斥
  12076. * @command underline
  12077. * @method execCommand
  12078. * @param { String } cmd 命令字符串
  12079. * @example
  12080. * ```javascript
  12081. * editor.execCommand( 'underline' );
  12082. * ```
  12083. */
  12084. /**
  12085. * 字体删除线,与下划线互斥
  12086. * @command strikethrough
  12087. * @method execCommand
  12088. * @param { String } cmd 命令字符串
  12089. * @example
  12090. * ```javascript
  12091. * editor.execCommand( 'strikethrough' );
  12092. * ```
  12093. */
  12094. /**
  12095. * 字体边框
  12096. * @command fontborder
  12097. * @method execCommand
  12098. * @param { String } cmd 命令字符串
  12099. * @example
  12100. * ```javascript
  12101. * editor.execCommand( 'fontborder' );
  12102. * ```
  12103. */
  12104. UE.plugins['font'] = function() {
  12105. var me = this,
  12106. fonts = {
  12107. forecolor: 'color',
  12108. backcolor: 'background-color',
  12109. fontsize: 'font-size',
  12110. fontfamily: 'font-family',
  12111. underline: 'text-decoration',
  12112. strikethrough: 'text-decoration',
  12113. fontborder: 'border'
  12114. },
  12115. needCmd = {underline: 1, strikethrough: 1, fontborder: 1},
  12116. needSetChild = {
  12117. forecolor: 'color',
  12118. backcolor: 'background-color',
  12119. fontsize: 'font-size',
  12120. fontfamily: 'font-family'
  12121. }
  12122. me.setOpt({
  12123. fontfamily: [
  12124. {name: 'songti', val: '宋体,SimSun'},
  12125. {name: 'yahei', val: '微软雅黑,Microsoft YaHei'},
  12126. {name: 'kaiti', val: '楷体,楷体_GB2312, SimKai'},
  12127. {name: 'heiti', val: '黑体, SimHei'},
  12128. {name: 'lishu', val: '隶书, SimLi'},
  12129. {name: 'andaleMono', val: 'andale mono'},
  12130. {name: 'arial', val: 'arial, helvetica,sans-serif'},
  12131. {name: 'arialBlack', val: 'arial black,avant garde'},
  12132. {name: 'comicSansMs', val: 'comic sans ms'},
  12133. {name: 'impact', val: 'impact,chicago'},
  12134. {name: 'timesNewRoman', val: 'times new roman'}
  12135. ],
  12136. fontsize: [10, 11, 12, 14, 16, 18, 20, 24, 36]
  12137. })
  12138. function mergeWithParent(node) {
  12139. var parent
  12140. while ((parent = node.parentNode)) {
  12141. if (
  12142. parent.tagName == 'SPAN' &&
  12143. domUtils.getChildCount(parent, function(child) {
  12144. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child)
  12145. }) == 1
  12146. ) {
  12147. parent.style.cssText += node.style.cssText
  12148. domUtils.remove(node, true)
  12149. node = parent
  12150. } else {
  12151. break
  12152. }
  12153. }
  12154. }
  12155. function mergeChild(rng, cmdName, value) {
  12156. if (needSetChild[cmdName]) {
  12157. rng.adjustmentBoundary()
  12158. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  12159. var start = rng.startContainer.childNodes[rng.startOffset]
  12160. if (start && domUtils.isTagNode(start, 'span')) {
  12161. var bk = rng.createBookmark()
  12162. utils.each(domUtils.getElementsByTagName(start, 'span'), function(
  12163. span
  12164. ) {
  12165. if (!span.parentNode || domUtils.isBookmarkNode(span)) return
  12166. if (
  12167. cmdName == 'backcolor' &&
  12168. domUtils
  12169. .getComputedStyle(span, 'background-color')
  12170. .toLowerCase() === value
  12171. ) {
  12172. return
  12173. }
  12174. domUtils.removeStyle(span, needSetChild[cmdName])
  12175. if (span.style.cssText.replace(/^\s+$/, '').length == 0) {
  12176. domUtils.remove(span, true)
  12177. }
  12178. })
  12179. rng.moveToBookmark(bk)
  12180. }
  12181. }
  12182. }
  12183. }
  12184. function mergesibling(rng, cmdName, value) {
  12185. var collapsed = rng.collapsed,
  12186. bk = rng.createBookmark(),
  12187. common
  12188. if (collapsed) {
  12189. common = bk.start.parentNode
  12190. while (dtd.$inline[common.tagName]) {
  12191. common = common.parentNode
  12192. }
  12193. } else {
  12194. common = domUtils.getCommonAncestor(bk.start, bk.end)
  12195. }
  12196. utils.each(domUtils.getElementsByTagName(common, 'span'), function(span) {
  12197. if (!span.parentNode || domUtils.isBookmarkNode(span)) return
  12198. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  12199. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  12200. domUtils.remove(span, true)
  12201. } else {
  12202. domUtils.removeStyle(span, 'border')
  12203. }
  12204. return
  12205. }
  12206. if (
  12207. /border/i.test(span.style.cssText) &&
  12208. span.parentNode.tagName == 'SPAN' &&
  12209. /border/i.test(span.parentNode.style.cssText)
  12210. ) {
  12211. span.style.cssText = span.style.cssText.replace(
  12212. /border[^:]*:[^;]+;?/gi,
  12213. ''
  12214. )
  12215. }
  12216. if (!(cmdName == 'fontborder' && value == 'none')) {
  12217. var next = span.nextSibling
  12218. while (next && next.nodeType == 1 && next.tagName == 'SPAN') {
  12219. if (domUtils.isBookmarkNode(next) && cmdName == 'fontborder') {
  12220. span.appendChild(next)
  12221. next = span.nextSibling
  12222. continue
  12223. }
  12224. if (next.style.cssText == span.style.cssText) {
  12225. domUtils.moveChild(next, span)
  12226. domUtils.remove(next)
  12227. }
  12228. if (span.nextSibling === next) break
  12229. next = span.nextSibling
  12230. }
  12231. }
  12232. mergeWithParent(span)
  12233. if (browser.ie && browser.version > 8) {
  12234. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  12235. var parent = domUtils.findParent(span, function(n) {
  12236. return (
  12237. n.tagName == 'SPAN' && /background-color/.test(n.style.cssText)
  12238. )
  12239. })
  12240. if (parent && !/background-color/.test(span.style.cssText)) {
  12241. span.style.backgroundColor = parent.style.backgroundColor
  12242. }
  12243. }
  12244. })
  12245. rng.moveToBookmark(bk)
  12246. mergeChild(rng, cmdName, value)
  12247. }
  12248. me.addInputRule(function(root) {
  12249. utils.each(root.getNodesByTagName('u s del font strike'), function(node) {
  12250. if (node.tagName == 'font') {
  12251. var cssStyle = []
  12252. for (var p in node.attrs) {
  12253. switch (p) {
  12254. case 'size':
  12255. cssStyle.push(
  12256. 'font-size:' +
  12257. ({
  12258. '1': '10',
  12259. '2': '12',
  12260. '3': '16',
  12261. '4': '18',
  12262. '5': '24',
  12263. '6': '32',
  12264. '7': '48'
  12265. }[node.attrs[p]] || node.attrs[p]) +
  12266. 'px'
  12267. )
  12268. break
  12269. case 'color':
  12270. cssStyle.push('color:' + node.attrs[p])
  12271. break
  12272. case 'face':
  12273. cssStyle.push('font-family:' + node.attrs[p])
  12274. break
  12275. case 'style':
  12276. cssStyle.push(node.attrs[p])
  12277. }
  12278. }
  12279. node.attrs = {
  12280. style: cssStyle.join(';')
  12281. }
  12282. } else {
  12283. var val = node.tagName == 'u' ? 'underline' : 'line-through'
  12284. node.attrs = {
  12285. style:
  12286. (node.getAttr('style') || '') + 'text-decoration:' + val + ';'
  12287. }
  12288. }
  12289. node.tagName = 'span'
  12290. })
  12291. // utils.each(root.getNodesByTagName('span'), function (node) {
  12292. // var val;
  12293. // if(val = node.getAttr('class')){
  12294. // if(/fontstrikethrough/.test(val)){
  12295. // node.setStyle('text-decoration','line-through');
  12296. // if(node.attrs['class']){
  12297. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  12298. // }else{
  12299. // node.setAttr('class')
  12300. // }
  12301. // }
  12302. // if(/fontborder/.test(val)){
  12303. // node.setStyle('border','1px solid #000');
  12304. // if(node.attrs['class']){
  12305. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  12306. // }else{
  12307. // node.setAttr('class')
  12308. // }
  12309. // }
  12310. // }
  12311. // });
  12312. })
  12313. // me.addOutputRule(function(root){
  12314. // utils.each(root.getNodesByTagName('span'), function (node) {
  12315. // var val;
  12316. // if(val = node.getStyle('text-decoration')){
  12317. // if(/line-through/.test(val)){
  12318. // if(node.attrs['class']){
  12319. // node.attrs['class'] += ' fontstrikethrough';
  12320. // }else{
  12321. // node.setAttr('class','fontstrikethrough')
  12322. // }
  12323. // }
  12324. //
  12325. // node.setStyle('text-decoration')
  12326. // }
  12327. // if(val = node.getStyle('border')){
  12328. // if(/1px/.test(val) && /solid/.test(val)){
  12329. // if(node.attrs['class']){
  12330. // node.attrs['class'] += ' fontborder';
  12331. //
  12332. // }else{
  12333. // node.setAttr('class','fontborder')
  12334. // }
  12335. // }
  12336. // node.setStyle('border')
  12337. //
  12338. // }
  12339. // });
  12340. // });
  12341. for (var p in fonts) {
  12342. ;(function(cmd, style) {
  12343. UE.commands[cmd] = {
  12344. execCommand: function(cmdName, value) {
  12345. value =
  12346. value ||
  12347. (this.queryCommandState(cmdName)
  12348. ? 'none'
  12349. : cmdName == 'underline'
  12350. ? 'underline'
  12351. : cmdName == 'fontborder'
  12352. ? '1px solid #000'
  12353. : 'line-through')
  12354. var me = this,
  12355. range = this.selection.getRange(),
  12356. text
  12357. if (value == 'default') {
  12358. if (range.collapsed) {
  12359. text = me.document.createTextNode('font')
  12360. range.insertNode(text).select()
  12361. }
  12362. me.execCommand('removeFormat', 'span,a', style)
  12363. if (text) {
  12364. range.setStartBefore(text).collapse(true)
  12365. domUtils.remove(text)
  12366. }
  12367. mergesibling(range, cmdName, value)
  12368. range.select()
  12369. } else {
  12370. if (!range.collapsed) {
  12371. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  12372. me.execCommand('removeFormat', 'span,a', style)
  12373. }
  12374. range = me.selection.getRange()
  12375. range.applyInlineStyle('span', {style: style + ':' + value})
  12376. mergesibling(range, cmdName, value)
  12377. range.select()
  12378. } else {
  12379. var span = domUtils.findParentByTagName(
  12380. range.startContainer,
  12381. 'span',
  12382. true
  12383. )
  12384. text = me.document.createTextNode('font')
  12385. if (
  12386. span &&
  12387. !span.children.length &&
  12388. !span[browser.ie ? 'innerText' : 'textContent'].replace(
  12389. fillCharReg,
  12390. ''
  12391. ).length
  12392. ) {
  12393. //for ie hack when enter
  12394. range.insertNode(text)
  12395. if (needCmd[cmd]) {
  12396. range.selectNode(text).select()
  12397. me.execCommand('removeFormat', 'span,a', style, null)
  12398. span = domUtils.findParentByTagName(text, 'span', true)
  12399. range.setStartBefore(text)
  12400. }
  12401. span && (span.style.cssText += ';' + style + ':' + value)
  12402. range.collapse(true).select()
  12403. } else {
  12404. range.insertNode(text)
  12405. range.selectNode(text).select()
  12406. span = range.document.createElement('span')
  12407. if (needCmd[cmd]) {
  12408. //a标签内的不处理跳过
  12409. if (domUtils.findParentByTagName(text, 'a', true)) {
  12410. range.setStartBefore(text).setCursor()
  12411. domUtils.remove(text)
  12412. return
  12413. }
  12414. me.execCommand('removeFormat', 'span,a', style)
  12415. }
  12416. span.style.cssText = style + ':' + value
  12417. text.parentNode.insertBefore(span, text)
  12418. //修复,span套span 但样式不继承的问题
  12419. if (!browser.ie || (browser.ie && browser.version == 9)) {
  12420. var spanParent = span.parentNode
  12421. while (!domUtils.isBlockElm(spanParent)) {
  12422. if (spanParent.tagName == 'SPAN') {
  12423. //opera合并style不会加入";"
  12424. span.style.cssText =
  12425. spanParent.style.cssText + ';' + span.style.cssText
  12426. }
  12427. spanParent = spanParent.parentNode
  12428. }
  12429. }
  12430. if (opera) {
  12431. setTimeout(function() {
  12432. range.setStart(span, 0).collapse(true)
  12433. mergesibling(range, cmdName, value)
  12434. range.select()
  12435. })
  12436. } else {
  12437. range.setStart(span, 0).collapse(true)
  12438. mergesibling(range, cmdName, value)
  12439. range.select()
  12440. }
  12441. //trace:981
  12442. //domUtils.mergeToParent(span)
  12443. }
  12444. domUtils.remove(text)
  12445. }
  12446. }
  12447. return true
  12448. },
  12449. queryCommandValue: function(cmdName) {
  12450. var startNode = this.selection.getStart()
  12451. //trace:946
  12452. if (cmdName == 'underline' || cmdName == 'strikethrough') {
  12453. var tmpNode = startNode,
  12454. value
  12455. while (
  12456. tmpNode &&
  12457. !domUtils.isBlockElm(tmpNode) &&
  12458. !domUtils.isBody(tmpNode)
  12459. ) {
  12460. if (tmpNode.nodeType == 1) {
  12461. value = domUtils.getComputedStyle(tmpNode, style)
  12462. if (value != 'none') {
  12463. return value
  12464. }
  12465. }
  12466. tmpNode = tmpNode.parentNode
  12467. }
  12468. return 'none'
  12469. }
  12470. if (cmdName == 'fontborder') {
  12471. var tmp = startNode,
  12472. val
  12473. while (tmp && dtd.$inline[tmp.tagName]) {
  12474. if ((val = domUtils.getComputedStyle(tmp, 'border'))) {
  12475. if (/1px/.test(val) && /solid/.test(val)) {
  12476. return val
  12477. }
  12478. }
  12479. tmp = tmp.parentNode
  12480. }
  12481. return ''
  12482. }
  12483. if (cmdName == 'FontSize') {
  12484. var styleVal = domUtils.getComputedStyle(startNode, style),
  12485. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal)
  12486. if (tmp) {
  12487. return Math.floor(tmp[1]) + tmp[2]
  12488. }
  12489. return styleVal
  12490. }
  12491. return domUtils.getComputedStyle(startNode, style)
  12492. },
  12493. queryCommandState: function(cmdName) {
  12494. if (!needCmd[cmdName]) return 0
  12495. var val = this.queryCommandValue(cmdName)
  12496. if (cmdName == 'fontborder') {
  12497. return /1px/.test(val) && /solid/.test(val)
  12498. } else {
  12499. return cmdName == 'underline'
  12500. ? /underline/.test(val)
  12501. : /line\-through/.test(val)
  12502. }
  12503. }
  12504. }
  12505. })(p, fonts[p])
  12506. }
  12507. }
  12508. // plugins/link.js
  12509. /**
  12510. * 超链接
  12511. * @file
  12512. * @since 1.2.6.1
  12513. */
  12514. /**
  12515. * 插入超链接
  12516. * @command link
  12517. * @method execCommand
  12518. * @param { String } cmd 命令字符串
  12519. * @param { Object } options 设置自定义属性,例如:url、title、target
  12520. * @example
  12521. * ```javascript
  12522. * editor.execCommand( 'link', '{
  12523. * url:'ueditor.baidu.com',
  12524. * title:'ueditor',
  12525. * target:'_blank'
  12526. * }' );
  12527. * ```
  12528. */
  12529. /**
  12530. * 返回当前选中的第一个超链接节点
  12531. * @command link
  12532. * @method queryCommandValue
  12533. * @param { String } cmd 命令字符串
  12534. * @return { Element } 超链接节点
  12535. * @example
  12536. * ```javascript
  12537. * editor.queryCommandValue( 'link' );
  12538. * ```
  12539. */
  12540. /**
  12541. * 取消超链接
  12542. * @command unlink
  12543. * @method execCommand
  12544. * @param { String } cmd 命令字符串
  12545. * @example
  12546. * ```javascript
  12547. * editor.execCommand( 'unlink');
  12548. * ```
  12549. */
  12550. UE.plugins['link'] = function() {
  12551. function optimize(range) {
  12552. var start = range.startContainer,
  12553. end = range.endContainer
  12554. if ((start = domUtils.findParentByTagName(start, 'a', true))) {
  12555. range.setStartBefore(start)
  12556. }
  12557. if ((end = domUtils.findParentByTagName(end, 'a', true))) {
  12558. range.setEndAfter(end)
  12559. }
  12560. }
  12561. UE.commands['unlink'] = {
  12562. execCommand: function() {
  12563. var range = this.selection.getRange(),
  12564. bookmark
  12565. if (
  12566. range.collapsed &&
  12567. !domUtils.findParentByTagName(range.startContainer, 'a', true)
  12568. ) {
  12569. return
  12570. }
  12571. bookmark = range.createBookmark()
  12572. optimize(range)
  12573. range
  12574. .removeInlineStyle('a')
  12575. .moveToBookmark(bookmark)
  12576. .select()
  12577. },
  12578. queryCommandState: function() {
  12579. return !this.highlight && this.queryCommandValue('link') ? 0 : -1
  12580. }
  12581. }
  12582. function doLink(range, opt, me) {
  12583. var rngClone = range.cloneRange(),
  12584. link = me.queryCommandValue('link')
  12585. optimize((range = range.adjustmentBoundary()))
  12586. var start = range.startContainer
  12587. if (start.nodeType == 1 && link) {
  12588. start = start.childNodes[range.startOffset]
  12589. if (
  12590. start &&
  12591. start.nodeType == 1 &&
  12592. start.tagName == 'A' &&
  12593. /^(?:https?|ftp|file)\s*:\s*\/\//.test(
  12594. start[browser.ie ? 'innerText' : 'textContent']
  12595. )
  12596. ) {
  12597. start[browser.ie ? 'innerText' : 'textContent'] = utils.html(
  12598. opt.textValue || opt.href
  12599. )
  12600. }
  12601. }
  12602. if (!rngClone.collapsed || link) {
  12603. range.removeInlineStyle('a')
  12604. rngClone = range.cloneRange()
  12605. }
  12606. if (rngClone.collapsed) {
  12607. var a = range.document.createElement('a'),
  12608. text = ''
  12609. if (opt.textValue) {
  12610. text = utils.html(opt.textValue)
  12611. delete opt.textValue
  12612. } else {
  12613. text = utils.html(opt.href)
  12614. }
  12615. domUtils.setAttributes(a, opt)
  12616. start = domUtils.findParentByTagName(rngClone.startContainer, 'a', true)
  12617. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  12618. range.setStartAfter(start).collapse(true)
  12619. }
  12620. a[browser.ie ? 'innerText' : 'textContent'] = text
  12621. range.insertNode(a).selectNode(a)
  12622. } else {
  12623. range.applyInlineStyle('a', opt)
  12624. }
  12625. }
  12626. UE.commands['link'] = {
  12627. execCommand: function(cmdName, opt) {
  12628. var range
  12629. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g))
  12630. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g))
  12631. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g))
  12632. doLink((range = this.selection.getRange()), opt, this)
  12633. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  12634. range.collapse().select(true)
  12635. },
  12636. queryCommandValue: function() {
  12637. var range = this.selection.getRange(),
  12638. node
  12639. if (range.collapsed) {
  12640. // node = this.selection.getStart();
  12641. //在ie下getstart()取值偏上了
  12642. node = range.startContainer
  12643. node = node.nodeType == 1 ? node : node.parentNode
  12644. if (
  12645. node &&
  12646. (node = domUtils.findParentByTagName(node, 'a', true)) &&
  12647. !domUtils.isInNodeEndBoundary(range, node)
  12648. ) {
  12649. return node
  12650. }
  12651. } else {
  12652. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  12653. range.shrinkBoundary()
  12654. var start =
  12655. range.startContainer.nodeType == 3 ||
  12656. !range.startContainer.childNodes[range.startOffset]
  12657. ? range.startContainer
  12658. : range.startContainer.childNodes[range.startOffset],
  12659. end =
  12660. range.endContainer.nodeType == 3 || range.endOffset == 0
  12661. ? range.endContainer
  12662. : range.endContainer.childNodes[range.endOffset - 1],
  12663. common = range.getCommonAncestor()
  12664. node = domUtils.findParentByTagName(common, 'a', true)
  12665. if (!node && common.nodeType == 1) {
  12666. var as = common.getElementsByTagName('a'),
  12667. ps,
  12668. pe
  12669. for (var i = 0, ci; (ci = as[i++]); ) {
  12670. ;(ps = domUtils.getPosition(ci, start)),
  12671. (pe = domUtils.getPosition(ci, end))
  12672. if (
  12673. (ps & domUtils.POSITION_FOLLOWING ||
  12674. ps & domUtils.POSITION_CONTAINS) &&
  12675. (pe & domUtils.POSITION_PRECEDING ||
  12676. pe & domUtils.POSITION_CONTAINS)
  12677. ) {
  12678. node = ci
  12679. break
  12680. }
  12681. }
  12682. }
  12683. return node
  12684. }
  12685. },
  12686. queryCommandState: function() {
  12687. //判断如果是视频的话连接不可用
  12688. //fix 853
  12689. var img = this.selection.getRange().getClosedNode(),
  12690. flag =
  12691. img &&
  12692. (img.className == 'edui-faked-video' ||
  12693. img.className.indexOf('edui-upload-video') != -1)
  12694. return flag ? -1 : 0
  12695. }
  12696. }
  12697. }
  12698. // plugins/iframe.js
  12699. ///import core
  12700. ///import plugins\inserthtml.js
  12701. ///commands 插入框架
  12702. ///commandsName InsertFrame
  12703. ///commandsTitle 插入Iframe
  12704. ///commandsDialog dialogs\insertframe
  12705. UE.plugins['insertframe'] = function() {
  12706. var me = this
  12707. function deleteIframe() {
  12708. me._iframe && delete me._iframe
  12709. }
  12710. me.addListener('selectionchange', function() {
  12711. deleteIframe()
  12712. })
  12713. }
  12714. // plugins/scrawl.js
  12715. ///import core
  12716. ///commands 涂鸦
  12717. ///commandsName Scrawl
  12718. ///commandsTitle 涂鸦
  12719. ///commandsDialog dialogs\scrawl
  12720. UE.commands['scrawl'] = {
  12721. queryCommandState: function() {
  12722. return browser.ie && browser.version <= 8 ? -1 : 0
  12723. }
  12724. }
  12725. // plugins/removeformat.js
  12726. /**
  12727. * 清除格式
  12728. * @file
  12729. * @since 1.2.6.1
  12730. */
  12731. /**
  12732. * 清除文字样式
  12733. * @command removeformat
  12734. * @method execCommand
  12735. * @param { String } cmd 命令字符串
  12736. * @param {String} tags 以逗号隔开的标签。如:strong
  12737. * @param {String} style 样式如:color
  12738. * @param {String} attrs 属性如:width
  12739. * @example
  12740. * ```javascript
  12741. * editor.execCommand( 'removeformat', 'strong','color','width' );
  12742. * ```
  12743. */
  12744. UE.plugins['removeformat'] = function() {
  12745. var me = this
  12746. me.setOpt({
  12747. removeFormatTags:
  12748. 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var',
  12749. removeFormatAttributes:
  12750. 'class,style,lang,width,height,align,hspace,valign'
  12751. })
  12752. me.commands['removeformat'] = {
  12753. execCommand: function(cmdName, tags, style, attrs, notIncludeA) {
  12754. var tagReg = new RegExp(
  12755. '^(?:' +
  12756. (tags || this.options.removeFormatTags).replace(/,/g, '|') +
  12757. ')$',
  12758. 'i'
  12759. ),
  12760. removeFormatAttributes = style
  12761. ? []
  12762. : (attrs || this.options.removeFormatAttributes).split(','),
  12763. range = new dom.Range(this.document),
  12764. bookmark,
  12765. node,
  12766. parent,
  12767. filter = function(node) {
  12768. return node.nodeType == 1
  12769. }
  12770. function isRedundantSpan(node) {
  12771. if (node.nodeType == 3 || node.tagName.toLowerCase() != 'span') {
  12772. return 0
  12773. }
  12774. if (browser.ie) {
  12775. //ie 下判断实效,所以只能简单用style来判断
  12776. //return node.style.cssText == '' ? 1 : 0;
  12777. var attrs = node.attributes
  12778. if (attrs.length) {
  12779. for (var i = 0, l = attrs.length; i < l; i++) {
  12780. if (attrs[i].specified) {
  12781. return 0
  12782. }
  12783. }
  12784. return 1
  12785. }
  12786. }
  12787. return !node.attributes.length
  12788. }
  12789. function doRemove(range) {
  12790. var bookmark1 = range.createBookmark()
  12791. if (range.collapsed) {
  12792. range.enlarge(true)
  12793. }
  12794. //不能把a标签切了
  12795. if (!notIncludeA) {
  12796. var aNode = domUtils.findParentByTagName(
  12797. range.startContainer,
  12798. 'a',
  12799. true
  12800. )
  12801. if (aNode) {
  12802. range.setStartBefore(aNode)
  12803. }
  12804. aNode = domUtils.findParentByTagName(range.endContainer, 'a', true)
  12805. if (aNode) {
  12806. range.setEndAfter(aNode)
  12807. }
  12808. }
  12809. bookmark = range.createBookmark()
  12810. node = bookmark.start
  12811. //切开始
  12812. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12813. domUtils.breakParent(node, parent)
  12814. domUtils.clearEmptySibling(node)
  12815. }
  12816. if (bookmark.end) {
  12817. //切结束
  12818. node = bookmark.end
  12819. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12820. domUtils.breakParent(node, parent)
  12821. domUtils.clearEmptySibling(node)
  12822. }
  12823. //开始去除样式
  12824. var current = domUtils.getNextDomNode(
  12825. bookmark.start,
  12826. false,
  12827. filter
  12828. ),
  12829. next
  12830. while (current) {
  12831. if (current == bookmark.end) {
  12832. break
  12833. }
  12834. next = domUtils.getNextDomNode(current, true, filter)
  12835. if (
  12836. !dtd.$empty[current.tagName.toLowerCase()] &&
  12837. !domUtils.isBookmarkNode(current)
  12838. ) {
  12839. if (tagReg.test(current.tagName)) {
  12840. if (style) {
  12841. domUtils.removeStyle(current, style)
  12842. if (
  12843. isRedundantSpan(current) &&
  12844. style != 'text-decoration'
  12845. ) {
  12846. domUtils.remove(current, true)
  12847. }
  12848. } else {
  12849. domUtils.remove(current, true)
  12850. }
  12851. } else {
  12852. //trace:939 不能把list上的样式去掉
  12853. if (
  12854. !dtd.$tableContent[current.tagName] &&
  12855. !dtd.$list[current.tagName]
  12856. ) {
  12857. domUtils.removeAttributes(current, removeFormatAttributes)
  12858. if (isRedundantSpan(current)) {
  12859. domUtils.remove(current, true)
  12860. }
  12861. }
  12862. }
  12863. }
  12864. current = next
  12865. }
  12866. }
  12867. //trace:1035
  12868. //trace:1096 不能把td上的样式去掉,比如边框
  12869. var pN = bookmark.start.parentNode
  12870. if (
  12871. domUtils.isBlockElm(pN) &&
  12872. !dtd.$tableContent[pN.tagName] &&
  12873. !dtd.$list[pN.tagName]
  12874. ) {
  12875. domUtils.removeAttributes(pN, removeFormatAttributes)
  12876. }
  12877. pN = bookmark.end.parentNode
  12878. if (
  12879. bookmark.end &&
  12880. domUtils.isBlockElm(pN) &&
  12881. !dtd.$tableContent[pN.tagName] &&
  12882. !dtd.$list[pN.tagName]
  12883. ) {
  12884. domUtils.removeAttributes(pN, removeFormatAttributes)
  12885. }
  12886. range.moveToBookmark(bookmark).moveToBookmark(bookmark1)
  12887. //清除冗余的代码 <b><bookmark></b>
  12888. var node = range.startContainer,
  12889. tmp,
  12890. collapsed = range.collapsed
  12891. while (
  12892. node.nodeType == 1 &&
  12893. domUtils.isEmptyNode(node) &&
  12894. dtd.$removeEmpty[node.tagName]
  12895. ) {
  12896. tmp = node.parentNode
  12897. range.setStartBefore(node)
  12898. //trace:937
  12899. //更新结束边界
  12900. if (range.startContainer === range.endContainer) {
  12901. range.endOffset--
  12902. }
  12903. domUtils.remove(node)
  12904. node = tmp
  12905. }
  12906. if (!collapsed) {
  12907. node = range.endContainer
  12908. while (
  12909. node.nodeType == 1 &&
  12910. domUtils.isEmptyNode(node) &&
  12911. dtd.$removeEmpty[node.tagName]
  12912. ) {
  12913. tmp = node.parentNode
  12914. range.setEndBefore(node)
  12915. domUtils.remove(node)
  12916. node = tmp
  12917. }
  12918. }
  12919. }
  12920. range = this.selection.getRange()
  12921. doRemove(range)
  12922. range.select()
  12923. }
  12924. }
  12925. }
  12926. // plugins/blockquote.js
  12927. /**
  12928. * 添加引用
  12929. * @file
  12930. * @since 1.2.6.1
  12931. */
  12932. /**
  12933. * 添加引用
  12934. * @command blockquote
  12935. * @method execCommand
  12936. * @param { String } cmd 命令字符串
  12937. * @example
  12938. * ```javascript
  12939. * editor.execCommand( 'blockquote' );
  12940. * ```
  12941. */
  12942. /**
  12943. * 添加引用
  12944. * @command blockquote
  12945. * @method execCommand
  12946. * @param { String } cmd 命令字符串
  12947. * @param { Object } attrs 节点属性
  12948. * @example
  12949. * ```javascript
  12950. * editor.execCommand( 'blockquote',{
  12951. * style: "color: red;"
  12952. * } );
  12953. * ```
  12954. */
  12955. UE.plugins['blockquote'] = function() {
  12956. var me = this
  12957. function getObj(editor) {
  12958. return domUtils.filterNodeList(
  12959. editor.selection.getStartElementPath(),
  12960. 'blockquote'
  12961. )
  12962. }
  12963. me.commands['blockquote'] = {
  12964. execCommand: function(cmdName, attrs) {
  12965. var range = this.selection.getRange(),
  12966. obj = getObj(this),
  12967. blockquote = dtd.blockquote,
  12968. bookmark = range.createBookmark()
  12969. if (obj) {
  12970. var start = range.startContainer,
  12971. startBlock = domUtils.isBlockElm(start)
  12972. ? start
  12973. : domUtils.findParent(start, function(node) {
  12974. return domUtils.isBlockElm(node)
  12975. }),
  12976. end = range.endContainer,
  12977. endBlock = domUtils.isBlockElm(end)
  12978. ? end
  12979. : domUtils.findParent(end, function(node) {
  12980. return domUtils.isBlockElm(node)
  12981. })
  12982. //处理一下li
  12983. startBlock =
  12984. domUtils.findParentByTagName(startBlock, 'li', true) || startBlock
  12985. endBlock =
  12986. domUtils.findParentByTagName(endBlock, 'li', true) || endBlock
  12987. if (
  12988. startBlock.tagName == 'LI' ||
  12989. startBlock.tagName == 'TD' ||
  12990. startBlock === obj ||
  12991. domUtils.isBody(startBlock)
  12992. ) {
  12993. domUtils.remove(obj, true)
  12994. } else {
  12995. domUtils.breakParent(startBlock, obj)
  12996. }
  12997. if (startBlock !== endBlock) {
  12998. obj = domUtils.findParentByTagName(endBlock, 'blockquote')
  12999. if (obj) {
  13000. if (
  13001. endBlock.tagName == 'LI' ||
  13002. endBlock.tagName == 'TD' ||
  13003. domUtils.isBody(endBlock)
  13004. ) {
  13005. obj.parentNode && domUtils.remove(obj, true)
  13006. } else {
  13007. domUtils.breakParent(endBlock, obj)
  13008. }
  13009. }
  13010. }
  13011. var blockquotes = domUtils.getElementsByTagName(
  13012. this.document,
  13013. 'blockquote'
  13014. )
  13015. for (var i = 0, bi; (bi = blockquotes[i++]); ) {
  13016. if (!bi.childNodes.length) {
  13017. domUtils.remove(bi)
  13018. } else if (
  13019. domUtils.getPosition(bi, startBlock) &
  13020. domUtils.POSITION_FOLLOWING &&
  13021. domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING
  13022. ) {
  13023. domUtils.remove(bi, true)
  13024. }
  13025. }
  13026. } else {
  13027. var tmpRange = range.cloneRange(),
  13028. node =
  13029. tmpRange.startContainer.nodeType == 1
  13030. ? tmpRange.startContainer
  13031. : tmpRange.startContainer.parentNode,
  13032. preNode = node,
  13033. doEnd = 1
  13034. //调整开始
  13035. while (1) {
  13036. if (domUtils.isBody(node)) {
  13037. if (preNode !== node) {
  13038. if (range.collapsed) {
  13039. tmpRange.selectNode(preNode)
  13040. doEnd = 0
  13041. } else {
  13042. tmpRange.setStartBefore(preNode)
  13043. }
  13044. } else {
  13045. tmpRange.setStart(node, 0)
  13046. }
  13047. break
  13048. }
  13049. if (!blockquote[node.tagName]) {
  13050. if (range.collapsed) {
  13051. tmpRange.selectNode(preNode)
  13052. } else {
  13053. tmpRange.setStartBefore(preNode)
  13054. }
  13055. break
  13056. }
  13057. preNode = node
  13058. node = node.parentNode
  13059. }
  13060. //调整结束
  13061. if (doEnd) {
  13062. preNode = node = node =
  13063. tmpRange.endContainer.nodeType == 1
  13064. ? tmpRange.endContainer
  13065. : tmpRange.endContainer.parentNode
  13066. while (1) {
  13067. if (domUtils.isBody(node)) {
  13068. if (preNode !== node) {
  13069. tmpRange.setEndAfter(preNode)
  13070. } else {
  13071. tmpRange.setEnd(node, node.childNodes.length)
  13072. }
  13073. break
  13074. }
  13075. if (!blockquote[node.tagName]) {
  13076. tmpRange.setEndAfter(preNode)
  13077. break
  13078. }
  13079. preNode = node
  13080. node = node.parentNode
  13081. }
  13082. }
  13083. node = range.document.createElement('blockquote')
  13084. domUtils.setAttributes(node, attrs)
  13085. node.appendChild(tmpRange.extractContents())
  13086. tmpRange.insertNode(node)
  13087. //去除重复的
  13088. var childs = domUtils.getElementsByTagName(node, 'blockquote')
  13089. for (var i = 0, ci; (ci = childs[i++]); ) {
  13090. if (ci.parentNode) {
  13091. domUtils.remove(ci, true)
  13092. }
  13093. }
  13094. }
  13095. range.moveToBookmark(bookmark).select()
  13096. },
  13097. queryCommandState: function() {
  13098. return getObj(this) ? 1 : 0
  13099. }
  13100. }
  13101. }
  13102. // plugins/convertcase.js
  13103. /**
  13104. * 大小写转换
  13105. * @file
  13106. * @since 1.2.6.1
  13107. */
  13108. /**
  13109. * 把选区内文本变大写,与“tolowercase”命令互斥
  13110. * @command touppercase
  13111. * @method execCommand
  13112. * @param { String } cmd 命令字符串
  13113. * @example
  13114. * ```javascript
  13115. * editor.execCommand( 'touppercase' );
  13116. * ```
  13117. */
  13118. /**
  13119. * 把选区内文本变小写,与“touppercase”命令互斥
  13120. * @command tolowercase
  13121. * @method execCommand
  13122. * @param { String } cmd 命令字符串
  13123. * @example
  13124. * ```javascript
  13125. * editor.execCommand( 'tolowercase' );
  13126. * ```
  13127. */
  13128. UE.commands['touppercase'] = UE.commands['tolowercase'] = {
  13129. execCommand: function(cmd) {
  13130. var me = this
  13131. var rng = me.selection.getRange()
  13132. if (rng.collapsed) {
  13133. return rng
  13134. }
  13135. var bk = rng.createBookmark(),
  13136. bkEnd = bk.end,
  13137. filterFn = function(node) {
  13138. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  13139. },
  13140. curNode = domUtils.getNextDomNode(bk.start, false, filterFn)
  13141. while (
  13142. curNode &&
  13143. domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING
  13144. ) {
  13145. if (curNode.nodeType == 3) {
  13146. curNode.nodeValue = curNode.nodeValue[
  13147. cmd == 'touppercase' ? 'toUpperCase' : 'toLowerCase'
  13148. ]()
  13149. }
  13150. curNode = domUtils.getNextDomNode(curNode, true, filterFn)
  13151. if (curNode === bkEnd) {
  13152. break
  13153. }
  13154. }
  13155. rng.moveToBookmark(bk).select()
  13156. }
  13157. }
  13158. // plugins/indent.js
  13159. /**
  13160. * 首行缩进
  13161. * @file
  13162. * @since 1.2.6.1
  13163. */
  13164. /**
  13165. * 缩进
  13166. * @command indent
  13167. * @method execCommand
  13168. * @param { String } cmd 命令字符串
  13169. * @example
  13170. * ```javascript
  13171. * editor.execCommand( 'indent' );
  13172. * ```
  13173. */
  13174. UE.commands['indent'] = {
  13175. execCommand: function() {
  13176. var me = this,
  13177. value = me.queryCommandState('indent')
  13178. ? '0em'
  13179. : me.options.indentValue || '2em'
  13180. me.execCommand('Paragraph', 'p', {style: 'text-indent:' + value})
  13181. },
  13182. queryCommandState: function() {
  13183. var pN = domUtils.filterNodeList(
  13184. this.selection.getStartElementPath(),
  13185. 'p h1 h2 h3 h4 h5 h6'
  13186. )
  13187. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0
  13188. }
  13189. }
  13190. // plugins/print.js
  13191. /**
  13192. * 打印
  13193. * @file
  13194. * @since 1.2.6.1
  13195. */
  13196. /**
  13197. * 打印
  13198. * @command print
  13199. * @method execCommand
  13200. * @param { String } cmd 命令字符串
  13201. * @example
  13202. * ```javascript
  13203. * editor.execCommand( 'print' );
  13204. * ```
  13205. */
  13206. UE.commands['print'] = {
  13207. execCommand: function() {
  13208. this.window.print()
  13209. },
  13210. notNeedUndo: 1
  13211. }
  13212. // plugins/preview.js
  13213. /**
  13214. * 预览
  13215. * @file
  13216. * @since 1.2.6.1
  13217. */
  13218. /**
  13219. * 预览
  13220. * @command preview
  13221. * @method execCommand
  13222. * @param { String } cmd 命令字符串
  13223. * @example
  13224. * ```javascript
  13225. * editor.execCommand( 'preview' );
  13226. * ```
  13227. */
  13228. UE.commands['preview'] = {
  13229. execCommand: function() {
  13230. var w = window.open('', '_blank', ''),
  13231. d = w.document
  13232. d.open()
  13233. d.write(
  13234. '<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' +
  13235. this.options.UEDITOR_HOME_URL +
  13236. 'ueditor.parse.js"></script><script>' +
  13237. "setTimeout(function(){uParse('div',{rootPath: '" +
  13238. this.options.UEDITOR_HOME_URL +
  13239. "'})},300)" +
  13240. '</script></head><body><div>' +
  13241. this.getContent(null, null, true) +
  13242. '</div></body></html>'
  13243. )
  13244. d.close()
  13245. },
  13246. notNeedUndo: 1
  13247. }
  13248. // plugins/selectall.js
  13249. /**
  13250. * 全选
  13251. * @file
  13252. * @since 1.2.6.1
  13253. */
  13254. /**
  13255. * 选中所有内容
  13256. * @command selectall
  13257. * @method execCommand
  13258. * @param { String } cmd 命令字符串
  13259. * @example
  13260. * ```javascript
  13261. * editor.execCommand( 'selectall' );
  13262. * ```
  13263. */
  13264. UE.plugins['selectall'] = function() {
  13265. var me = this
  13266. me.commands['selectall'] = {
  13267. execCommand: function() {
  13268. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  13269. var me = this,
  13270. body = me.body,
  13271. range = me.selection.getRange()
  13272. range.selectNodeContents(body)
  13273. if (domUtils.isEmptyBlock(body)) {
  13274. //opera不能自动合并到元素的里边,要手动处理一下
  13275. if (
  13276. browser.opera &&
  13277. body.firstChild &&
  13278. body.firstChild.nodeType == 1
  13279. ) {
  13280. range.setStartAtFirst(body.firstChild)
  13281. }
  13282. range.collapse(true)
  13283. }
  13284. range.select(true)
  13285. },
  13286. notNeedUndo: 1
  13287. }
  13288. //快捷键
  13289. me.addshortcutkey({
  13290. selectAll: 'ctrl+65'
  13291. })
  13292. }
  13293. // plugins/paragraph.js
  13294. /**
  13295. * 段落样式
  13296. * @file
  13297. * @since 1.2.6.1
  13298. */
  13299. /**
  13300. * 段落格式
  13301. * @command paragraph
  13302. * @method execCommand
  13303. * @param { String } cmd 命令字符串
  13304. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  13305. * @param {Object} attrs 标签的属性
  13306. * @example
  13307. * ```javascript
  13308. * editor.execCommand( 'Paragraph','h1','{
  13309. * class:'test'
  13310. * }' );
  13311. * ```
  13312. */
  13313. /**
  13314. * 返回选区内节点标签名
  13315. * @command paragraph
  13316. * @method queryCommandValue
  13317. * @param { String } cmd 命令字符串
  13318. * @return { String } 节点标签名
  13319. * @example
  13320. * ```javascript
  13321. * editor.queryCommandValue( 'Paragraph' );
  13322. * ```
  13323. */
  13324. UE.plugins['paragraph'] = function() {
  13325. var me = this,
  13326. block = domUtils.isBlockElm,
  13327. notExchange = ['TD', 'LI', 'PRE'],
  13328. doParagraph = function(range, style, attrs, sourceCmdName) {
  13329. var bookmark = range.createBookmark(),
  13330. filterFn = function(node) {
  13331. return node.nodeType == 1
  13332. ? node.tagName.toLowerCase() != 'br' &&
  13333. !domUtils.isBookmarkNode(node)
  13334. : !domUtils.isWhitespace(node)
  13335. },
  13336. para
  13337. range.enlarge(true)
  13338. var bookmark2 = range.createBookmark(),
  13339. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13340. tmpRange = range.cloneRange(),
  13341. tmpNode
  13342. while (
  13343. current &&
  13344. !(
  13345. domUtils.getPosition(current, bookmark2.end) &
  13346. domUtils.POSITION_FOLLOWING
  13347. )
  13348. ) {
  13349. if (current.nodeType == 3 || !block(current)) {
  13350. tmpRange.setStartBefore(current)
  13351. while (current && current !== bookmark2.end && !block(current)) {
  13352. tmpNode = current
  13353. current = domUtils.getNextDomNode(current, false, null, function(
  13354. node
  13355. ) {
  13356. return !block(node)
  13357. })
  13358. }
  13359. tmpRange.setEndAfter(tmpNode)
  13360. para = range.document.createElement(style)
  13361. if (attrs) {
  13362. domUtils.setAttributes(para, attrs)
  13363. if (
  13364. sourceCmdName &&
  13365. sourceCmdName == 'customstyle' &&
  13366. attrs.style
  13367. ) {
  13368. para.style.cssText = attrs.style
  13369. }
  13370. }
  13371. para.appendChild(tmpRange.extractContents())
  13372. //需要内容占位
  13373. if (domUtils.isEmptyNode(para)) {
  13374. domUtils.fillChar(range.document, para)
  13375. }
  13376. tmpRange.insertNode(para)
  13377. var parent = para.parentNode
  13378. //如果para上一级是一个block元素且不是body,td就删除它
  13379. if (
  13380. block(parent) &&
  13381. !domUtils.isBody(para.parentNode) &&
  13382. utils.indexOf(notExchange, parent.tagName) == -1
  13383. ) {
  13384. //存储dir,style
  13385. if (!(sourceCmdName && sourceCmdName == 'customstyle')) {
  13386. parent.getAttribute('dir') &&
  13387. para.setAttribute('dir', parent.getAttribute('dir'))
  13388. //trace:1070
  13389. parent.style.cssText &&
  13390. (para.style.cssText =
  13391. parent.style.cssText + ';' + para.style.cssText)
  13392. //trace:1030
  13393. parent.style.textAlign &&
  13394. !para.style.textAlign &&
  13395. (para.style.textAlign = parent.style.textAlign)
  13396. parent.style.textIndent &&
  13397. !para.style.textIndent &&
  13398. (para.style.textIndent = parent.style.textIndent)
  13399. parent.style.padding &&
  13400. !para.style.padding &&
  13401. (para.style.padding = parent.style.padding)
  13402. }
  13403. //trace:1706 选择的就是h1-6要删除
  13404. if (
  13405. attrs &&
  13406. /h\d/i.test(parent.tagName) &&
  13407. !/h\d/i.test(para.tagName)
  13408. ) {
  13409. domUtils.setAttributes(parent, attrs)
  13410. if (
  13411. sourceCmdName &&
  13412. sourceCmdName == 'customstyle' &&
  13413. attrs.style
  13414. ) {
  13415. parent.style.cssText = attrs.style
  13416. }
  13417. domUtils.remove(para, true)
  13418. para = parent
  13419. } else {
  13420. domUtils.remove(para.parentNode, true)
  13421. }
  13422. }
  13423. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  13424. current = parent
  13425. } else {
  13426. current = para
  13427. }
  13428. current = domUtils.getNextDomNode(current, false, filterFn)
  13429. } else {
  13430. current = domUtils.getNextDomNode(current, true, filterFn)
  13431. }
  13432. }
  13433. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  13434. }
  13435. me.setOpt('paragraph', {
  13436. p: '',
  13437. h1: '',
  13438. h2: '',
  13439. h3: '',
  13440. h4: '',
  13441. h5: '',
  13442. h6: ''
  13443. })
  13444. me.commands['paragraph'] = {
  13445. execCommand: function(cmdName, style, attrs, sourceCmdName) {
  13446. var range = this.selection.getRange()
  13447. //闭合时单独处理
  13448. if (range.collapsed) {
  13449. var txt = this.document.createTextNode('p')
  13450. range.insertNode(txt)
  13451. //去掉冗余的fillchar
  13452. if (browser.ie) {
  13453. var node = txt.previousSibling
  13454. if (node && domUtils.isWhitespace(node)) {
  13455. domUtils.remove(node)
  13456. }
  13457. node = txt.nextSibling
  13458. if (node && domUtils.isWhitespace(node)) {
  13459. domUtils.remove(node)
  13460. }
  13461. }
  13462. }
  13463. range = doParagraph(range, style, attrs, sourceCmdName)
  13464. if (txt) {
  13465. range.setStartBefore(txt).collapse(true)
  13466. pN = txt.parentNode
  13467. domUtils.remove(txt)
  13468. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  13469. domUtils.fillNode(this.document, pN)
  13470. }
  13471. }
  13472. if (
  13473. browser.gecko &&
  13474. range.collapsed &&
  13475. range.startContainer.nodeType == 1
  13476. ) {
  13477. var child = range.startContainer.childNodes[range.startOffset]
  13478. if (
  13479. child &&
  13480. child.nodeType == 1 &&
  13481. child.tagName.toLowerCase() == style
  13482. ) {
  13483. range.setStart(child, 0).collapse(true)
  13484. }
  13485. }
  13486. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  13487. range.select()
  13488. return true
  13489. },
  13490. queryCommandValue: function() {
  13491. var node = domUtils.filterNodeList(
  13492. this.selection.getStartElementPath(),
  13493. 'p h1 h2 h3 h4 h5 h6'
  13494. )
  13495. return node ? node.tagName.toLowerCase() : ''
  13496. }
  13497. }
  13498. }
  13499. // plugins/directionality.js
  13500. /**
  13501. * 设置文字输入的方向的插件
  13502. * @file
  13503. * @since 1.2.6.1
  13504. */
  13505. ;(function() {
  13506. var block = domUtils.isBlockElm,
  13507. getObj = function(editor) {
  13508. // var startNode = editor.selection.getStart(),
  13509. // parents;
  13510. // if ( startNode ) {
  13511. // //查找所有的是block的父亲节点
  13512. // parents = domUtils.findParents( startNode, true, block, true );
  13513. // for ( var i = 0,ci; ci = parents[i++]; ) {
  13514. // if ( ci.getAttribute( 'dir' ) ) {
  13515. // return ci;
  13516. // }
  13517. // }
  13518. // }
  13519. return domUtils.filterNodeList(
  13520. editor.selection.getStartElementPath(),
  13521. function(n) {
  13522. return n && n.nodeType == 1 && n.getAttribute('dir')
  13523. }
  13524. )
  13525. },
  13526. doDirectionality = function(range, editor, forward) {
  13527. var bookmark,
  13528. filterFn = function(node) {
  13529. return node.nodeType == 1
  13530. ? !domUtils.isBookmarkNode(node)
  13531. : !domUtils.isWhitespace(node)
  13532. },
  13533. obj = getObj(editor)
  13534. if (obj && range.collapsed) {
  13535. obj.setAttribute('dir', forward)
  13536. return range
  13537. }
  13538. bookmark = range.createBookmark()
  13539. range.enlarge(true)
  13540. var bookmark2 = range.createBookmark(),
  13541. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13542. tmpRange = range.cloneRange(),
  13543. tmpNode
  13544. while (
  13545. current &&
  13546. !(
  13547. domUtils.getPosition(current, bookmark2.end) &
  13548. domUtils.POSITION_FOLLOWING
  13549. )
  13550. ) {
  13551. if (current.nodeType == 3 || !block(current)) {
  13552. tmpRange.setStartBefore(current)
  13553. while (current && current !== bookmark2.end && !block(current)) {
  13554. tmpNode = current
  13555. current = domUtils.getNextDomNode(current, false, null, function(
  13556. node
  13557. ) {
  13558. return !block(node)
  13559. })
  13560. }
  13561. tmpRange.setEndAfter(tmpNode)
  13562. var common = tmpRange.getCommonAncestor()
  13563. if (!domUtils.isBody(common) && block(common)) {
  13564. //遍历到了block节点
  13565. common.setAttribute('dir', forward)
  13566. current = common
  13567. } else {
  13568. //没有遍历到,添加一个block节点
  13569. var p = range.document.createElement('p')
  13570. p.setAttribute('dir', forward)
  13571. var frag = tmpRange.extractContents()
  13572. p.appendChild(frag)
  13573. tmpRange.insertNode(p)
  13574. current = p
  13575. }
  13576. current = domUtils.getNextDomNode(current, false, filterFn)
  13577. } else {
  13578. current = domUtils.getNextDomNode(current, true, filterFn)
  13579. }
  13580. }
  13581. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  13582. }
  13583. /**
  13584. * 文字输入方向
  13585. * @command directionality
  13586. * @method execCommand
  13587. * @param { String } cmdName 命令字符串
  13588. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  13589. * @example
  13590. * ```javascript
  13591. * editor.execCommand( 'directionality', 'ltr');
  13592. * ```
  13593. */
  13594. /**
  13595. * 查询当前选区的文字输入方向
  13596. * @command directionality
  13597. * @method queryCommandValue
  13598. * @param { String } cmdName 命令字符串
  13599. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  13600. * @example
  13601. * ```javascript
  13602. * editor.queryCommandValue( 'directionality');
  13603. * ```
  13604. */
  13605. UE.commands['directionality'] = {
  13606. execCommand: function(cmdName, forward) {
  13607. var range = this.selection.getRange()
  13608. //闭合时单独处理
  13609. if (range.collapsed) {
  13610. var txt = this.document.createTextNode('d')
  13611. range.insertNode(txt)
  13612. }
  13613. doDirectionality(range, this, forward)
  13614. if (txt) {
  13615. range.setStartBefore(txt).collapse(true)
  13616. domUtils.remove(txt)
  13617. }
  13618. range.select()
  13619. return true
  13620. },
  13621. queryCommandValue: function() {
  13622. var node = getObj(this)
  13623. return node ? node.getAttribute('dir') : 'ltr'
  13624. }
  13625. }
  13626. })()
  13627. // plugins/horizontal.js
  13628. /**
  13629. * 插入分割线插件
  13630. * @file
  13631. * @since 1.2.6.1
  13632. */
  13633. /**
  13634. * 插入分割线
  13635. * @command horizontal
  13636. * @method execCommand
  13637. * @param { String } cmdName 命令字符串
  13638. * @example
  13639. * ```javascript
  13640. * editor.execCommand( 'horizontal' );
  13641. * ```
  13642. */
  13643. UE.plugins['horizontal'] = function() {
  13644. var me = this
  13645. me.commands['horizontal'] = {
  13646. execCommand: function(cmdName) {
  13647. var me = this
  13648. if (me.queryCommandState(cmdName) !== -1) {
  13649. me.execCommand('insertHtml', '<hr>')
  13650. var range = me.selection.getRange(),
  13651. start = range.startContainer
  13652. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  13653. var tmp
  13654. if ((tmp = start.childNodes[range.startOffset - 1])) {
  13655. if (tmp.nodeType == 1 && tmp.tagName == 'HR') {
  13656. if (me.options.enterTag == 'p') {
  13657. tmp = me.document.createElement('p')
  13658. range.insertNode(tmp)
  13659. range.setStart(tmp, 0).setCursor()
  13660. } else {
  13661. tmp = me.document.createElement('br')
  13662. range.insertNode(tmp)
  13663. range.setStartBefore(tmp).setCursor()
  13664. }
  13665. }
  13666. }
  13667. }
  13668. return true
  13669. }
  13670. },
  13671. //边界在table里不能加分隔线
  13672. queryCommandState: function() {
  13673. return domUtils.filterNodeList(
  13674. this.selection.getStartElementPath(),
  13675. 'table'
  13676. )
  13677. ? -1
  13678. : 0
  13679. }
  13680. }
  13681. // me.addListener('delkeyup',function(){
  13682. // var rng = this.selection.getRange();
  13683. // if(browser.ie && browser.version > 8){
  13684. // rng.txtToElmBoundary(true);
  13685. // if(domUtils.isStartInblock(rng)){
  13686. // var tmpNode = rng.startContainer;
  13687. // var pre = tmpNode.previousSibling;
  13688. // if(pre && domUtils.isTagNode(pre,'hr')){
  13689. // domUtils.remove(pre);
  13690. // rng.select();
  13691. // return;
  13692. // }
  13693. // }
  13694. // }
  13695. // if(domUtils.isBody(rng.startContainer)){
  13696. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  13697. // if(hr && hr.nodeName == 'HR'){
  13698. // var next = hr.nextSibling;
  13699. // if(next){
  13700. // rng.setStart(next,0)
  13701. // }else if(hr.previousSibling){
  13702. // rng.setStartAtLast(hr.previousSibling)
  13703. // }else{
  13704. // var p = this.document.createElement('p');
  13705. // hr.parentNode.insertBefore(p,hr);
  13706. // domUtils.fillNode(this.document,p);
  13707. // rng.setStart(p,0);
  13708. // }
  13709. // domUtils.remove(hr);
  13710. // rng.setCursor(false,true);
  13711. // }
  13712. // }
  13713. // })
  13714. me.addListener('delkeydown', function(name, evt) {
  13715. var rng = this.selection.getRange()
  13716. rng.txtToElmBoundary(true)
  13717. if (domUtils.isStartInblock(rng)) {
  13718. var tmpNode = rng.startContainer
  13719. var pre = tmpNode.previousSibling
  13720. if (pre && domUtils.isTagNode(pre, 'hr')) {
  13721. domUtils.remove(pre)
  13722. rng.select()
  13723. domUtils.preventDefault(evt)
  13724. return true
  13725. }
  13726. }
  13727. })
  13728. }
  13729. // plugins/time.js
  13730. /**
  13731. * 插入时间和日期
  13732. * @file
  13733. * @since 1.2.6.1
  13734. */
  13735. /**
  13736. * 插入时间,默认格式:12:59:59
  13737. * @command time
  13738. * @method execCommand
  13739. * @param { String } cmd 命令字符串
  13740. * @example
  13741. * ```javascript
  13742. * editor.execCommand( 'time');
  13743. * ```
  13744. */
  13745. /**
  13746. * 插入日期,默认格式:2013-08-30
  13747. * @command date
  13748. * @method execCommand
  13749. * @param { String } cmd 命令字符串
  13750. * @example
  13751. * ```javascript
  13752. * editor.execCommand( 'date');
  13753. * ```
  13754. */
  13755. UE.commands['time'] = UE.commands['date'] = {
  13756. execCommand: function(cmd, format) {
  13757. var date = new Date()
  13758. function formatTime(date, format) {
  13759. var hh = ('0' + date.getHours()).slice(-2),
  13760. ii = ('0' + date.getMinutes()).slice(-2),
  13761. ss = ('0' + date.getSeconds()).slice(-2)
  13762. format = format || 'hh:ii:ss'
  13763. return format
  13764. .replace(/hh/gi, hh)
  13765. .replace(/ii/gi, ii)
  13766. .replace(/ss/gi, ss)
  13767. }
  13768. function formatDate(date, format) {
  13769. var yyyy = ('000' + date.getFullYear()).slice(-4),
  13770. yy = yyyy.slice(-2),
  13771. mm = ('0' + (date.getMonth() + 1)).slice(-2),
  13772. dd = ('0' + date.getDate()).slice(-2)
  13773. format = format || 'yyyy-mm-dd'
  13774. return format
  13775. .replace(/yyyy/gi, yyyy)
  13776. .replace(/yy/gi, yy)
  13777. .replace(/mm/gi, mm)
  13778. .replace(/dd/gi, dd)
  13779. }
  13780. this.execCommand(
  13781. 'insertHtml',
  13782. cmd == 'time' ? formatTime(date, format) : formatDate(date, format)
  13783. )
  13784. }
  13785. }
  13786. // plugins/rowspacing.js
  13787. /**
  13788. * 段前段后间距插件
  13789. * @file
  13790. * @since 1.2.6.1
  13791. */
  13792. /**
  13793. * 设置段间距
  13794. * @command rowspacing
  13795. * @method execCommand
  13796. * @param { String } cmd 命令字符串
  13797. * @param { String } value 段间距的值,以px为单位
  13798. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  13799. * @example
  13800. * ```javascript
  13801. * editor.execCommand( 'rowspacing', '10', 'top' );
  13802. * ```
  13803. */
  13804. UE.plugins['rowspacing'] = function() {
  13805. var me = this
  13806. me.setOpt({
  13807. rowspacingtop: ['5', '10', '15', '20', '25'],
  13808. rowspacingbottom: ['5', '10', '15', '20', '25']
  13809. })
  13810. me.commands['rowspacing'] = {
  13811. execCommand: function(cmdName, value, dir) {
  13812. this.execCommand('paragraph', 'p', {
  13813. style: 'margin-' + dir + ':' + value + 'px'
  13814. })
  13815. return true
  13816. },
  13817. queryCommandValue: function(cmdName, dir) {
  13818. var pN = domUtils.filterNodeList(
  13819. this.selection.getStartElementPath(),
  13820. function(node) {
  13821. return domUtils.isBlockElm(node)
  13822. }
  13823. ),
  13824. value
  13825. //trace:1026
  13826. if (pN) {
  13827. value = domUtils
  13828. .getComputedStyle(pN, 'margin-' + dir)
  13829. .replace(/[^\d]/g, '')
  13830. return !value ? 0 : value
  13831. }
  13832. return 0
  13833. }
  13834. }
  13835. }
  13836. // plugins/lineheight.js
  13837. /**
  13838. * 设置行内间距
  13839. * @file
  13840. * @since 1.2.6.1
  13841. */
  13842. UE.plugins['lineheight'] = function() {
  13843. var me = this
  13844. me.setOpt({lineheight: ['1', '1.5', '1.75', '2', '3', '4', '5']})
  13845. /**
  13846. * 行距
  13847. * @command lineheight
  13848. * @method execCommand
  13849. * @param { String } cmdName 命令字符串
  13850. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  13851. * @example
  13852. * ```javascript
  13853. * editor.execCommand( 'lineheight', 1.5);
  13854. * ```
  13855. */
  13856. /**
  13857. * 查询当前选区内容的行高大小
  13858. * @command lineheight
  13859. * @method queryCommandValue
  13860. * @param { String } cmd 命令字符串
  13861. * @return { String } 返回当前行高大小
  13862. * @example
  13863. * ```javascript
  13864. * editor.queryCommandValue( 'lineheight' );
  13865. * ```
  13866. */
  13867. me.commands['lineheight'] = {
  13868. execCommand: function(cmdName, value) {
  13869. this.execCommand('paragraph', 'p', {
  13870. style: 'line-height:' + (value == '1' ? 'normal' : value + 'em')
  13871. })
  13872. return true
  13873. },
  13874. queryCommandValue: function() {
  13875. var pN = domUtils.filterNodeList(
  13876. this.selection.getStartElementPath(),
  13877. function(node) {
  13878. return domUtils.isBlockElm(node)
  13879. }
  13880. )
  13881. if (pN) {
  13882. var value = domUtils.getComputedStyle(pN, 'line-height')
  13883. return value == 'normal' ? 1 : value.replace(/[^\d.]*/gi, '')
  13884. }
  13885. }
  13886. }
  13887. }
  13888. // plugins/insertcode.js
  13889. /**
  13890. * 插入代码插件
  13891. * @file
  13892. * @since 1.2.6.1
  13893. */
  13894. UE.plugins['insertcode'] = function() {
  13895. var me = this
  13896. me.ready(function() {
  13897. utils.cssRule(
  13898. 'pre',
  13899. 'pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}',
  13900. me.document
  13901. )
  13902. })
  13903. me.setOpt('insertcode', {
  13904. as3: 'ActionScript3',
  13905. bash: 'Bash/Shell',
  13906. cpp: 'C/C++',
  13907. css: 'Css',
  13908. cf: 'CodeFunction',
  13909. 'c#': 'C#',
  13910. delphi: 'Delphi',
  13911. diff: 'Diff',
  13912. erlang: 'Erlang',
  13913. groovy: 'Groovy',
  13914. html: 'Html',
  13915. java: 'Java',
  13916. jfx: 'JavaFx',
  13917. js: 'Javascript',
  13918. pl: 'Perl',
  13919. php: 'Php',
  13920. plain: 'Plain Text',
  13921. ps: 'PowerShell',
  13922. python: 'Python',
  13923. ruby: 'Ruby',
  13924. scala: 'Scala',
  13925. sql: 'Sql',
  13926. vb: 'Vb',
  13927. xml: 'Xml'
  13928. })
  13929. /**
  13930. * 插入代码
  13931. * @command insertcode
  13932. * @method execCommand
  13933. * @param { String } cmd 命令字符串
  13934. * @param { String } lang 插入代码的语言
  13935. * @example
  13936. * ```javascript
  13937. * editor.execCommand( 'insertcode', 'javascript' );
  13938. * ```
  13939. */
  13940. /**
  13941. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  13942. * @command insertcode
  13943. * @method queryCommandValue
  13944. * @param { String } cmd 命令字符串
  13945. * @return { String } 返回代码的语言
  13946. * @example
  13947. * ```javascript
  13948. * editor.queryCommandValue( 'insertcode' );
  13949. * ```
  13950. */
  13951. me.commands['insertcode'] = {
  13952. execCommand: function(cmd, lang) {
  13953. var me = this,
  13954. rng = me.selection.getRange(),
  13955. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  13956. if (pre) {
  13957. pre.className = 'brush:' + lang + ';toolbar:false;'
  13958. } else {
  13959. var code = ''
  13960. if (rng.collapsed) {
  13961. code =
  13962. browser.ie && browser.ie11below
  13963. ? browser.version <= 8
  13964. ? '&nbsp;'
  13965. : ''
  13966. : '<br/>'
  13967. } else {
  13968. var frag = rng.extractContents()
  13969. var div = me.document.createElement('div')
  13970. div.appendChild(frag)
  13971. utils.each(
  13972. UE.filterNode(
  13973. UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, '')),
  13974. me.options.filterTxtRules
  13975. ).children,
  13976. function(node) {
  13977. if (browser.ie && browser.ie11below && browser.version > 8) {
  13978. if (node.type == 'element') {
  13979. if (node.tagName == 'br') {
  13980. code += '\n'
  13981. } else if (!dtd.$empty[node.tagName]) {
  13982. utils.each(node.children, function(cn) {
  13983. if (cn.type == 'element') {
  13984. if (cn.tagName == 'br') {
  13985. code += '\n'
  13986. } else if (!dtd.$empty[node.tagName]) {
  13987. code += cn.innerText()
  13988. }
  13989. } else {
  13990. code += cn.data
  13991. }
  13992. })
  13993. if (!/\n$/.test(code)) {
  13994. code += '\n'
  13995. }
  13996. }
  13997. } else {
  13998. code += node.data + '\n'
  13999. }
  14000. if (!node.nextSibling() && /\n$/.test(code)) {
  14001. code = code.replace(/\n$/, '')
  14002. }
  14003. } else {
  14004. if (browser.ie && browser.ie11below) {
  14005. if (node.type == 'element') {
  14006. if (node.tagName == 'br') {
  14007. code += '<br>'
  14008. } else if (!dtd.$empty[node.tagName]) {
  14009. utils.each(node.children, function(cn) {
  14010. if (cn.type == 'element') {
  14011. if (cn.tagName == 'br') {
  14012. code += '<br>'
  14013. } else if (!dtd.$empty[node.tagName]) {
  14014. code += cn.innerText()
  14015. }
  14016. } else {
  14017. code += cn.data
  14018. }
  14019. })
  14020. if (!/br>$/.test(code)) {
  14021. code += '<br>'
  14022. }
  14023. }
  14024. } else {
  14025. code += node.data + '<br>'
  14026. }
  14027. if (!node.nextSibling() && /<br>$/.test(code)) {
  14028. code = code.replace(/<br>$/, '')
  14029. }
  14030. } else {
  14031. code +=
  14032. node.type == 'element'
  14033. ? dtd.$empty[node.tagName]
  14034. ? ''
  14035. : node.innerText()
  14036. : node.data
  14037. if (!/br\/?\s*>$/.test(code)) {
  14038. if (!node.nextSibling()) return
  14039. code += '<br>'
  14040. }
  14041. }
  14042. }
  14043. }
  14044. )
  14045. }
  14046. me.execCommand(
  14047. 'inserthtml',
  14048. '<pre id="coder"class="brush:' +
  14049. lang +
  14050. ';toolbar:false">' +
  14051. code +
  14052. '</pre>',
  14053. true
  14054. )
  14055. pre = me.document.getElementById('coder')
  14056. domUtils.removeAttributes(pre, 'id')
  14057. var tmpNode = pre.previousSibling
  14058. if (
  14059. tmpNode &&
  14060. ((tmpNode.nodeType == 3 &&
  14061. tmpNode.nodeValue.length == 1 &&
  14062. browser.ie &&
  14063. browser.version == 6) ||
  14064. domUtils.isEmptyBlock(tmpNode))
  14065. ) {
  14066. domUtils.remove(tmpNode)
  14067. }
  14068. var rng = me.selection.getRange()
  14069. if (domUtils.isEmptyBlock(pre)) {
  14070. rng.setStart(pre, 0).setCursor(false, true)
  14071. } else {
  14072. rng.selectNodeContents(pre).select()
  14073. }
  14074. }
  14075. },
  14076. queryCommandValue: function() {
  14077. var path = this.selection.getStartElementPath()
  14078. var lang = ''
  14079. utils.each(path, function(node) {
  14080. if (node.nodeName == 'PRE') {
  14081. var match = node.className.match(/brush:([^;]+)/)
  14082. lang = match && match[1] ? match[1] : ''
  14083. return false
  14084. }
  14085. })
  14086. return lang
  14087. }
  14088. }
  14089. me.addInputRule(function(root) {
  14090. utils.each(root.getNodesByTagName('pre'), function(pre) {
  14091. var brs = pre.getNodesByTagName('br')
  14092. if (brs.length) {
  14093. browser.ie &&
  14094. browser.ie11below &&
  14095. browser.version > 8 &&
  14096. utils.each(brs, function(br) {
  14097. var txt = UE.uNode.createText('\n')
  14098. br.parentNode.insertBefore(txt, br)
  14099. br.parentNode.removeChild(br)
  14100. })
  14101. return
  14102. }
  14103. if (browser.ie && browser.ie11below && browser.version > 8) return
  14104. var code = pre.innerText().split(/\n/)
  14105. pre.innerHTML('')
  14106. utils.each(code, function(c) {
  14107. if (c.length) {
  14108. pre.appendChild(UE.uNode.createText(c))
  14109. }
  14110. pre.appendChild(UE.uNode.createElement('br'))
  14111. })
  14112. })
  14113. })
  14114. me.addOutputRule(function(root) {
  14115. utils.each(root.getNodesByTagName('pre'), function(pre) {
  14116. var code = ''
  14117. utils.each(pre.children, function(n) {
  14118. if (n.type == 'text') {
  14119. //在ie下文本内容有可能末尾带有\n要去掉
  14120. //trace:3396
  14121. code += n.data.replace(/[ ]/g, '&nbsp;').replace(/\n$/, '')
  14122. } else {
  14123. if (n.tagName == 'br') {
  14124. code += '\n'
  14125. } else {
  14126. code += !dtd.$empty[n.tagName] ? '' : n.innerText()
  14127. }
  14128. }
  14129. })
  14130. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ''))
  14131. })
  14132. })
  14133. //不需要判断highlight的command列表
  14134. me.notNeedCodeQuery = {
  14135. help: 1,
  14136. undo: 1,
  14137. redo: 1,
  14138. source: 1,
  14139. print: 1,
  14140. searchreplace: 1,
  14141. fullscreen: 1,
  14142. preview: 1,
  14143. insertparagraph: 1,
  14144. elementpath: 1,
  14145. insertcode: 1,
  14146. inserthtml: 1,
  14147. selectall: 1
  14148. }
  14149. //将queyCommamndState重置
  14150. var orgQuery = me.queryCommandState
  14151. me.queryCommandState = function(cmd) {
  14152. var me = this
  14153. if (
  14154. !me.notNeedCodeQuery[cmd.toLowerCase()] &&
  14155. me.selection &&
  14156. me.queryCommandValue('insertcode')
  14157. ) {
  14158. return -1
  14159. }
  14160. return UE.Editor.prototype.queryCommandState.apply(this, arguments)
  14161. }
  14162. me.addListener('beforeenterkeydown', function() {
  14163. var rng = me.selection.getRange()
  14164. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  14165. if (pre) {
  14166. me.fireEvent('saveScene')
  14167. if (!rng.collapsed) {
  14168. rng.deleteContents()
  14169. }
  14170. if (!browser.ie || browser.ie9above) {
  14171. var tmpNode = me.document.createElement('br'),
  14172. pre
  14173. rng
  14174. .insertNode(tmpNode)
  14175. .setStartAfter(tmpNode)
  14176. .collapse(true)
  14177. var next = tmpNode.nextSibling
  14178. if (!next && (!browser.ie || browser.version > 10)) {
  14179. rng.insertNode(tmpNode.cloneNode(false))
  14180. } else {
  14181. rng.setStartAfter(tmpNode)
  14182. }
  14183. pre = tmpNode.previousSibling
  14184. var tmp
  14185. while (pre) {
  14186. tmp = pre
  14187. pre = pre.previousSibling
  14188. if (!pre || pre.nodeName == 'BR') {
  14189. pre = tmp
  14190. break
  14191. }
  14192. }
  14193. if (pre) {
  14194. var str = ''
  14195. while (
  14196. pre &&
  14197. pre.nodeName != 'BR' &&
  14198. new RegExp('^[\\s' + domUtils.fillChar + ']*$').test(
  14199. pre.nodeValue
  14200. )
  14201. ) {
  14202. str += pre.nodeValue
  14203. pre = pre.nextSibling
  14204. }
  14205. if (pre.nodeName != 'BR') {
  14206. var match = pre.nodeValue.match(
  14207. new RegExp('^([\\s' + domUtils.fillChar + ']+)')
  14208. )
  14209. if (match && match[1]) {
  14210. str += match[1]
  14211. }
  14212. }
  14213. if (str) {
  14214. str = me.document.createTextNode(str)
  14215. rng.insertNode(str).setStartAfter(str)
  14216. }
  14217. }
  14218. rng.collapse(true).select(true)
  14219. } else {
  14220. if (browser.version > 8) {
  14221. var txt = me.document.createTextNode('\n')
  14222. var start = rng.startContainer
  14223. if (rng.startOffset == 0) {
  14224. var preNode = start.previousSibling
  14225. if (preNode) {
  14226. rng.insertNode(txt)
  14227. var fillchar = me.document.createTextNode(' ')
  14228. rng
  14229. .setStartAfter(txt)
  14230. .insertNode(fillchar)
  14231. .setStart(fillchar, 0)
  14232. .collapse(true)
  14233. .select(true)
  14234. }
  14235. } else {
  14236. rng.insertNode(txt).setStartAfter(txt)
  14237. var fillchar = me.document.createTextNode(' ')
  14238. start = rng.startContainer.childNodes[rng.startOffset]
  14239. if (start && !/^\n/.test(start.nodeValue)) {
  14240. rng.setStartBefore(txt)
  14241. }
  14242. rng
  14243. .insertNode(fillchar)
  14244. .setStart(fillchar, 0)
  14245. .collapse(true)
  14246. .select(true)
  14247. }
  14248. } else {
  14249. var tmpNode = me.document.createElement('br')
  14250. rng.insertNode(tmpNode)
  14251. rng.insertNode(me.document.createTextNode(domUtils.fillChar))
  14252. rng.setStartAfter(tmpNode)
  14253. pre = tmpNode.previousSibling
  14254. var tmp
  14255. while (pre) {
  14256. tmp = pre
  14257. pre = pre.previousSibling
  14258. if (!pre || pre.nodeName == 'BR') {
  14259. pre = tmp
  14260. break
  14261. }
  14262. }
  14263. if (pre) {
  14264. var str = ''
  14265. while (
  14266. pre &&
  14267. pre.nodeName != 'BR' &&
  14268. new RegExp('^[ ' + domUtils.fillChar + ']*$').test(
  14269. pre.nodeValue
  14270. )
  14271. ) {
  14272. str += pre.nodeValue
  14273. pre = pre.nextSibling
  14274. }
  14275. if (pre.nodeName != 'BR') {
  14276. var match = pre.nodeValue.match(
  14277. new RegExp('^([ ' + domUtils.fillChar + ']+)')
  14278. )
  14279. if (match && match[1]) {
  14280. str += match[1]
  14281. }
  14282. }
  14283. str = me.document.createTextNode(str)
  14284. rng.insertNode(str).setStartAfter(str)
  14285. }
  14286. rng.collapse(true).select()
  14287. }
  14288. }
  14289. me.fireEvent('saveScene')
  14290. return true
  14291. }
  14292. })
  14293. me.addListener('tabkeydown', function(cmd, evt) {
  14294. var rng = me.selection.getRange()
  14295. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  14296. if (pre) {
  14297. me.fireEvent('saveScene')
  14298. if (evt.shiftKey) {
  14299. } else {
  14300. if (!rng.collapsed) {
  14301. var bk = rng.createBookmark()
  14302. var start = bk.start.previousSibling
  14303. while (start) {
  14304. if (pre.firstChild === start && !domUtils.isBr(start)) {
  14305. pre.insertBefore(me.document.createTextNode(' '), start)
  14306. break
  14307. }
  14308. if (domUtils.isBr(start)) {
  14309. pre.insertBefore(
  14310. me.document.createTextNode(' '),
  14311. start.nextSibling
  14312. )
  14313. break
  14314. }
  14315. start = start.previousSibling
  14316. }
  14317. var end = bk.end
  14318. start = bk.start.nextSibling
  14319. if (pre.firstChild === bk.start) {
  14320. pre.insertBefore(
  14321. me.document.createTextNode(' '),
  14322. start.nextSibling
  14323. )
  14324. }
  14325. while (start && start !== end) {
  14326. if (domUtils.isBr(start) && start.nextSibling) {
  14327. if (start.nextSibling === end) {
  14328. break
  14329. }
  14330. pre.insertBefore(
  14331. me.document.createTextNode(' '),
  14332. start.nextSibling
  14333. )
  14334. }
  14335. start = start.nextSibling
  14336. }
  14337. rng.moveToBookmark(bk).select()
  14338. } else {
  14339. var tmpNode = me.document.createTextNode(' ')
  14340. rng
  14341. .insertNode(tmpNode)
  14342. .setStartAfter(tmpNode)
  14343. .collapse(true)
  14344. .select(true)
  14345. }
  14346. }
  14347. me.fireEvent('saveScene')
  14348. return true
  14349. }
  14350. })
  14351. me.addListener('beforeinserthtml', function(evtName, html) {
  14352. var me = this,
  14353. rng = me.selection.getRange(),
  14354. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  14355. if (pre) {
  14356. if (!rng.collapsed) {
  14357. rng.deleteContents()
  14358. }
  14359. var htmlstr = ''
  14360. if (browser.ie && browser.version > 8) {
  14361. utils.each(
  14362. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14363. .children,
  14364. function(node) {
  14365. if (node.type == 'element') {
  14366. if (node.tagName == 'br') {
  14367. htmlstr += '\n'
  14368. } else if (!dtd.$empty[node.tagName]) {
  14369. utils.each(node.children, function(cn) {
  14370. if (cn.type == 'element') {
  14371. if (cn.tagName == 'br') {
  14372. htmlstr += '\n'
  14373. } else if (!dtd.$empty[node.tagName]) {
  14374. htmlstr += cn.innerText()
  14375. }
  14376. } else {
  14377. htmlstr += cn.data
  14378. }
  14379. })
  14380. if (!/\n$/.test(htmlstr)) {
  14381. htmlstr += '\n'
  14382. }
  14383. }
  14384. } else {
  14385. htmlstr += node.data + '\n'
  14386. }
  14387. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  14388. htmlstr = htmlstr.replace(/\n$/, '')
  14389. }
  14390. }
  14391. )
  14392. var tmpNode = me.document.createTextNode(
  14393. utils.html(htmlstr.replace(/&nbsp;/g, ' '))
  14394. )
  14395. rng
  14396. .insertNode(tmpNode)
  14397. .selectNode(tmpNode)
  14398. .select()
  14399. } else {
  14400. var frag = me.document.createDocumentFragment()
  14401. utils.each(
  14402. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14403. .children,
  14404. function(node) {
  14405. if (node.type == 'element') {
  14406. if (node.tagName == 'br') {
  14407. frag.appendChild(me.document.createElement('br'))
  14408. } else if (!dtd.$empty[node.tagName]) {
  14409. utils.each(node.children, function(cn) {
  14410. if (cn.type == 'element') {
  14411. if (cn.tagName == 'br') {
  14412. frag.appendChild(me.document.createElement('br'))
  14413. } else if (!dtd.$empty[node.tagName]) {
  14414. frag.appendChild(
  14415. me.document.createTextNode(
  14416. utils.html(cn.innerText().replace(/&nbsp;/g, ' '))
  14417. )
  14418. )
  14419. }
  14420. } else {
  14421. frag.appendChild(
  14422. me.document.createTextNode(
  14423. utils.html(cn.data.replace(/&nbsp;/g, ' '))
  14424. )
  14425. )
  14426. }
  14427. })
  14428. if (frag.lastChild.nodeName != 'BR') {
  14429. frag.appendChild(me.document.createElement('br'))
  14430. }
  14431. }
  14432. } else {
  14433. frag.appendChild(
  14434. me.document.createTextNode(
  14435. utils.html(node.data.replace(/&nbsp;/g, ' '))
  14436. )
  14437. )
  14438. }
  14439. if (!node.nextSibling() && frag.lastChild.nodeName == 'BR') {
  14440. frag.removeChild(frag.lastChild)
  14441. }
  14442. }
  14443. )
  14444. rng.insertNode(frag).select()
  14445. }
  14446. return true
  14447. }
  14448. })
  14449. //方向键的处理
  14450. me.addListener('keydown', function(cmd, evt) {
  14451. var me = this,
  14452. keyCode = evt.keyCode || evt.which
  14453. if (keyCode == 40) {
  14454. var rng = me.selection.getRange(),
  14455. pre,
  14456. start = rng.startContainer
  14457. if (
  14458. rng.collapsed &&
  14459. (pre = domUtils.findParentByTagName(
  14460. rng.startContainer,
  14461. 'pre',
  14462. true
  14463. )) &&
  14464. !pre.nextSibling
  14465. ) {
  14466. var last = pre.lastChild
  14467. while (last && last.nodeName == 'BR') {
  14468. last = last.previousSibling
  14469. }
  14470. if (
  14471. last === start ||
  14472. (rng.startContainer === pre &&
  14473. rng.startOffset == pre.childNodes.length)
  14474. ) {
  14475. me.execCommand('insertparagraph')
  14476. domUtils.preventDefault(evt)
  14477. }
  14478. }
  14479. }
  14480. })
  14481. //trace:3395
  14482. me.addListener('delkeydown', function(type, evt) {
  14483. var rng = this.selection.getRange()
  14484. rng.txtToElmBoundary(true)
  14485. var start = rng.startContainer
  14486. if (
  14487. domUtils.isTagNode(start, 'pre') &&
  14488. rng.collapsed &&
  14489. domUtils.isStartInblock(rng)
  14490. ) {
  14491. var p = me.document.createElement('p')
  14492. domUtils.fillNode(me.document, p)
  14493. start.parentNode.insertBefore(p, start)
  14494. domUtils.remove(start)
  14495. rng.setStart(p, 0).setCursor(false, true)
  14496. domUtils.preventDefault(evt)
  14497. return true
  14498. }
  14499. })
  14500. }
  14501. // plugins/cleardoc.js
  14502. /**
  14503. * 清空文档插件
  14504. * @file
  14505. * @since 1.2.6.1
  14506. */
  14507. /**
  14508. * 清空文档
  14509. * @command cleardoc
  14510. * @method execCommand
  14511. * @param { String } cmd 命令字符串
  14512. * @example
  14513. * ```javascript
  14514. * //editor 是编辑器实例
  14515. * editor.execCommand('cleardoc');
  14516. * ```
  14517. */
  14518. UE.commands['cleardoc'] = {
  14519. execCommand: function(cmdName) {
  14520. var me = this,
  14521. enterTag = me.options.enterTag,
  14522. range = me.selection.getRange()
  14523. if (enterTag == 'br') {
  14524. me.body.innerHTML = '<br/>'
  14525. range.setStart(me.body, 0).setCursor()
  14526. } else {
  14527. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>'
  14528. range.setStart(me.body.firstChild, 0).setCursor(false, true)
  14529. }
  14530. setTimeout(function() {
  14531. me.fireEvent('clearDoc')
  14532. }, 0)
  14533. }
  14534. }
  14535. // plugins/anchor.js
  14536. /**
  14537. * 锚点插件,为UEditor提供插入锚点支持
  14538. * @file
  14539. * @since 1.2.6.1
  14540. */
  14541. UE.plugin.register('anchor', function() {
  14542. return {
  14543. bindEvents: {
  14544. ready: function() {
  14545. utils.cssRule(
  14546. 'anchor',
  14547. ".anchorclass{background: url('" +
  14548. this.options.themePath +
  14549. this.options.theme +
  14550. "/images/anchor.gif') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}",
  14551. this.document
  14552. )
  14553. }
  14554. },
  14555. outputRule: function(root) {
  14556. utils.each(root.getNodesByTagName('img'), function(a) {
  14557. var val
  14558. if ((val = a.getAttr('anchorname'))) {
  14559. a.tagName = 'a'
  14560. a.setAttr({
  14561. anchorname: '',
  14562. name: val,
  14563. class: ''
  14564. })
  14565. }
  14566. })
  14567. },
  14568. inputRule: function(root) {
  14569. utils.each(root.getNodesByTagName('a'), function(a) {
  14570. var val
  14571. if ((val = a.getAttr('name')) && !a.getAttr('href')) {
  14572. a.tagName = 'img'
  14573. a.setAttr({
  14574. anchorname: a.getAttr('name'),
  14575. class: 'anchorclass'
  14576. })
  14577. a.setAttr('name')
  14578. }
  14579. })
  14580. },
  14581. commands: {
  14582. /**
  14583. * 插入锚点
  14584. * @command anchor
  14585. * @method execCommand
  14586. * @param { String } cmd 命令字符串
  14587. * @param { String } name 锚点名称字符串
  14588. * @example
  14589. * ```javascript
  14590. * //editor 是编辑器实例
  14591. * editor.execCommand('anchor', 'anchor1');
  14592. * ```
  14593. */
  14594. anchor: {
  14595. execCommand: function(cmd, name) {
  14596. var range = this.selection.getRange(),
  14597. img = range.getClosedNode()
  14598. if (img && img.getAttribute('anchorname')) {
  14599. if (name) {
  14600. img.setAttribute('anchorname', name)
  14601. } else {
  14602. range.setStartBefore(img).setCursor()
  14603. domUtils.remove(img)
  14604. }
  14605. } else {
  14606. if (name) {
  14607. //只在选区的开始插入
  14608. var anchor = this.document.createElement('img')
  14609. range.collapse(true)
  14610. domUtils.setAttributes(anchor, {
  14611. anchorname: name,
  14612. class: 'anchorclass'
  14613. })
  14614. range
  14615. .insertNode(anchor)
  14616. .setStartAfter(anchor)
  14617. .setCursor(false, true)
  14618. }
  14619. }
  14620. }
  14621. }
  14622. }
  14623. }
  14624. })
  14625. // plugins/wordcount.js
  14626. ///import core
  14627. ///commands 字数统计
  14628. ///commandsName WordCount,wordCount
  14629. ///commandsTitle 字数统计
  14630. /*
  14631. * Created by JetBrains WebStorm.
  14632. * User: taoqili
  14633. * Date: 11-9-7
  14634. * Time: 下午8:18
  14635. * To change this template use File | Settings | File Templates.
  14636. */
  14637. UE.plugins['wordcount'] = function() {
  14638. var me = this
  14639. me.setOpt('wordCount', true)
  14640. me.addListener('contentchange', function() {
  14641. me.fireEvent('wordcount')
  14642. })
  14643. var timer
  14644. me.addListener('ready', function() {
  14645. var me = this
  14646. domUtils.on(me.body, 'keyup', function(evt) {
  14647. var code = evt.keyCode || evt.which,
  14648. //忽略的按键,ctr,alt,shift,方向键
  14649. ignores = {
  14650. '16': 1,
  14651. '18': 1,
  14652. '20': 1,
  14653. '37': 1,
  14654. '38': 1,
  14655. '39': 1,
  14656. '40': 1
  14657. }
  14658. if (code in ignores) return
  14659. clearTimeout(timer)
  14660. timer = setTimeout(function() {
  14661. me.fireEvent('wordcount')
  14662. }, 200)
  14663. })
  14664. })
  14665. }
  14666. // plugins/pagebreak.js
  14667. /**
  14668. * 分页功能插件
  14669. * @file
  14670. * @since 1.2.6.1
  14671. */
  14672. UE.plugins['pagebreak'] = function() {
  14673. var me = this,
  14674. notBreakTags = ['td']
  14675. me.setOpt('pageBreakTag', '_ueditor_page_break_tag_')
  14676. function fillNode(node) {
  14677. if (domUtils.isEmptyBlock(node)) {
  14678. var firstChild = node.firstChild,
  14679. tmpNode
  14680. while (
  14681. firstChild &&
  14682. firstChild.nodeType == 1 &&
  14683. domUtils.isEmptyBlock(firstChild)
  14684. ) {
  14685. tmpNode = firstChild
  14686. firstChild = firstChild.firstChild
  14687. }
  14688. !tmpNode && (tmpNode = node)
  14689. domUtils.fillNode(me.document, tmpNode)
  14690. }
  14691. }
  14692. //分页符样式添加
  14693. me.ready(function() {
  14694. utils.cssRule(
  14695. 'pagebreak',
  14696. '.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}',
  14697. me.document
  14698. )
  14699. })
  14700. function isHr(node) {
  14701. return (
  14702. node &&
  14703. node.nodeType == 1 &&
  14704. node.tagName == 'HR' &&
  14705. node.className == 'pagebreak'
  14706. )
  14707. }
  14708. me.addInputRule(function(root) {
  14709. root.traversal(function(node) {
  14710. if (node.type == 'text' && node.data == me.options.pageBreakTag) {
  14711. var hr = UE.uNode.createElement(
  14712. '<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">'
  14713. )
  14714. node.parentNode.insertBefore(hr, node)
  14715. node.parentNode.removeChild(node)
  14716. }
  14717. })
  14718. })
  14719. me.addOutputRule(function(node) {
  14720. utils.each(node.getNodesByTagName('hr'), function(n) {
  14721. if (n.getAttr('class') == 'pagebreak') {
  14722. var txt = UE.uNode.createText(me.options.pageBreakTag)
  14723. n.parentNode.insertBefore(txt, n)
  14724. n.parentNode.removeChild(n)
  14725. }
  14726. })
  14727. })
  14728. /**
  14729. * 插入分页符
  14730. * @command pagebreak
  14731. * @method execCommand
  14732. * @param { String } cmd 命令字符串
  14733. * @remind 在表格中插入分页符会把表格切分成两部分
  14734. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  14735. * 以便于提交数据到服务器端后处理分页。
  14736. * @example
  14737. * ```javascript
  14738. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  14739. * ```
  14740. */
  14741. me.commands['pagebreak'] = {
  14742. execCommand: function() {
  14743. var range = me.selection.getRange(),
  14744. hr = me.document.createElement('hr')
  14745. domUtils.setAttributes(hr, {
  14746. class: 'pagebreak',
  14747. noshade: 'noshade',
  14748. size: '5'
  14749. })
  14750. domUtils.unSelectable(hr)
  14751. //table单独处理
  14752. var node = domUtils.findParentByTagName(
  14753. range.startContainer,
  14754. notBreakTags,
  14755. true
  14756. ),
  14757. parents = [],
  14758. pN
  14759. if (node) {
  14760. switch (node.tagName) {
  14761. case 'TD':
  14762. pN = node.parentNode
  14763. if (!pN.previousSibling) {
  14764. var table = domUtils.findParentByTagName(pN, 'table')
  14765. // var tableWrapDiv = table.parentNode;
  14766. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  14767. // && tableWrapDiv.tagName == 'DIV'
  14768. // && tableWrapDiv.getAttribute('dropdrag')
  14769. // ){
  14770. // domUtils.remove(tableWrapDiv,true);
  14771. // }
  14772. table.parentNode.insertBefore(hr, table)
  14773. parents = domUtils.findParents(hr, true)
  14774. } else {
  14775. pN.parentNode.insertBefore(hr, pN)
  14776. parents = domUtils.findParents(hr)
  14777. }
  14778. pN = parents[1]
  14779. if (hr !== pN) {
  14780. domUtils.breakParent(hr, pN)
  14781. }
  14782. //table要重写绑定一下拖拽
  14783. me.fireEvent('afteradjusttable', me.document)
  14784. }
  14785. } else {
  14786. if (!range.collapsed) {
  14787. range.deleteContents()
  14788. var start = range.startContainer
  14789. while (
  14790. !domUtils.isBody(start) &&
  14791. domUtils.isBlockElm(start) &&
  14792. domUtils.isEmptyNode(start)
  14793. ) {
  14794. range.setStartBefore(start).collapse(true)
  14795. domUtils.remove(start)
  14796. start = range.startContainer
  14797. }
  14798. }
  14799. range.insertNode(hr)
  14800. var pN = hr.parentNode,
  14801. nextNode
  14802. while (!domUtils.isBody(pN)) {
  14803. domUtils.breakParent(hr, pN)
  14804. nextNode = hr.nextSibling
  14805. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  14806. domUtils.remove(nextNode)
  14807. }
  14808. pN = hr.parentNode
  14809. }
  14810. nextNode = hr.nextSibling
  14811. var pre = hr.previousSibling
  14812. if (isHr(pre)) {
  14813. domUtils.remove(pre)
  14814. } else {
  14815. pre && fillNode(pre)
  14816. }
  14817. if (!nextNode) {
  14818. var p = me.document.createElement('p')
  14819. hr.parentNode.appendChild(p)
  14820. domUtils.fillNode(me.document, p)
  14821. range.setStart(p, 0).collapse(true)
  14822. } else {
  14823. if (isHr(nextNode)) {
  14824. domUtils.remove(nextNode)
  14825. } else {
  14826. fillNode(nextNode)
  14827. }
  14828. range.setEndAfter(hr).collapse(false)
  14829. }
  14830. range.select(true)
  14831. }
  14832. }
  14833. }
  14834. }
  14835. // plugins/wordimage.js
  14836. ///import core
  14837. ///commands 本地图片引导上传
  14838. ///commandsName WordImage
  14839. ///commandsTitle 本地图片引导上传
  14840. ///commandsDialog dialogs\wordimage
  14841. UE.plugin.register('wordimage', function() {
  14842. var me = this,
  14843. images = []
  14844. return {
  14845. commands: {
  14846. wordimage: {
  14847. execCommand: function() {
  14848. var images = domUtils.getElementsByTagName(me.body, 'img')
  14849. var urlList = []
  14850. for (var i = 0, ci; (ci = images[i++]); ) {
  14851. var url = ci.getAttribute('word_img')
  14852. url && urlList.push(url)
  14853. }
  14854. return urlList
  14855. },
  14856. queryCommandState: function() {
  14857. images = domUtils.getElementsByTagName(me.body, 'img')
  14858. for (var i = 0, ci; (ci = images[i++]); ) {
  14859. if (ci.getAttribute('word_img')) {
  14860. return 1
  14861. }
  14862. }
  14863. return -1
  14864. },
  14865. notNeedUndo: true
  14866. }
  14867. },
  14868. inputRule: function(root) {
  14869. utils.each(root.getNodesByTagName('img'), function(img) {
  14870. var attrs = img.attrs,
  14871. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  14872. opt = me.options,
  14873. src = opt.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif'
  14874. if (attrs['src'] && /^(?:(file:\/+))/.test(attrs['src'])) {
  14875. img.setAttr({
  14876. width: attrs.width,
  14877. height: attrs.height,
  14878. alt: attrs.alt,
  14879. word_img: attrs.src,
  14880. src: src,
  14881. style:
  14882. 'background:url(' +
  14883. (flag
  14884. ? opt.themePath + opt.theme + '/images/word.gif'
  14885. : opt.langPath + opt.lang + '/images/localimage.png') +
  14886. ') no-repeat center center;border:1px solid #ddd'
  14887. })
  14888. }
  14889. })
  14890. }
  14891. }
  14892. })
  14893. // plugins/dragdrop.js
  14894. UE.plugins['dragdrop'] = function() {
  14895. var me = this
  14896. me.ready(function() {
  14897. domUtils.on(this.body, 'dragend', function() {
  14898. var rng = me.selection.getRange()
  14899. var node = rng.getClosedNode() || me.selection.getStart()
  14900. if (node && node.tagName == 'IMG') {
  14901. var pre = node.previousSibling,
  14902. next
  14903. while ((next = node.nextSibling)) {
  14904. if (
  14905. next.nodeType == 1 &&
  14906. next.tagName == 'SPAN' &&
  14907. !next.firstChild
  14908. ) {
  14909. domUtils.remove(next)
  14910. } else {
  14911. break
  14912. }
  14913. }
  14914. if (
  14915. ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre)) ||
  14916. !pre) &&
  14917. (!next || (next && !domUtils.isEmptyBlock(next)))
  14918. ) {
  14919. if (pre && pre.tagName == 'P' && !domUtils.isEmptyBlock(pre)) {
  14920. pre.appendChild(node)
  14921. domUtils.moveChild(next, pre)
  14922. domUtils.remove(next)
  14923. } else if (
  14924. next &&
  14925. next.tagName == 'P' &&
  14926. !domUtils.isEmptyBlock(next)
  14927. ) {
  14928. next.insertBefore(node, next.firstChild)
  14929. }
  14930. if (pre && pre.tagName == 'P' && domUtils.isEmptyBlock(pre)) {
  14931. domUtils.remove(pre)
  14932. }
  14933. if (next && next.tagName == 'P' && domUtils.isEmptyBlock(next)) {
  14934. domUtils.remove(next)
  14935. }
  14936. rng.selectNode(node).select()
  14937. me.fireEvent('saveScene')
  14938. }
  14939. }
  14940. })
  14941. })
  14942. me.addListener('keyup', function(type, evt) {
  14943. var keyCode = evt.keyCode || evt.which
  14944. if (keyCode == 13) {
  14945. var rng = me.selection.getRange(),
  14946. node
  14947. if (
  14948. (node = domUtils.findParentByTagName(rng.startContainer, 'p', true))
  14949. ) {
  14950. if (domUtils.getComputedStyle(node, 'text-align') == 'center') {
  14951. domUtils.removeStyle(node, 'text-align')
  14952. }
  14953. }
  14954. }
  14955. })
  14956. }
  14957. // plugins/undo.js
  14958. /**
  14959. * undo redo
  14960. * @file
  14961. * @since 1.2.6.1
  14962. */
  14963. /**
  14964. * 撤销上一次执行的命令
  14965. * @command undo
  14966. * @method execCommand
  14967. * @param { String } cmd 命令字符串
  14968. * @example
  14969. * ```javascript
  14970. * editor.execCommand( 'undo' );
  14971. * ```
  14972. */
  14973. /**
  14974. * 重做上一次执行的命令
  14975. * @command redo
  14976. * @method execCommand
  14977. * @param { String } cmd 命令字符串
  14978. * @example
  14979. * ```javascript
  14980. * editor.execCommand( 'redo' );
  14981. * ```
  14982. */
  14983. UE.plugins['undo'] = function() {
  14984. var saveSceneTimer
  14985. var me = this,
  14986. maxUndoCount = me.options.maxUndoCount || 20,
  14987. maxInputCount = me.options.maxInputCount || 20,
  14988. fillchar = new RegExp(domUtils.fillChar + '|</hr>', 'gi') // ie会产生多余的</hr>
  14989. var noNeedFillCharTags = {
  14990. ol: 1,
  14991. ul: 1,
  14992. table: 1,
  14993. tbody: 1,
  14994. tr: 1,
  14995. body: 1
  14996. }
  14997. var orgState = me.options.autoClearEmptyNode
  14998. function compareAddr(indexA, indexB) {
  14999. if (indexA.length != indexB.length) return 0
  15000. for (var i = 0, l = indexA.length; i < l; i++) {
  15001. if (indexA[i] != indexB[i]) return 0
  15002. }
  15003. return 1
  15004. }
  15005. function compareRangeAddress(rngAddrA, rngAddrB) {
  15006. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  15007. return 0
  15008. }
  15009. if (
  15010. !compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) ||
  15011. !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)
  15012. ) {
  15013. return 0
  15014. }
  15015. return 1
  15016. }
  15017. function UndoManager() {
  15018. this.list = []
  15019. this.index = 0
  15020. this.hasUndo = false
  15021. this.hasRedo = false
  15022. this.undo = function() {
  15023. if (this.hasUndo) {
  15024. if (!this.list[this.index - 1] && this.list.length == 1) {
  15025. this.reset()
  15026. return
  15027. }
  15028. while (
  15029. this.list[this.index].content == this.list[this.index - 1].content
  15030. ) {
  15031. this.index--
  15032. if (this.index == 0) {
  15033. return this.restore(0)
  15034. }
  15035. }
  15036. this.restore(--this.index)
  15037. }
  15038. }
  15039. this.redo = function() {
  15040. if (this.hasRedo) {
  15041. while (
  15042. this.list[this.index].content == this.list[this.index + 1].content
  15043. ) {
  15044. this.index++
  15045. if (this.index == this.list.length - 1) {
  15046. return this.restore(this.index)
  15047. }
  15048. }
  15049. this.restore(++this.index)
  15050. }
  15051. }
  15052. this.restore = function() {
  15053. var me = this.editor
  15054. var scene = this.list[this.index]
  15055. var root = UE.htmlparser(scene.content.replace(fillchar, ''))
  15056. me.options.autoClearEmptyNode = false
  15057. me.filterInputRule(root)
  15058. me.options.autoClearEmptyNode = orgState
  15059. //trace:873
  15060. //去掉展位符
  15061. me.document.body.innerHTML = root.toHtml()
  15062. me.fireEvent('afterscencerestore')
  15063. //处理undo后空格不展位的问题
  15064. if (browser.ie) {
  15065. utils.each(
  15066. domUtils.getElementsByTagName(me.document, 'td th caption p'),
  15067. function(node) {
  15068. if (domUtils.isEmptyNode(node)) {
  15069. domUtils.fillNode(me.document, node)
  15070. }
  15071. }
  15072. )
  15073. }
  15074. try {
  15075. var rng = new dom.Range(me.document).moveToAddress(scene.address)
  15076. rng.select(
  15077. noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]
  15078. )
  15079. } catch (e) {}
  15080. this.update()
  15081. this.clearKey()
  15082. //不能把自己reset了
  15083. me.fireEvent('reset', true)
  15084. }
  15085. this.getScene = function() {
  15086. var me = this.editor
  15087. var rng = me.selection.getRange(),
  15088. rngAddress = rng.createAddress(false, true)
  15089. me.fireEvent('beforegetscene')
  15090. var root = UE.htmlparser(me.body.innerHTML)
  15091. me.options.autoClearEmptyNode = false
  15092. me.filterOutputRule(root)
  15093. me.options.autoClearEmptyNode = orgState
  15094. var cont = root.toHtml()
  15095. //trace:3461
  15096. //这个会引起回退时导致空格丢失的情况
  15097. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  15098. me.fireEvent('aftergetscene')
  15099. return {
  15100. address: rngAddress,
  15101. content: cont
  15102. }
  15103. }
  15104. this.save = function(notCompareRange, notSetCursor) {
  15105. clearTimeout(saveSceneTimer)
  15106. var currentScene = this.getScene(notSetCursor),
  15107. lastScene = this.list[this.index]
  15108. if (lastScene && lastScene.content != currentScene.content) {
  15109. me.trigger('contentchange')
  15110. }
  15111. //内容相同位置相同不存
  15112. if (
  15113. lastScene &&
  15114. lastScene.content == currentScene.content &&
  15115. (notCompareRange
  15116. ? 1
  15117. : compareRangeAddress(lastScene.address, currentScene.address))
  15118. ) {
  15119. return
  15120. }
  15121. this.list = this.list.slice(0, this.index + 1)
  15122. this.list.push(currentScene)
  15123. //如果大于最大数量了,就把最前的剔除
  15124. if (this.list.length > maxUndoCount) {
  15125. this.list.shift()
  15126. }
  15127. this.index = this.list.length - 1
  15128. this.clearKey()
  15129. //跟新undo/redo状态
  15130. this.update()
  15131. }
  15132. this.update = function() {
  15133. this.hasRedo = !!this.list[this.index + 1]
  15134. this.hasUndo = !!this.list[this.index - 1]
  15135. }
  15136. this.reset = function() {
  15137. this.list = []
  15138. this.index = 0
  15139. this.hasUndo = false
  15140. this.hasRedo = false
  15141. this.clearKey()
  15142. }
  15143. this.clearKey = function() {
  15144. keycont = 0
  15145. lastKeyCode = null
  15146. }
  15147. }
  15148. me.undoManger = new UndoManager()
  15149. me.undoManger.editor = me
  15150. function saveScene() {
  15151. this.undoManger.save()
  15152. }
  15153. me.addListener('saveScene', function() {
  15154. var args = Array.prototype.splice.call(arguments, 1)
  15155. this.undoManger.save.apply(this.undoManger, args)
  15156. })
  15157. // me.addListener('beforeexeccommand', saveScene);
  15158. // me.addListener('afterexeccommand', saveScene);
  15159. me.addListener('reset', function(type, exclude) {
  15160. if (!exclude) {
  15161. this.undoManger.reset()
  15162. }
  15163. })
  15164. me.commands['redo'] = me.commands['undo'] = {
  15165. execCommand: function(cmdName) {
  15166. this.undoManger[cmdName]()
  15167. },
  15168. queryCommandState: function(cmdName) {
  15169. return this.undoManger[
  15170. 'has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')
  15171. ]
  15172. ? 0
  15173. : -1
  15174. },
  15175. notNeedUndo: 1
  15176. }
  15177. var keys = {
  15178. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  15179. /*Shift*/ 16: 1,
  15180. /*Ctrl*/ 17: 1,
  15181. /*Alt*/ 18: 1,
  15182. 37: 1,
  15183. 38: 1,
  15184. 39: 1,
  15185. 40: 1
  15186. },
  15187. keycont = 0,
  15188. lastKeyCode
  15189. //输入法状态下不计算字符数
  15190. var inputType = false
  15191. me.addListener('ready', function() {
  15192. domUtils.on(this.body, 'compositionstart', function() {
  15193. inputType = true
  15194. })
  15195. domUtils.on(this.body, 'compositionend', function() {
  15196. inputType = false
  15197. })
  15198. })
  15199. //快捷键
  15200. me.addshortcutkey({
  15201. Undo: 'ctrl+90', //undo
  15202. Redo: 'ctrl+89' //redo
  15203. })
  15204. var isCollapsed = true
  15205. me.addListener('keydown', function(type, evt) {
  15206. var me = this
  15207. var keyCode = evt.keyCode || evt.which
  15208. if (
  15209. !keys[keyCode] &&
  15210. !evt.ctrlKey &&
  15211. !evt.metaKey &&
  15212. !evt.shiftKey &&
  15213. !evt.altKey
  15214. ) {
  15215. if (inputType) return
  15216. if (!me.selection.getRange().collapsed) {
  15217. me.undoManger.save(false, true)
  15218. isCollapsed = false
  15219. return
  15220. }
  15221. if (me.undoManger.list.length == 0) {
  15222. me.undoManger.save(true)
  15223. }
  15224. clearTimeout(saveSceneTimer)
  15225. function save(cont) {
  15226. cont.undoManger.save(false, true)
  15227. cont.fireEvent('selectionchange')
  15228. }
  15229. saveSceneTimer = setTimeout(function() {
  15230. if (inputType) {
  15231. var interalTimer = setInterval(function() {
  15232. if (!inputType) {
  15233. save(me)
  15234. clearInterval(interalTimer)
  15235. }
  15236. }, 300)
  15237. return
  15238. }
  15239. save(me)
  15240. }, 200)
  15241. lastKeyCode = keyCode
  15242. keycont++
  15243. if (keycont >= maxInputCount) {
  15244. save(me)
  15245. }
  15246. }
  15247. })
  15248. me.addListener('keyup', function(type, evt) {
  15249. var keyCode = evt.keyCode || evt.which
  15250. if (
  15251. !keys[keyCode] &&
  15252. !evt.ctrlKey &&
  15253. !evt.metaKey &&
  15254. !evt.shiftKey &&
  15255. !evt.altKey
  15256. ) {
  15257. if (inputType) return
  15258. if (!isCollapsed) {
  15259. this.undoManger.save(false, true)
  15260. isCollapsed = true
  15261. }
  15262. }
  15263. })
  15264. //扩展实例,添加关闭和开启命令undo
  15265. me.stopCmdUndo = function() {
  15266. me.__hasEnterExecCommand = true
  15267. }
  15268. me.startCmdUndo = function() {
  15269. me.__hasEnterExecCommand = false
  15270. }
  15271. }
  15272. // plugins/copy.js
  15273. UE.plugin.register('copy', function() {
  15274. var me = this
  15275. function initZeroClipboard() {
  15276. ZeroClipboard.config({
  15277. debug: false,
  15278. swfPath:
  15279. me.options.UEDITOR_HOME_URL +
  15280. 'third-party/zeroclipboard/ZeroClipboard.swf'
  15281. })
  15282. var client = (me.zeroclipboard = new ZeroClipboard())
  15283. // 复制内容
  15284. client.on('copy', function(e) {
  15285. var client = e.client,
  15286. rng = me.selection.getRange(),
  15287. div = document.createElement('div')
  15288. div.appendChild(rng.cloneContents())
  15289. client.setText(div.innerText || div.textContent)
  15290. client.setHtml(div.innerHTML)
  15291. rng.select()
  15292. })
  15293. // hover事件传递到target
  15294. client.on('mouseover mouseout', function(e) {
  15295. var target = e.target
  15296. if (e.type == 'mouseover') {
  15297. domUtils.addClass(target, 'edui-state-hover')
  15298. } else if (e.type == 'mouseout') {
  15299. domUtils.removeClasses(target, 'edui-state-hover')
  15300. }
  15301. })
  15302. // flash加载不成功
  15303. client.on('wrongflash noflash', function() {
  15304. ZeroClipboard.destroy()
  15305. })
  15306. }
  15307. return {
  15308. bindEvents: {
  15309. ready: function() {
  15310. if (!browser.ie) {
  15311. if (window.ZeroClipboard) {
  15312. initZeroClipboard()
  15313. } else {
  15314. utils.loadFile(
  15315. document,
  15316. {
  15317. src:
  15318. me.options.UEDITOR_HOME_URL +
  15319. 'third-party/zeroclipboard/ZeroClipboard.js',
  15320. tag: 'script',
  15321. type: 'text/javascript',
  15322. defer: 'defer'
  15323. },
  15324. function() {
  15325. initZeroClipboard()
  15326. }
  15327. )
  15328. }
  15329. }
  15330. }
  15331. },
  15332. commands: {
  15333. copy: {
  15334. execCommand: function(cmd) {
  15335. if (!me.document.execCommand('copy')) {
  15336. alert(me.getLang('copymsg'))
  15337. }
  15338. }
  15339. }
  15340. }
  15341. }
  15342. })
  15343. // plugins/paste.js
  15344. ///import core
  15345. ///import plugins/inserthtml.js
  15346. ///import plugins/undo.js
  15347. ///import plugins/serialize.js
  15348. ///commands 粘贴
  15349. ///commandsName PastePlain
  15350. ///commandsTitle 纯文本粘贴模式
  15351. /**
  15352. * @description 粘贴
  15353. * @author zhanyi
  15354. */
  15355. UE.plugins['paste'] = function() {
  15356. function getClipboardData(callback) {
  15357. var doc = this.document
  15358. if (doc.getElementById('baidu_pastebin')) {
  15359. return
  15360. }
  15361. var range = this.selection.getRange(),
  15362. bk = range.createBookmark(),
  15363. //创建剪贴的容器div
  15364. pastebin = doc.createElement('div')
  15365. pastebin.id = 'baidu_pastebin'
  15366. // Safari 要求div必须有内容,才能粘贴内容进来
  15367. browser.webkit &&
  15368. pastebin.appendChild(
  15369. doc.createTextNode(domUtils.fillChar + domUtils.fillChar)
  15370. )
  15371. doc.body.appendChild(pastebin)
  15372. //trace:717 隐藏的span不能得到top
  15373. //bk.start.innerHTML = '&nbsp;';
  15374. bk.start.style.display = ''
  15375. pastebin.style.cssText =
  15376. 'position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:' +
  15377. //要在现在光标平行的位置加入,否则会出现跳动的问题
  15378. domUtils.getXY(bk.start).y +
  15379. 'px'
  15380. range.selectNodeContents(pastebin).select(true)
  15381. setTimeout(function() {
  15382. if (browser.webkit) {
  15383. for (
  15384. var i = 0, pastebins = doc.querySelectorAll('#baidu_pastebin'), pi;
  15385. (pi = pastebins[i++]);
  15386. ) {
  15387. if (domUtils.isEmptyNode(pi)) {
  15388. domUtils.remove(pi)
  15389. } else {
  15390. pastebin = pi
  15391. break
  15392. }
  15393. }
  15394. }
  15395. try {
  15396. pastebin.parentNode.removeChild(pastebin)
  15397. } catch (e) {}
  15398. range.moveToBookmark(bk).select(true)
  15399. callback(pastebin)
  15400. }, 0)
  15401. }
  15402. var me = this
  15403. me.setOpt({
  15404. retainOnlyLabelPasted: false
  15405. })
  15406. var txtContent, htmlContent, address
  15407. function getPureHtml(html) {
  15408. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function(
  15409. a,
  15410. b,
  15411. tagName,
  15412. attrs
  15413. ) {
  15414. tagName = tagName.toLowerCase()
  15415. if ({img: 1}[tagName]) {
  15416. return a
  15417. }
  15418. attrs = attrs.replace(
  15419. /([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi,
  15420. function(str, atr, val) {
  15421. if (
  15422. {
  15423. src: 1,
  15424. href: 1,
  15425. name: 1
  15426. }[atr.toLowerCase()]
  15427. ) {
  15428. return atr + '=' + val + ' '
  15429. }
  15430. return ''
  15431. }
  15432. )
  15433. if (
  15434. {
  15435. span: 1,
  15436. div: 1
  15437. }[tagName]
  15438. ) {
  15439. return ''
  15440. } else {
  15441. return '<' + b + tagName + ' ' + utils.trim(attrs) + '>'
  15442. }
  15443. })
  15444. }
  15445. function filter(div) {
  15446. var html
  15447. if (div.firstChild) {
  15448. //去掉cut中添加的边界值
  15449. var nodes = domUtils.getElementsByTagName(div, 'span')
  15450. for (var i = 0, ni; (ni = nodes[i++]); ) {
  15451. if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') {
  15452. domUtils.remove(ni)
  15453. }
  15454. }
  15455. if (browser.webkit) {
  15456. var brs = div.querySelectorAll('div br')
  15457. for (var i = 0, bi; (bi = brs[i++]); ) {
  15458. var pN = bi.parentNode
  15459. if (pN.tagName == 'DIV' && pN.childNodes.length == 1) {
  15460. pN.innerHTML = '<p><br/></p>'
  15461. domUtils.remove(pN)
  15462. }
  15463. }
  15464. var divs = div.querySelectorAll('#baidu_pastebin')
  15465. for (var i = 0, di; (di = divs[i++]); ) {
  15466. var tmpP = me.document.createElement('p')
  15467. di.parentNode.insertBefore(tmpP, di)
  15468. while (di.firstChild) {
  15469. tmpP.appendChild(di.firstChild)
  15470. }
  15471. domUtils.remove(di)
  15472. }
  15473. var metas = div.querySelectorAll('meta')
  15474. for (var i = 0, ci; (ci = metas[i++]); ) {
  15475. domUtils.remove(ci)
  15476. }
  15477. var brs = div.querySelectorAll('br')
  15478. for (i = 0; (ci = brs[i++]); ) {
  15479. if (/^apple-/i.test(ci.className)) {
  15480. domUtils.remove(ci)
  15481. }
  15482. }
  15483. }
  15484. if (browser.gecko) {
  15485. var dirtyNodes = div.querySelectorAll('[_moz_dirty]')
  15486. for (i = 0; (ci = dirtyNodes[i++]); ) {
  15487. ci.removeAttribute('_moz_dirty')
  15488. }
  15489. }
  15490. if (!browser.ie) {
  15491. var spans = div.querySelectorAll('span.Apple-style-span')
  15492. for (var i = 0, ci; (ci = spans[i++]); ) {
  15493. domUtils.remove(ci, true)
  15494. }
  15495. }
  15496. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  15497. html = div.innerHTML //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  15498. //过滤word粘贴过来的冗余属性
  15499. html = UE.filterWord(html)
  15500. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  15501. var root = UE.htmlparser(html)
  15502. //如果给了过滤规则就先进行过滤
  15503. if (me.options.filterRules) {
  15504. UE.filterNode(root, me.options.filterRules)
  15505. }
  15506. //执行默认的处理
  15507. me.filterInputRule(root)
  15508. //针对chrome的处理
  15509. if (browser.webkit) {
  15510. var br = root.lastChild()
  15511. if (br && br.type == 'element' && br.tagName == 'br') {
  15512. root.removeChild(br)
  15513. }
  15514. utils.each(me.body.querySelectorAll('div'), function(node) {
  15515. if (domUtils.isEmptyBlock(node)) {
  15516. domUtils.remove(node, true)
  15517. }
  15518. })
  15519. }
  15520. html = {html: root.toHtml()}
  15521. me.fireEvent('beforepaste', html, root)
  15522. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  15523. if (!html.html) {
  15524. return
  15525. }
  15526. root = UE.htmlparser(html.html, true)
  15527. //如果开启了纯文本模式
  15528. if (me.queryCommandState('pasteplain') === 1) {
  15529. me.execCommand(
  15530. 'insertHtml',
  15531. UE.filterNode(root, me.options.filterTxtRules).toHtml(),
  15532. true
  15533. )
  15534. } else {
  15535. //文本模式
  15536. UE.filterNode(root, me.options.filterTxtRules)
  15537. txtContent = root.toHtml()
  15538. //完全模式
  15539. htmlContent = html.html
  15540. address = me.selection.getRange().createAddress(true)
  15541. me.execCommand(
  15542. 'insertHtml',
  15543. me.getOpt('retainOnlyLabelPasted') === true
  15544. ? getPureHtml(htmlContent)
  15545. : htmlContent,
  15546. true
  15547. )
  15548. }
  15549. me.fireEvent('afterpaste', html)
  15550. }
  15551. }
  15552. me.addListener('pasteTransfer', function(cmd, plainType) {
  15553. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  15554. var range = me.selection.getRange()
  15555. range.moveToAddress(address, true)
  15556. if (!range.collapsed) {
  15557. while (!domUtils.isBody(range.startContainer)) {
  15558. var start = range.startContainer
  15559. if (start.nodeType == 1) {
  15560. start = start.childNodes[range.startOffset]
  15561. if (!start) {
  15562. range.setStartBefore(range.startContainer)
  15563. continue
  15564. }
  15565. var pre = start.previousSibling
  15566. if (
  15567. pre &&
  15568. pre.nodeType == 3 &&
  15569. new RegExp('^[\n\r\t ' + domUtils.fillChar + ']*$').test(
  15570. pre.nodeValue
  15571. )
  15572. ) {
  15573. range.setStartBefore(pre)
  15574. }
  15575. }
  15576. if (range.startOffset == 0) {
  15577. range.setStartBefore(range.startContainer)
  15578. } else {
  15579. break
  15580. }
  15581. }
  15582. while (!domUtils.isBody(range.endContainer)) {
  15583. var end = range.endContainer
  15584. if (end.nodeType == 1) {
  15585. end = end.childNodes[range.endOffset]
  15586. if (!end) {
  15587. range.setEndAfter(range.endContainer)
  15588. continue
  15589. }
  15590. var next = end.nextSibling
  15591. if (
  15592. next &&
  15593. next.nodeType == 3 &&
  15594. new RegExp('^[\n\r\t' + domUtils.fillChar + ']*$').test(
  15595. next.nodeValue
  15596. )
  15597. ) {
  15598. range.setEndAfter(next)
  15599. }
  15600. }
  15601. if (
  15602. range.endOffset ==
  15603. range.endContainer[
  15604. range.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  15605. ].length
  15606. ) {
  15607. range.setEndAfter(range.endContainer)
  15608. } else {
  15609. break
  15610. }
  15611. }
  15612. }
  15613. range.deleteContents()
  15614. range.select(true)
  15615. me.__hasEnterExecCommand = true
  15616. var html = htmlContent
  15617. if (plainType === 2) {
  15618. html = getPureHtml(html)
  15619. } else if (plainType) {
  15620. html = txtContent
  15621. }
  15622. me.execCommand('inserthtml', html, true)
  15623. me.__hasEnterExecCommand = false
  15624. var rng = me.selection.getRange()
  15625. while (
  15626. !domUtils.isBody(rng.startContainer) &&
  15627. !rng.startOffset &&
  15628. rng.startContainer[
  15629. rng.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  15630. ].length
  15631. ) {
  15632. rng.setStartBefore(rng.startContainer)
  15633. }
  15634. var tmpAddress = rng.createAddress(true)
  15635. address.endAddress = tmpAddress.startAddress
  15636. }
  15637. })
  15638. me.addListener('ready', function() {
  15639. domUtils.on(me.body, 'cut', function() {
  15640. var range = me.selection.getRange()
  15641. if (!range.collapsed && me.undoManger) {
  15642. me.undoManger.save()
  15643. }
  15644. })
  15645. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  15646. domUtils.on(
  15647. me.body,
  15648. browser.ie || browser.opera ? 'keydown' : 'paste',
  15649. function(e) {
  15650. if (
  15651. (browser.ie || browser.opera) &&
  15652. ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')
  15653. ) {
  15654. return
  15655. }
  15656. getClipboardData.call(me, function(div) {
  15657. filter(div)
  15658. })
  15659. }
  15660. )
  15661. })
  15662. me.commands['paste'] = {
  15663. execCommand: function(cmd) {
  15664. if (browser.ie) {
  15665. getClipboardData.call(me, function(div) {
  15666. filter(div)
  15667. })
  15668. me.document.execCommand('paste')
  15669. } else {
  15670. alert(me.getLang('pastemsg'))
  15671. }
  15672. }
  15673. }
  15674. }
  15675. // plugins/puretxtpaste.js
  15676. /**
  15677. * 纯文本粘贴插件
  15678. * @file
  15679. * @since 1.2.6.1
  15680. */
  15681. UE.plugins['pasteplain'] = function() {
  15682. var me = this
  15683. me.setOpt({
  15684. pasteplain: false,
  15685. filterTxtRules: (function() {
  15686. function transP(node) {
  15687. node.tagName = 'p'
  15688. node.setStyle()
  15689. }
  15690. function removeNode(node) {
  15691. node.parentNode.removeChild(node, true)
  15692. }
  15693. return {
  15694. //直接删除及其字节点内容
  15695. '-': 'script style object iframe embed input select',
  15696. p: {$: {}},
  15697. br: {$: {}},
  15698. div: function(node) {
  15699. var tmpNode,
  15700. p = UE.uNode.createElement('p')
  15701. while ((tmpNode = node.firstChild())) {
  15702. if (
  15703. tmpNode.type == 'text' ||
  15704. !UE.dom.dtd.$block[tmpNode.tagName]
  15705. ) {
  15706. p.appendChild(tmpNode)
  15707. } else {
  15708. if (p.firstChild()) {
  15709. node.parentNode.insertBefore(p, node)
  15710. p = UE.uNode.createElement('p')
  15711. } else {
  15712. node.parentNode.insertBefore(tmpNode, node)
  15713. }
  15714. }
  15715. }
  15716. if (p.firstChild()) {
  15717. node.parentNode.insertBefore(p, node)
  15718. }
  15719. node.parentNode.removeChild(node)
  15720. },
  15721. ol: removeNode,
  15722. ul: removeNode,
  15723. dl: removeNode,
  15724. dt: removeNode,
  15725. dd: removeNode,
  15726. li: removeNode,
  15727. caption: transP,
  15728. th: transP,
  15729. tr: transP,
  15730. h1: transP,
  15731. h2: transP,
  15732. h3: transP,
  15733. h4: transP,
  15734. h5: transP,
  15735. h6: transP,
  15736. td: function(node) {
  15737. //没有内容的td直接删掉
  15738. var txt = !!node.innerText()
  15739. if (txt) {
  15740. node.parentNode.insertAfter(
  15741. UE.uNode.createText(' &nbsp; &nbsp;'),
  15742. node
  15743. )
  15744. }
  15745. node.parentNode.removeChild(node, node.innerText())
  15746. }
  15747. }
  15748. })()
  15749. })
  15750. //暂时这里支持一下老版本的属性
  15751. var pasteplain = me.options.pasteplain
  15752. /**
  15753. * 启用或取消纯文本粘贴模式
  15754. * @command pasteplain
  15755. * @method execCommand
  15756. * @param { String } cmd 命令字符串
  15757. * @example
  15758. * ```javascript
  15759. * editor.queryCommandState( 'pasteplain' );
  15760. * ```
  15761. */
  15762. /**
  15763. * 查询当前是否处于纯文本粘贴模式
  15764. * @command pasteplain
  15765. * @method queryCommandState
  15766. * @param { String } cmd 命令字符串
  15767. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  15768. * @example
  15769. * ```javascript
  15770. * editor.queryCommandState( 'pasteplain' );
  15771. * ```
  15772. */
  15773. me.commands['pasteplain'] = {
  15774. queryCommandState: function() {
  15775. return pasteplain ? 1 : 0
  15776. },
  15777. execCommand: function() {
  15778. pasteplain = !pasteplain | 0
  15779. },
  15780. notNeedUndo: 1
  15781. }
  15782. }
  15783. // plugins/list.js
  15784. /**
  15785. * 有序列表,无序列表插件
  15786. * @file
  15787. * @since 1.2.6.1
  15788. */
  15789. UE.plugins['list'] = function() {
  15790. var me = this,
  15791. notExchange = {
  15792. TD: 1,
  15793. PRE: 1,
  15794. BLOCKQUOTE: 1
  15795. }
  15796. var customStyle = {
  15797. cn: 'cn-1-',
  15798. cn1: 'cn-2-',
  15799. cn2: 'cn-3-',
  15800. num: 'num-1-',
  15801. num1: 'num-2-',
  15802. num2: 'num-3-',
  15803. dash: 'dash',
  15804. dot: 'dot'
  15805. }
  15806. me.setOpt({
  15807. autoTransWordToList: false,
  15808. insertorderedlist: {
  15809. num: '',
  15810. num1: '',
  15811. num2: '',
  15812. cn: '',
  15813. cn1: '',
  15814. cn2: '',
  15815. decimal: '',
  15816. 'lower-alpha': '',
  15817. 'lower-roman': '',
  15818. 'upper-alpha': '',
  15819. 'upper-roman': ''
  15820. },
  15821. insertunorderedlist: {
  15822. circle: '',
  15823. disc: '',
  15824. square: '',
  15825. dash: '',
  15826. dot: ''
  15827. },
  15828. listDefaultPaddingLeft: '30',
  15829. listiconpath: 'http://bs.baidu.com/listicon/',
  15830. maxListLevel: -1, //-1不限制
  15831. disablePInList: false
  15832. })
  15833. function listToArray(list) {
  15834. var arr = []
  15835. for (var p in list) {
  15836. arr.push(p)
  15837. }
  15838. return arr
  15839. }
  15840. var listStyle = {
  15841. OL: listToArray(me.options.insertorderedlist),
  15842. UL: listToArray(me.options.insertunorderedlist)
  15843. }
  15844. var liiconpath = me.options.listiconpath
  15845. //根据用户配置,调整customStyle
  15846. for (var s in customStyle) {
  15847. if (
  15848. !me.options.insertorderedlist.hasOwnProperty(s) &&
  15849. !me.options.insertunorderedlist.hasOwnProperty(s)
  15850. ) {
  15851. delete customStyle[s]
  15852. }
  15853. }
  15854. me.ready(function() {
  15855. var customCss = []
  15856. for (var p in customStyle) {
  15857. if (p == 'dash' || p == 'dot') {
  15858. customCss.push(
  15859. 'li.list-' +
  15860. customStyle[p] +
  15861. '{background-image:url(' +
  15862. liiconpath +
  15863. customStyle[p] +
  15864. '.gif)}'
  15865. )
  15866. customCss.push(
  15867. 'ul.custom_' +
  15868. p +
  15869. '{list-style:none;}ul.custom_' +
  15870. p +
  15871. ' li{background-position:0 3px;background-repeat:no-repeat}'
  15872. )
  15873. } else {
  15874. for (var i = 0; i < 99; i++) {
  15875. customCss.push(
  15876. 'li.list-' +
  15877. customStyle[p] +
  15878. i +
  15879. '{background-image:url(' +
  15880. liiconpath +
  15881. 'list-' +
  15882. customStyle[p] +
  15883. i +
  15884. '.gif)}'
  15885. )
  15886. }
  15887. customCss.push(
  15888. 'ol.custom_' +
  15889. p +
  15890. '{list-style:none;}ol.custom_' +
  15891. p +
  15892. ' li{background-position:0 3px;background-repeat:no-repeat}'
  15893. )
  15894. }
  15895. switch (p) {
  15896. case 'cn':
  15897. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}')
  15898. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}')
  15899. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}')
  15900. break
  15901. case 'cn1':
  15902. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:30px}')
  15903. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}')
  15904. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}')
  15905. break
  15906. case 'cn2':
  15907. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:40px}')
  15908. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:55px}')
  15909. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:68px}')
  15910. break
  15911. case 'num':
  15912. case 'num1':
  15913. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}')
  15914. break
  15915. case 'num2':
  15916. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:35px}')
  15917. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}')
  15918. break
  15919. case 'dash':
  15920. customCss.push('li.list-' + p + '-paddingleft{padding-left:35px}')
  15921. break
  15922. case 'dot':
  15923. customCss.push('li.list-' + p + '-paddingleft{padding-left:20px}')
  15924. }
  15925. }
  15926. customCss.push('.list-paddingleft-1{padding-left:0}')
  15927. customCss.push(
  15928. '.list-paddingleft-2{padding-left:' +
  15929. me.options.listDefaultPaddingLeft +
  15930. 'px}'
  15931. )
  15932. customCss.push(
  15933. '.list-paddingleft-3{padding-left:' +
  15934. me.options.listDefaultPaddingLeft * 2 +
  15935. 'px}'
  15936. )
  15937. //如果不给宽度会在自定应样式里出现滚动条
  15938. utils.cssRule(
  15939. 'list',
  15940. 'ol,ul{margin:0;pading:0;' +
  15941. (browser.ie ? '' : 'width:95%') +
  15942. '}li{clear:both;}' +
  15943. customCss.join('\n'),
  15944. me.document
  15945. )
  15946. })
  15947. //单独处理剪切的问题
  15948. me.ready(function() {
  15949. domUtils.on(me.body, 'cut', function() {
  15950. setTimeout(function() {
  15951. var rng = me.selection.getRange(),
  15952. li
  15953. //trace:3416
  15954. if (!rng.collapsed) {
  15955. if (
  15956. (li = domUtils.findParentByTagName(
  15957. rng.startContainer,
  15958. 'li',
  15959. true
  15960. ))
  15961. ) {
  15962. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  15963. var pn = li.parentNode,
  15964. node
  15965. if ((node = pn.previousSibling)) {
  15966. domUtils.remove(pn)
  15967. rng.setStartAtLast(node).collapse(true)
  15968. rng.select(true)
  15969. } else if ((node = pn.nextSibling)) {
  15970. domUtils.remove(pn)
  15971. rng.setStartAtFirst(node).collapse(true)
  15972. rng.select(true)
  15973. } else {
  15974. var tmpNode = me.document.createElement('p')
  15975. domUtils.fillNode(me.document, tmpNode)
  15976. pn.parentNode.insertBefore(tmpNode, pn)
  15977. domUtils.remove(pn)
  15978. rng.setStart(tmpNode, 0).collapse(true)
  15979. rng.select(true)
  15980. }
  15981. }
  15982. }
  15983. }
  15984. })
  15985. })
  15986. })
  15987. function getStyle(node) {
  15988. var cls = node.className
  15989. if (domUtils.hasClass(node, /custom_/)) {
  15990. return cls.match(/custom_(\w+)/)[1]
  15991. }
  15992. return domUtils.getStyle(node, 'list-style-type')
  15993. }
  15994. me.addListener('beforepaste', function(type, html) {
  15995. var me = this,
  15996. rng = me.selection.getRange(),
  15997. li
  15998. var root = UE.htmlparser(html.html, true)
  15999. if ((li = domUtils.findParentByTagName(rng.startContainer, 'li', true))) {
  16000. var list = li.parentNode,
  16001. tagName = list.tagName == 'OL' ? 'ul' : 'ol'
  16002. utils.each(root.getNodesByTagName(tagName), function(n) {
  16003. n.tagName = list.tagName
  16004. n.setAttr()
  16005. if (n.parentNode === root) {
  16006. type = getStyle(list) || (list.tagName == 'OL' ? 'decimal' : 'disc')
  16007. } else {
  16008. var className = n.parentNode.getAttr('class')
  16009. if (className && /custom_/.test(className)) {
  16010. type = className.match(/custom_(\w+)/)[1]
  16011. } else {
  16012. type = n.parentNode.getStyle('list-style-type')
  16013. }
  16014. if (!type) {
  16015. type = list.tagName == 'OL' ? 'decimal' : 'disc'
  16016. }
  16017. }
  16018. var index = utils.indexOf(listStyle[list.tagName], type)
  16019. if (n.parentNode !== root)
  16020. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1
  16021. var currentStyle = listStyle[list.tagName][index]
  16022. if (customStyle[currentStyle]) {
  16023. n.setAttr('class', 'custom_' + currentStyle)
  16024. } else {
  16025. n.setStyle('list-style-type', currentStyle)
  16026. }
  16027. })
  16028. }
  16029. html.html = root.toHtml()
  16030. })
  16031. //导出时,去掉p标签
  16032. me.getOpt('disablePInList') === true &&
  16033. me.addOutputRule(function(root) {
  16034. utils.each(root.getNodesByTagName('li'), function(li) {
  16035. var newChildrens = [],
  16036. index = 0
  16037. utils.each(li.children, function(n) {
  16038. if (n.tagName == 'p') {
  16039. var tmpNode
  16040. while ((tmpNode = n.children.pop())) {
  16041. newChildrens.splice(index, 0, tmpNode)
  16042. tmpNode.parentNode = li
  16043. lastNode = tmpNode
  16044. }
  16045. tmpNode = newChildrens[newChildrens.length - 1]
  16046. if (
  16047. !tmpNode ||
  16048. tmpNode.type != 'element' ||
  16049. tmpNode.tagName != 'br'
  16050. ) {
  16051. var br = UE.uNode.createElement('br')
  16052. br.parentNode = li
  16053. newChildrens.push(br)
  16054. }
  16055. index = newChildrens.length
  16056. }
  16057. })
  16058. if (newChildrens.length) {
  16059. li.children = newChildrens
  16060. }
  16061. })
  16062. })
  16063. //进入编辑器的li要套p标签
  16064. me.addInputRule(function(root) {
  16065. utils.each(root.getNodesByTagName('li'), function(li) {
  16066. var tmpP = UE.uNode.createElement('p')
  16067. for (var i = 0, ci; (ci = li.children[i]); ) {
  16068. if (ci.type == 'text' || dtd.p[ci.tagName]) {
  16069. tmpP.appendChild(ci)
  16070. } else {
  16071. if (tmpP.firstChild()) {
  16072. li.insertBefore(tmpP, ci)
  16073. tmpP = UE.uNode.createElement('p')
  16074. i = i + 2
  16075. } else {
  16076. i++
  16077. }
  16078. }
  16079. }
  16080. if ((tmpP.firstChild() && !tmpP.parentNode) || !li.firstChild()) {
  16081. li.appendChild(tmpP)
  16082. }
  16083. //trace:3357
  16084. //p不能为空
  16085. if (!tmpP.firstChild()) {
  16086. tmpP.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  16087. }
  16088. //去掉末尾的空白
  16089. var p = li.firstChild()
  16090. var lastChild = p.lastChild()
  16091. if (
  16092. lastChild &&
  16093. lastChild.type == 'text' &&
  16094. /^\s*$/.test(lastChild.data)
  16095. ) {
  16096. p.removeChild(lastChild)
  16097. }
  16098. })
  16099. if (me.options.autoTransWordToList) {
  16100. var orderlisttype = {
  16101. num1: /^\d+\)/,
  16102. decimal: /^\d+\./,
  16103. 'lower-alpha': /^[a-z]+\)/,
  16104. 'upper-alpha': /^[A-Z]+\./,
  16105. cn: /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  16106. cn2: /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  16107. },
  16108. unorderlisttype = {
  16109. square: 'n'
  16110. }
  16111. function checkListType(content, container) {
  16112. var span = container.firstChild()
  16113. if (
  16114. span &&
  16115. span.type == 'element' &&
  16116. span.tagName == 'span' &&
  16117. /Wingdings|Symbol/.test(span.getStyle('font-family'))
  16118. ) {
  16119. for (var p in unorderlisttype) {
  16120. if (unorderlisttype[p] == span.data) {
  16121. return p
  16122. }
  16123. }
  16124. return 'disc'
  16125. }
  16126. for (var p in orderlisttype) {
  16127. if (orderlisttype[p].test(content)) {
  16128. return p
  16129. }
  16130. }
  16131. }
  16132. utils.each(root.getNodesByTagName('p'), function(node) {
  16133. if (node.getAttr('class') != 'MsoListParagraph') {
  16134. return
  16135. }
  16136. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  16137. node.setStyle('margin', '')
  16138. node.setStyle('margin-left', '')
  16139. node.setAttr('class', '')
  16140. function appendLi(list, p, type) {
  16141. if (list.tagName == 'ol') {
  16142. if (browser.ie) {
  16143. var first = p.firstChild()
  16144. if (
  16145. first.type == 'element' &&
  16146. first.tagName == 'span' &&
  16147. orderlisttype[type].test(first.innerText())
  16148. ) {
  16149. p.removeChild(first)
  16150. }
  16151. } else {
  16152. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ''))
  16153. }
  16154. } else {
  16155. p.removeChild(p.firstChild())
  16156. }
  16157. var li = UE.uNode.createElement('li')
  16158. li.appendChild(p)
  16159. list.appendChild(li)
  16160. }
  16161. var tmp = node,
  16162. type,
  16163. cacheNode = node
  16164. if (
  16165. node.parentNode.tagName != 'li' &&
  16166. (type = checkListType(node.innerText(), node))
  16167. ) {
  16168. var list = UE.uNode.createElement(
  16169. me.options.insertorderedlist.hasOwnProperty(type) ? 'ol' : 'ul'
  16170. )
  16171. if (customStyle[type]) {
  16172. list.setAttr('class', 'custom_' + type)
  16173. } else {
  16174. list.setStyle('list-style-type', type)
  16175. }
  16176. while (
  16177. node &&
  16178. node.parentNode.tagName != 'li' &&
  16179. checkListType(node.innerText(), node)
  16180. ) {
  16181. tmp = node.nextSibling()
  16182. if (!tmp) {
  16183. node.parentNode.insertBefore(list, node)
  16184. }
  16185. appendLi(list, node, type)
  16186. node = tmp
  16187. }
  16188. if (!list.parentNode && node && node.parentNode) {
  16189. node.parentNode.insertBefore(list, node)
  16190. }
  16191. }
  16192. var span = cacheNode.firstChild()
  16193. if (
  16194. span &&
  16195. span.type == 'element' &&
  16196. span.tagName == 'span' &&
  16197. /^\s*(&nbsp;)+\s*$/.test(span.innerText())
  16198. ) {
  16199. span.parentNode.removeChild(span)
  16200. }
  16201. })
  16202. }
  16203. })
  16204. //调整索引标签
  16205. me.addListener('contentchange', function() {
  16206. adjustListStyle(me.document)
  16207. })
  16208. function adjustListStyle(doc, ignore) {
  16209. utils.each(domUtils.getElementsByTagName(doc, 'ol ul'), function(node) {
  16210. if (!domUtils.inDoc(node, doc)) return
  16211. var parent = node.parentNode
  16212. if (parent.tagName == node.tagName) {
  16213. var nodeStyleType =
  16214. getStyle(node) || (node.tagName == 'OL' ? 'decimal' : 'disc'),
  16215. parentStyleType =
  16216. getStyle(parent) || (parent.tagName == 'OL' ? 'decimal' : 'disc')
  16217. if (nodeStyleType == parentStyleType) {
  16218. var styleIndex = utils.indexOf(
  16219. listStyle[node.tagName],
  16220. nodeStyleType
  16221. )
  16222. styleIndex =
  16223. styleIndex + 1 == listStyle[node.tagName].length
  16224. ? 0
  16225. : styleIndex + 1
  16226. setListStyle(node, listStyle[node.tagName][styleIndex])
  16227. }
  16228. }
  16229. var index = 0,
  16230. type = 2
  16231. if (domUtils.hasClass(node, /custom_/)) {
  16232. if (
  16233. !(
  16234. /[ou]l/i.test(parent.tagName) &&
  16235. domUtils.hasClass(parent, /custom_/)
  16236. )
  16237. ) {
  16238. type = 1
  16239. }
  16240. } else {
  16241. if (
  16242. /[ou]l/i.test(parent.tagName) &&
  16243. domUtils.hasClass(parent, /custom_/)
  16244. ) {
  16245. type = 3
  16246. }
  16247. }
  16248. var style = domUtils.getStyle(node, 'list-style-type')
  16249. style && (node.style.cssText = 'list-style-type:' + style)
  16250. node.className =
  16251. utils.trim(node.className.replace(/list-paddingleft-\w+/, '')) +
  16252. ' list-paddingleft-' +
  16253. type
  16254. utils.each(domUtils.getElementsByTagName(node, 'li'), function(li) {
  16255. li.style.cssText && (li.style.cssText = '')
  16256. if (!li.firstChild) {
  16257. domUtils.remove(li)
  16258. return
  16259. }
  16260. if (li.parentNode !== node) {
  16261. return
  16262. }
  16263. index++
  16264. if (domUtils.hasClass(node, /custom_/)) {
  16265. var paddingLeft = 1,
  16266. currentStyle = getStyle(node)
  16267. if (node.tagName == 'OL') {
  16268. if (currentStyle) {
  16269. switch (currentStyle) {
  16270. case 'cn':
  16271. case 'cn1':
  16272. case 'cn2':
  16273. if (
  16274. index > 10 &&
  16275. (index % 10 == 0 || (index > 10 && index < 20))
  16276. ) {
  16277. paddingLeft = 2
  16278. } else if (index > 20) {
  16279. paddingLeft = 3
  16280. }
  16281. break
  16282. case 'num2':
  16283. if (index > 9) {
  16284. paddingLeft = 2
  16285. }
  16286. }
  16287. }
  16288. li.className =
  16289. 'list-' +
  16290. customStyle[currentStyle] +
  16291. index +
  16292. ' ' +
  16293. 'list-' +
  16294. currentStyle +
  16295. '-paddingleft-' +
  16296. paddingLeft
  16297. } else {
  16298. li.className =
  16299. 'list-' +
  16300. customStyle[currentStyle] +
  16301. ' ' +
  16302. 'list-' +
  16303. currentStyle +
  16304. '-paddingleft'
  16305. }
  16306. } else {
  16307. li.className = li.className.replace(/list-[\w\-]+/gi, '')
  16308. }
  16309. var className = li.getAttribute('class')
  16310. if (className !== null && !className.replace(/\s/g, '')) {
  16311. domUtils.removeAttributes(li, 'class')
  16312. }
  16313. })
  16314. !ignore &&
  16315. adjustList(
  16316. node,
  16317. node.tagName.toLowerCase(),
  16318. getStyle(node) || domUtils.getStyle(node, 'list-style-type'),
  16319. true
  16320. )
  16321. })
  16322. }
  16323. function adjustList(list, tag, style, ignoreEmpty) {
  16324. var nextList = list.nextSibling
  16325. if (
  16326. nextList &&
  16327. nextList.nodeType == 1 &&
  16328. nextList.tagName.toLowerCase() == tag &&
  16329. (getStyle(nextList) ||
  16330. domUtils.getStyle(nextList, 'list-style-type') ||
  16331. (tag == 'ol' ? 'decimal' : 'disc')) == style
  16332. ) {
  16333. domUtils.moveChild(nextList, list)
  16334. if (nextList.childNodes.length == 0) {
  16335. domUtils.remove(nextList)
  16336. }
  16337. }
  16338. if (nextList && domUtils.isFillChar(nextList)) {
  16339. domUtils.remove(nextList)
  16340. }
  16341. var preList = list.previousSibling
  16342. if (
  16343. preList &&
  16344. preList.nodeType == 1 &&
  16345. preList.tagName.toLowerCase() == tag &&
  16346. (getStyle(preList) ||
  16347. domUtils.getStyle(preList, 'list-style-type') ||
  16348. (tag == 'ol' ? 'decimal' : 'disc')) == style
  16349. ) {
  16350. domUtils.moveChild(list, preList)
  16351. }
  16352. if (preList && domUtils.isFillChar(preList)) {
  16353. domUtils.remove(preList)
  16354. }
  16355. !ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list)
  16356. if (getStyle(list)) {
  16357. adjustListStyle(list.ownerDocument, true)
  16358. }
  16359. }
  16360. function setListStyle(list, style) {
  16361. if (customStyle[style]) {
  16362. list.className = 'custom_' + style
  16363. }
  16364. try {
  16365. domUtils.setStyle(list, 'list-style-type', style)
  16366. } catch (e) {}
  16367. }
  16368. function clearEmptySibling(node) {
  16369. var tmpNode = node.previousSibling
  16370. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16371. domUtils.remove(tmpNode)
  16372. }
  16373. tmpNode = node.nextSibling
  16374. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16375. domUtils.remove(tmpNode)
  16376. }
  16377. }
  16378. me.addListener('keydown', function(type, evt) {
  16379. function preventAndSave() {
  16380. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  16381. me.fireEvent('contentchange')
  16382. me.undoManger && me.undoManger.save()
  16383. }
  16384. function findList(node, filterFn) {
  16385. while (node && !domUtils.isBody(node)) {
  16386. if (filterFn(node)) {
  16387. return null
  16388. }
  16389. if (node.nodeType == 1 && /[ou]l/i.test(node.tagName)) {
  16390. return node
  16391. }
  16392. node = node.parentNode
  16393. }
  16394. return null
  16395. }
  16396. var keyCode = evt.keyCode || evt.which
  16397. if (keyCode == 13 && !evt.shiftKey) {
  16398. //回车
  16399. var rng = me.selection.getRange(),
  16400. parent = domUtils.findParent(
  16401. rng.startContainer,
  16402. function(node) {
  16403. return domUtils.isBlockElm(node)
  16404. },
  16405. true
  16406. ),
  16407. li = domUtils.findParentByTagName(rng.startContainer, 'li', true)
  16408. if (parent && parent.tagName != 'PRE' && !li) {
  16409. var html = parent.innerHTML.replace(
  16410. new RegExp(domUtils.fillChar, 'g'),
  16411. ''
  16412. )
  16413. if (/^\s*1\s*\.[^\d]/.test(html)) {
  16414. parent.innerHTML = html.replace(/^\s*1\s*\./, '')
  16415. rng
  16416. .setStartAtLast(parent)
  16417. .collapse(true)
  16418. .select()
  16419. me.__hasEnterExecCommand = true
  16420. me.execCommand('insertorderedlist')
  16421. me.__hasEnterExecCommand = false
  16422. }
  16423. }
  16424. var range = me.selection.getRange(),
  16425. start = findList(range.startContainer, function(node) {
  16426. return node.tagName == 'TABLE'
  16427. }),
  16428. end = range.collapsed
  16429. ? start
  16430. : findList(range.endContainer, function(node) {
  16431. return node.tagName == 'TABLE'
  16432. })
  16433. if (start && end && start === end) {
  16434. if (!range.collapsed) {
  16435. start = domUtils.findParentByTagName(
  16436. range.startContainer,
  16437. 'li',
  16438. true
  16439. )
  16440. end = domUtils.findParentByTagName(range.endContainer, 'li', true)
  16441. if (start && end && start === end) {
  16442. range.deleteContents()
  16443. li = domUtils.findParentByTagName(
  16444. range.startContainer,
  16445. 'li',
  16446. true
  16447. )
  16448. if (li && domUtils.isEmptyBlock(li)) {
  16449. pre = li.previousSibling
  16450. next = li.nextSibling
  16451. p = me.document.createElement('p')
  16452. domUtils.fillNode(me.document, p)
  16453. parentList = li.parentNode
  16454. if (pre && next) {
  16455. range
  16456. .setStart(next, 0)
  16457. .collapse(true)
  16458. .select(true)
  16459. domUtils.remove(li)
  16460. } else {
  16461. if ((!pre && !next) || !pre) {
  16462. parentList.parentNode.insertBefore(p, parentList)
  16463. } else {
  16464. li.parentNode.parentNode.insertBefore(
  16465. p,
  16466. parentList.nextSibling
  16467. )
  16468. }
  16469. domUtils.remove(li)
  16470. if (!parentList.firstChild) {
  16471. domUtils.remove(parentList)
  16472. }
  16473. range.setStart(p, 0).setCursor()
  16474. }
  16475. preventAndSave()
  16476. return
  16477. }
  16478. } else {
  16479. var tmpRange = range.cloneRange(),
  16480. bk = tmpRange.collapse(false).createBookmark()
  16481. range.deleteContents()
  16482. tmpRange.moveToBookmark(bk)
  16483. var li = domUtils.findParentByTagName(
  16484. tmpRange.startContainer,
  16485. 'li',
  16486. true
  16487. )
  16488. clearEmptySibling(li)
  16489. tmpRange.select()
  16490. preventAndSave()
  16491. return
  16492. }
  16493. }
  16494. li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  16495. if (li) {
  16496. if (domUtils.isEmptyBlock(li)) {
  16497. bk = range.createBookmark()
  16498. var parentList = li.parentNode
  16499. if (li !== parentList.lastChild) {
  16500. domUtils.breakParent(li, parentList)
  16501. clearEmptySibling(li)
  16502. } else {
  16503. parentList.parentNode.insertBefore(li, parentList.nextSibling)
  16504. if (domUtils.isEmptyNode(parentList)) {
  16505. domUtils.remove(parentList)
  16506. }
  16507. }
  16508. //嵌套不处理
  16509. if (!dtd.$list[li.parentNode.tagName]) {
  16510. if (!domUtils.isBlockElm(li.firstChild)) {
  16511. p = me.document.createElement('p')
  16512. li.parentNode.insertBefore(p, li)
  16513. while (li.firstChild) {
  16514. p.appendChild(li.firstChild)
  16515. }
  16516. domUtils.remove(li)
  16517. } else {
  16518. domUtils.remove(li, true)
  16519. }
  16520. }
  16521. range.moveToBookmark(bk).select()
  16522. } else {
  16523. var first = li.firstChild
  16524. if (!first || !domUtils.isBlockElm(first)) {
  16525. var p = me.document.createElement('p')
  16526. !li.firstChild && domUtils.fillNode(me.document, p)
  16527. while (li.firstChild) {
  16528. p.appendChild(li.firstChild)
  16529. }
  16530. li.appendChild(p)
  16531. first = p
  16532. }
  16533. var span = me.document.createElement('span')
  16534. range.insertNode(span)
  16535. domUtils.breakParent(span, li)
  16536. var nextLi = span.nextSibling
  16537. first = nextLi.firstChild
  16538. if (!first) {
  16539. p = me.document.createElement('p')
  16540. domUtils.fillNode(me.document, p)
  16541. nextLi.appendChild(p)
  16542. first = p
  16543. }
  16544. if (domUtils.isEmptyNode(first)) {
  16545. first.innerHTML = ''
  16546. domUtils.fillNode(me.document, first)
  16547. }
  16548. range
  16549. .setStart(first, 0)
  16550. .collapse(true)
  16551. .shrinkBoundary()
  16552. .select()
  16553. domUtils.remove(span)
  16554. var pre = nextLi.previousSibling
  16555. if (pre && domUtils.isEmptyBlock(pre)) {
  16556. pre.innerHTML = '<p></p>'
  16557. domUtils.fillNode(me.document, pre.firstChild)
  16558. }
  16559. }
  16560. // }
  16561. preventAndSave()
  16562. }
  16563. }
  16564. }
  16565. if (keyCode == 8) {
  16566. //修中ie中li下的问题
  16567. range = me.selection.getRange()
  16568. if (range.collapsed && domUtils.isStartInblock(range)) {
  16569. tmpRange = range.cloneRange().trimBoundary()
  16570. li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  16571. //要在li的最左边,才能处理
  16572. if (li && domUtils.isStartInblock(tmpRange)) {
  16573. start = domUtils.findParentByTagName(
  16574. range.startContainer,
  16575. 'p',
  16576. true
  16577. )
  16578. if (start && start !== li.firstChild) {
  16579. var parentList = domUtils.findParentByTagName(start, ['ol', 'ul'])
  16580. domUtils.breakParent(start, parentList)
  16581. clearEmptySibling(start)
  16582. me.fireEvent('contentchange')
  16583. range.setStart(start, 0).setCursor(false, true)
  16584. me.fireEvent('saveScene')
  16585. domUtils.preventDefault(evt)
  16586. return
  16587. }
  16588. if (li && (pre = li.previousSibling)) {
  16589. if (keyCode == 46 && li.childNodes.length) {
  16590. return
  16591. }
  16592. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  16593. if (dtd.$list[pre.tagName]) {
  16594. pre = pre.lastChild
  16595. }
  16596. me.undoManger && me.undoManger.save()
  16597. first = li.firstChild
  16598. if (domUtils.isBlockElm(first)) {
  16599. if (domUtils.isEmptyNode(first)) {
  16600. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16601. pre.appendChild(first)
  16602. range.setStart(first, 0).setCursor(false, true)
  16603. //first不是唯一的节点
  16604. while (li.firstChild) {
  16605. pre.appendChild(li.firstChild)
  16606. }
  16607. } else {
  16608. span = me.document.createElement('span')
  16609. range.insertNode(span)
  16610. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  16611. if (domUtils.isEmptyBlock(pre)) {
  16612. pre.innerHTML = ''
  16613. }
  16614. domUtils.moveChild(li, pre)
  16615. range
  16616. .setStartBefore(span)
  16617. .collapse(true)
  16618. .select(true)
  16619. domUtils.remove(span)
  16620. }
  16621. } else {
  16622. if (domUtils.isEmptyNode(li)) {
  16623. var p = me.document.createElement('p')
  16624. pre.appendChild(p)
  16625. range.setStart(p, 0).setCursor()
  16626. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16627. } else {
  16628. range
  16629. .setEnd(pre, pre.childNodes.length)
  16630. .collapse()
  16631. .select(true)
  16632. while (li.firstChild) {
  16633. pre.appendChild(li.firstChild)
  16634. }
  16635. }
  16636. }
  16637. domUtils.remove(li)
  16638. me.fireEvent('contentchange')
  16639. me.fireEvent('saveScene')
  16640. domUtils.preventDefault(evt)
  16641. return
  16642. }
  16643. //trace:980
  16644. if (li && !li.previousSibling) {
  16645. var parentList = li.parentNode
  16646. var bk = range.createBookmark()
  16647. if (domUtils.isTagNode(parentList.parentNode, 'ol ul')) {
  16648. parentList.parentNode.insertBefore(li, parentList)
  16649. if (domUtils.isEmptyNode(parentList)) {
  16650. domUtils.remove(parentList)
  16651. }
  16652. } else {
  16653. while (li.firstChild) {
  16654. parentList.parentNode.insertBefore(li.firstChild, parentList)
  16655. }
  16656. domUtils.remove(li)
  16657. if (domUtils.isEmptyNode(parentList)) {
  16658. domUtils.remove(parentList)
  16659. }
  16660. }
  16661. range.moveToBookmark(bk).setCursor(false, true)
  16662. me.fireEvent('contentchange')
  16663. me.fireEvent('saveScene')
  16664. domUtils.preventDefault(evt)
  16665. return
  16666. }
  16667. }
  16668. }
  16669. }
  16670. })
  16671. me.addListener('keyup', function(type, evt) {
  16672. var keyCode = evt.keyCode || evt.which
  16673. if (keyCode == 8) {
  16674. var rng = me.selection.getRange(),
  16675. list
  16676. if (
  16677. (list = domUtils.findParentByTagName(
  16678. rng.startContainer,
  16679. ['ol', 'ul'],
  16680. true
  16681. ))
  16682. ) {
  16683. adjustList(
  16684. list,
  16685. list.tagName.toLowerCase(),
  16686. getStyle(list) ||
  16687. domUtils.getComputedStyle(list, 'list-style-type'),
  16688. true
  16689. )
  16690. }
  16691. }
  16692. })
  16693. //处理tab键
  16694. me.addListener('tabkeydown', function() {
  16695. var range = me.selection.getRange()
  16696. //控制级数
  16697. function checkLevel(li) {
  16698. if (me.options.maxListLevel != -1) {
  16699. var level = li.parentNode,
  16700. levelNum = 0
  16701. while (/[ou]l/i.test(level.tagName)) {
  16702. levelNum++
  16703. level = level.parentNode
  16704. }
  16705. if (levelNum >= me.options.maxListLevel) {
  16706. return true
  16707. }
  16708. }
  16709. }
  16710. //只以开始为准
  16711. //todo 后续改进
  16712. var li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  16713. if (li) {
  16714. var bk
  16715. if (range.collapsed) {
  16716. if (checkLevel(li)) return true
  16717. var parentLi = li.parentNode,
  16718. list = me.document.createElement(parentLi.tagName),
  16719. index = utils.indexOf(
  16720. listStyle[list.tagName],
  16721. getStyle(parentLi) ||
  16722. domUtils.getComputedStyle(parentLi, 'list-style-type')
  16723. )
  16724. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1
  16725. var currentStyle = listStyle[list.tagName][index]
  16726. setListStyle(list, currentStyle)
  16727. if (domUtils.isStartInblock(range)) {
  16728. me.fireEvent('saveScene')
  16729. bk = range.createBookmark()
  16730. parentLi.insertBefore(list, li)
  16731. list.appendChild(li)
  16732. adjustList(list, list.tagName.toLowerCase(), currentStyle)
  16733. me.fireEvent('contentchange')
  16734. range.moveToBookmark(bk).select(true)
  16735. return true
  16736. }
  16737. } else {
  16738. me.fireEvent('saveScene')
  16739. bk = range.createBookmark()
  16740. for (
  16741. var i = 0, closeList, parents = domUtils.findParents(li), ci;
  16742. (ci = parents[i++]);
  16743. ) {
  16744. if (domUtils.isTagNode(ci, 'ol ul')) {
  16745. closeList = ci
  16746. break
  16747. }
  16748. }
  16749. var current = li
  16750. if (bk.end) {
  16751. while (
  16752. current &&
  16753. !(
  16754. domUtils.getPosition(current, bk.end) &
  16755. domUtils.POSITION_FOLLOWING
  16756. )
  16757. ) {
  16758. if (checkLevel(current)) {
  16759. current = domUtils.getNextDomNode(
  16760. current,
  16761. false,
  16762. null,
  16763. function(node) {
  16764. return node !== closeList
  16765. }
  16766. )
  16767. continue
  16768. }
  16769. var parentLi = current.parentNode,
  16770. list = me.document.createElement(parentLi.tagName),
  16771. index = utils.indexOf(
  16772. listStyle[list.tagName],
  16773. getStyle(parentLi) ||
  16774. domUtils.getComputedStyle(parentLi, 'list-style-type')
  16775. )
  16776. var currentIndex =
  16777. index + 1 == listStyle[list.tagName].length ? 0 : index + 1
  16778. var currentStyle = listStyle[list.tagName][currentIndex]
  16779. setListStyle(list, currentStyle)
  16780. parentLi.insertBefore(list, current)
  16781. while (
  16782. current &&
  16783. !(
  16784. domUtils.getPosition(current, bk.end) &
  16785. domUtils.POSITION_FOLLOWING
  16786. )
  16787. ) {
  16788. li = current.nextSibling
  16789. list.appendChild(current)
  16790. if (!li || domUtils.isTagNode(li, 'ol ul')) {
  16791. if (li) {
  16792. while ((li = li.firstChild)) {
  16793. if (li.tagName == 'LI') {
  16794. break
  16795. }
  16796. }
  16797. } else {
  16798. li = domUtils.getNextDomNode(current, false, null, function(
  16799. node
  16800. ) {
  16801. return node !== closeList
  16802. })
  16803. }
  16804. break
  16805. }
  16806. current = li
  16807. }
  16808. adjustList(list, list.tagName.toLowerCase(), currentStyle)
  16809. current = li
  16810. }
  16811. }
  16812. me.fireEvent('contentchange')
  16813. range.moveToBookmark(bk).select()
  16814. return true
  16815. }
  16816. }
  16817. })
  16818. function getLi(start) {
  16819. while (start && !domUtils.isBody(start)) {
  16820. if (start.nodeName == 'TABLE') {
  16821. return null
  16822. }
  16823. if (start.nodeName == 'LI') {
  16824. return start
  16825. }
  16826. start = start.parentNode
  16827. }
  16828. }
  16829. /**
  16830. * 有序列表,与“insertunorderedlist”命令互斥
  16831. * @command insertorderedlist
  16832. * @method execCommand
  16833. * @param { String } command 命令字符串
  16834. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16835. * @example
  16836. * ```javascript
  16837. * editor.execCommand( 'insertorderedlist','decimal');
  16838. * ```
  16839. */
  16840. /**
  16841. * 查询当前选区内容是否有序列表
  16842. * @command insertorderedlist
  16843. * @method queryCommandState
  16844. * @param { String } cmd 命令字符串
  16845. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  16846. * @example
  16847. * ```javascript
  16848. * editor.queryCommandState( 'insertorderedlist' );
  16849. * ```
  16850. */
  16851. /**
  16852. * 查询当前选区内容是否有序列表
  16853. * @command insertorderedlist
  16854. * @method queryCommandValue
  16855. * @param { String } cmd 命令字符串
  16856. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16857. * @example
  16858. * ```javascript
  16859. * editor.queryCommandValue( 'insertorderedlist' );
  16860. * ```
  16861. */
  16862. /**
  16863. * 无序列表,与“insertorderedlist”命令互斥
  16864. * @command insertunorderedlist
  16865. * @method execCommand
  16866. * @param { String } command 命令字符串
  16867. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  16868. * @example
  16869. * ```javascript
  16870. * editor.execCommand( 'insertunorderedlist','circle');
  16871. * ```
  16872. */
  16873. /**
  16874. * 查询当前是否有word文档粘贴进来的图片
  16875. * @command insertunorderedlist
  16876. * @method insertunorderedlist
  16877. * @param { String } command 命令字符串
  16878. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  16879. * @example
  16880. * ```javascript
  16881. * editor.queryCommandState( 'insertunorderedlist' );
  16882. * ```
  16883. */
  16884. /**
  16885. * 查询当前选区内容是否有序列表
  16886. * @command insertunorderedlist
  16887. * @method queryCommandValue
  16888. * @param { String } command 命令字符串
  16889. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  16890. * @example
  16891. * ```javascript
  16892. * editor.queryCommandValue( 'insertunorderedlist' );
  16893. * ```
  16894. */
  16895. me.commands['insertorderedlist'] = me.commands['insertunorderedlist'] = {
  16896. execCommand: function(command, style) {
  16897. if (!style) {
  16898. style =
  16899. command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc'
  16900. }
  16901. var me = this,
  16902. range = this.selection.getRange(),
  16903. filterFn = function(node) {
  16904. return node.nodeType == 1
  16905. ? node.tagName.toLowerCase() != 'br'
  16906. : !domUtils.isWhitespace(node)
  16907. },
  16908. tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul',
  16909. frag = me.document.createDocumentFragment()
  16910. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  16911. //range.shrinkBoundary();//.adjustmentBoundary();
  16912. range.adjustmentBoundary().shrinkBoundary()
  16913. var bko = range.createBookmark(true),
  16914. start = getLi(me.document.getElementById(bko.start)),
  16915. modifyStart = 0,
  16916. end = getLi(me.document.getElementById(bko.end)),
  16917. modifyEnd = 0,
  16918. startParent,
  16919. endParent,
  16920. list,
  16921. tmp
  16922. if (start || end) {
  16923. start && (startParent = start.parentNode)
  16924. if (!bko.end) {
  16925. end = start
  16926. }
  16927. end && (endParent = end.parentNode)
  16928. if (startParent === endParent) {
  16929. while (start !== end) {
  16930. tmp = start
  16931. start = start.nextSibling
  16932. if (!domUtils.isBlockElm(tmp.firstChild)) {
  16933. var p = me.document.createElement('p')
  16934. while (tmp.firstChild) {
  16935. p.appendChild(tmp.firstChild)
  16936. }
  16937. tmp.appendChild(p)
  16938. }
  16939. frag.appendChild(tmp)
  16940. }
  16941. tmp = me.document.createElement('span')
  16942. startParent.insertBefore(tmp, end)
  16943. if (!domUtils.isBlockElm(end.firstChild)) {
  16944. p = me.document.createElement('p')
  16945. while (end.firstChild) {
  16946. p.appendChild(end.firstChild)
  16947. }
  16948. end.appendChild(p)
  16949. }
  16950. frag.appendChild(end)
  16951. domUtils.breakParent(tmp, startParent)
  16952. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  16953. domUtils.remove(tmp.previousSibling)
  16954. }
  16955. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  16956. domUtils.remove(tmp.nextSibling)
  16957. }
  16958. var nodeStyle =
  16959. getStyle(startParent) ||
  16960. domUtils.getComputedStyle(startParent, 'list-style-type') ||
  16961. (command.toLowerCase() == 'insertorderedlist'
  16962. ? 'decimal'
  16963. : 'disc')
  16964. if (
  16965. startParent.tagName.toLowerCase() == tag &&
  16966. nodeStyle == style
  16967. ) {
  16968. for (
  16969. var i = 0, ci, tmpFrag = me.document.createDocumentFragment();
  16970. (ci = frag.firstChild);
  16971. ) {
  16972. if (domUtils.isTagNode(ci, 'ol ul')) {
  16973. // 删除时,子列表不处理
  16974. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  16975. // while(li.firstChild){
  16976. // tmpFrag.appendChild(li.firstChild);
  16977. // }
  16978. //
  16979. // });
  16980. tmpFrag.appendChild(ci)
  16981. } else {
  16982. while (ci.firstChild) {
  16983. tmpFrag.appendChild(ci.firstChild)
  16984. domUtils.remove(ci)
  16985. }
  16986. }
  16987. }
  16988. tmp.parentNode.insertBefore(tmpFrag, tmp)
  16989. } else {
  16990. list = me.document.createElement(tag)
  16991. setListStyle(list, style)
  16992. list.appendChild(frag)
  16993. tmp.parentNode.insertBefore(list, tmp)
  16994. }
  16995. domUtils.remove(tmp)
  16996. list && adjustList(list, tag, style)
  16997. range.moveToBookmark(bko).select()
  16998. return
  16999. }
  17000. //开始
  17001. if (start) {
  17002. while (start) {
  17003. tmp = start.nextSibling
  17004. if (domUtils.isTagNode(start, 'ol ul')) {
  17005. frag.appendChild(start)
  17006. } else {
  17007. var tmpfrag = me.document.createDocumentFragment(),
  17008. hasBlock = 0
  17009. while (start.firstChild) {
  17010. if (domUtils.isBlockElm(start.firstChild)) {
  17011. hasBlock = 1
  17012. }
  17013. tmpfrag.appendChild(start.firstChild)
  17014. }
  17015. if (!hasBlock) {
  17016. var tmpP = me.document.createElement('p')
  17017. tmpP.appendChild(tmpfrag)
  17018. frag.appendChild(tmpP)
  17019. } else {
  17020. frag.appendChild(tmpfrag)
  17021. }
  17022. domUtils.remove(start)
  17023. }
  17024. start = tmp
  17025. }
  17026. startParent.parentNode.insertBefore(frag, startParent.nextSibling)
  17027. if (domUtils.isEmptyNode(startParent)) {
  17028. range.setStartBefore(startParent)
  17029. domUtils.remove(startParent)
  17030. } else {
  17031. range.setStartAfter(startParent)
  17032. }
  17033. modifyStart = 1
  17034. }
  17035. if (end && domUtils.inDoc(endParent, me.document)) {
  17036. //结束
  17037. start = endParent.firstChild
  17038. while (start && start !== end) {
  17039. tmp = start.nextSibling
  17040. if (domUtils.isTagNode(start, 'ol ul')) {
  17041. frag.appendChild(start)
  17042. } else {
  17043. tmpfrag = me.document.createDocumentFragment()
  17044. hasBlock = 0
  17045. while (start.firstChild) {
  17046. if (domUtils.isBlockElm(start.firstChild)) {
  17047. hasBlock = 1
  17048. }
  17049. tmpfrag.appendChild(start.firstChild)
  17050. }
  17051. if (!hasBlock) {
  17052. tmpP = me.document.createElement('p')
  17053. tmpP.appendChild(tmpfrag)
  17054. frag.appendChild(tmpP)
  17055. } else {
  17056. frag.appendChild(tmpfrag)
  17057. }
  17058. domUtils.remove(start)
  17059. }
  17060. start = tmp
  17061. }
  17062. var tmpDiv = domUtils.createElement(me.document, 'div', {
  17063. tmpDiv: 1
  17064. })
  17065. domUtils.moveChild(end, tmpDiv)
  17066. frag.appendChild(tmpDiv)
  17067. domUtils.remove(end)
  17068. endParent.parentNode.insertBefore(frag, endParent)
  17069. range.setEndBefore(endParent)
  17070. if (domUtils.isEmptyNode(endParent)) {
  17071. domUtils.remove(endParent)
  17072. }
  17073. modifyEnd = 1
  17074. }
  17075. }
  17076. if (!modifyStart) {
  17077. range.setStartBefore(me.document.getElementById(bko.start))
  17078. }
  17079. if (bko.end && !modifyEnd) {
  17080. range.setEndAfter(me.document.getElementById(bko.end))
  17081. }
  17082. range.enlarge(true, function(node) {
  17083. return notExchange[node.tagName]
  17084. })
  17085. frag = me.document.createDocumentFragment()
  17086. var bk = range.createBookmark(),
  17087. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  17088. tmpRange = range.cloneRange(),
  17089. tmpNode,
  17090. block = domUtils.isBlockElm
  17091. while (
  17092. current &&
  17093. current !== bk.end &&
  17094. domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING
  17095. ) {
  17096. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  17097. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  17098. while (current.firstChild) {
  17099. frag.appendChild(current.firstChild)
  17100. }
  17101. tmpNode = domUtils.getNextDomNode(current, false, filterFn)
  17102. domUtils.remove(current)
  17103. current = tmpNode
  17104. continue
  17105. }
  17106. tmpNode = current
  17107. tmpRange.setStartBefore(current)
  17108. while (
  17109. current &&
  17110. current !== bk.end &&
  17111. (!block(current) || domUtils.isBookmarkNode(current))
  17112. ) {
  17113. tmpNode = current
  17114. current = domUtils.getNextDomNode(current, false, null, function(
  17115. node
  17116. ) {
  17117. return !notExchange[node.tagName]
  17118. })
  17119. }
  17120. if (current && block(current)) {
  17121. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn)
  17122. if (tmp && domUtils.isBookmarkNode(tmp)) {
  17123. current = domUtils.getNextDomNode(tmp, false, filterFn)
  17124. tmpNode = tmp
  17125. }
  17126. }
  17127. tmpRange.setEndAfter(tmpNode)
  17128. current = domUtils.getNextDomNode(tmpNode, false, filterFn)
  17129. var li = range.document.createElement('li')
  17130. li.appendChild(tmpRange.extractContents())
  17131. if (domUtils.isEmptyNode(li)) {
  17132. var tmpNode = range.document.createElement('p')
  17133. while (li.firstChild) {
  17134. tmpNode.appendChild(li.firstChild)
  17135. }
  17136. li.appendChild(tmpNode)
  17137. }
  17138. frag.appendChild(li)
  17139. } else {
  17140. current = domUtils.getNextDomNode(current, true, filterFn)
  17141. }
  17142. }
  17143. range.moveToBookmark(bk).collapse(true)
  17144. list = me.document.createElement(tag)
  17145. setListStyle(list, style)
  17146. list.appendChild(frag)
  17147. range.insertNode(list)
  17148. //当前list上下看能否合并
  17149. adjustList(list, tag, style)
  17150. //去掉冗余的tmpDiv
  17151. for (
  17152. var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, 'div');
  17153. (ci = tmpDivs[i++]);
  17154. ) {
  17155. if (ci.getAttribute('tmpDiv')) {
  17156. domUtils.remove(ci, true)
  17157. }
  17158. }
  17159. range.moveToBookmark(bko).select()
  17160. },
  17161. queryCommandState: function(command) {
  17162. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul'
  17163. var path = this.selection.getStartElementPath()
  17164. for (var i = 0, ci; (ci = path[i++]); ) {
  17165. if (ci.nodeName == 'TABLE') {
  17166. return 0
  17167. }
  17168. if (tag == ci.nodeName.toLowerCase()) {
  17169. return 1
  17170. }
  17171. }
  17172. return 0
  17173. },
  17174. queryCommandValue: function(command) {
  17175. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul'
  17176. var path = this.selection.getStartElementPath(),
  17177. node
  17178. for (var i = 0, ci; (ci = path[i++]); ) {
  17179. if (ci.nodeName == 'TABLE') {
  17180. node = null
  17181. break
  17182. }
  17183. if (tag == ci.nodeName.toLowerCase()) {
  17184. node = ci
  17185. break
  17186. }
  17187. }
  17188. return node
  17189. ? getStyle(node) || domUtils.getComputedStyle(node, 'list-style-type')
  17190. : null
  17191. }
  17192. }
  17193. }
  17194. // plugins/source.js
  17195. /**
  17196. * 源码编辑插件
  17197. * @file
  17198. * @since 1.2.6.1
  17199. */
  17200. ;(function() {
  17201. var sourceEditors = {
  17202. textarea: function(editor, holder) {
  17203. var textarea = holder.ownerDocument.createElement('textarea')
  17204. textarea.style.cssText =
  17205. 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;'
  17206. // todo: IE下只有onresize属性可用... 很纠结
  17207. if (browser.ie && browser.version < 8) {
  17208. textarea.style.width = holder.offsetWidth + 'px'
  17209. textarea.style.height = holder.offsetHeight + 'px'
  17210. holder.onresize = function() {
  17211. textarea.style.width = holder.offsetWidth + 'px'
  17212. textarea.style.height = holder.offsetHeight + 'px'
  17213. }
  17214. }
  17215. holder.appendChild(textarea)
  17216. return {
  17217. setContent: function(content) {
  17218. textarea.value = content
  17219. },
  17220. getContent: function() {
  17221. return textarea.value
  17222. },
  17223. select: function() {
  17224. var range
  17225. if (browser.ie) {
  17226. range = textarea.createTextRange()
  17227. range.collapse(true)
  17228. range.select()
  17229. } else {
  17230. //todo: chrome下无法设置焦点
  17231. textarea.setSelectionRange(0, 0)
  17232. textarea.focus()
  17233. }
  17234. },
  17235. dispose: function() {
  17236. holder.removeChild(textarea)
  17237. // todo
  17238. holder.onresize = null
  17239. textarea = null
  17240. holder = null
  17241. }
  17242. }
  17243. },
  17244. codemirror: function(editor, holder) {
  17245. var codeEditor = window.CodeMirror(holder, {
  17246. mode: 'text/html',
  17247. tabMode: 'indent',
  17248. lineNumbers: true,
  17249. lineWrapping: true
  17250. })
  17251. var dom = codeEditor.getWrapperElement()
  17252. dom.style.cssText =
  17253. 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;'
  17254. codeEditor.getScrollerElement().style.cssText =
  17255. 'position:absolute;left:0;top:0;width:100%;height:100%;'
  17256. codeEditor.refresh()
  17257. return {
  17258. getCodeMirror: function() {
  17259. return codeEditor
  17260. },
  17261. setContent: function(content) {
  17262. codeEditor.setValue(content)
  17263. },
  17264. getContent: function() {
  17265. return codeEditor.getValue()
  17266. },
  17267. select: function() {
  17268. codeEditor.focus()
  17269. },
  17270. dispose: function() {
  17271. holder.removeChild(dom)
  17272. dom = null
  17273. codeEditor = null
  17274. }
  17275. }
  17276. }
  17277. }
  17278. UE.plugins['source'] = function() {
  17279. var me = this
  17280. var opt = this.options
  17281. var sourceMode = false
  17282. var sourceEditor
  17283. var orgSetContent
  17284. opt.sourceEditor = browser.ie
  17285. ? 'textarea'
  17286. : opt.sourceEditor || 'codemirror'
  17287. me.setOpt({
  17288. sourceEditorFirst: false
  17289. })
  17290. function createSourceEditor(holder) {
  17291. return sourceEditors[
  17292. opt.sourceEditor == 'codemirror' && window.CodeMirror
  17293. ? 'codemirror'
  17294. : 'textarea'
  17295. ](me, holder)
  17296. }
  17297. var bakCssText
  17298. //解决在源码模式下getContent不能得到最新的内容问题
  17299. var oldGetContent, bakAddress
  17300. /**
  17301. * 切换源码模式和编辑模式
  17302. * @command source
  17303. * @method execCommand
  17304. * @param { String } cmd 命令字符串
  17305. * @example
  17306. * ```javascript
  17307. * editor.execCommand( 'source');
  17308. * ```
  17309. */
  17310. /**
  17311. * 查询当前编辑区域的状态是源码模式还是可视化模式
  17312. * @command source
  17313. * @method queryCommandState
  17314. * @param { String } cmd 命令字符串
  17315. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  17316. * @example
  17317. * ```javascript
  17318. * editor.queryCommandState( 'source' );
  17319. * ```
  17320. */
  17321. me.commands['source'] = {
  17322. execCommand: function() {
  17323. sourceMode = !sourceMode
  17324. if (sourceMode) {
  17325. bakAddress = me.selection.getRange().createAddress(false, true)
  17326. me.undoManger && me.undoManger.save(true)
  17327. if (browser.gecko) {
  17328. me.body.contentEditable = false
  17329. }
  17330. bakCssText = me.iframe.style.cssText
  17331. me.iframe.style.cssText +=
  17332. 'position:absolute;left:-32768px;top:-32768px;'
  17333. me.fireEvent('beforegetcontent')
  17334. var root = UE.htmlparser(me.body.innerHTML)
  17335. me.filterOutputRule(root)
  17336. root.traversal(function(node) {
  17337. if (node.type == 'element') {
  17338. switch (node.tagName) {
  17339. case 'td':
  17340. case 'th':
  17341. case 'caption':
  17342. if (node.children && node.children.length == 1) {
  17343. if (node.firstChild().tagName == 'br') {
  17344. node.removeChild(node.firstChild())
  17345. }
  17346. }
  17347. break
  17348. case 'pre':
  17349. node.innerText(node.innerText().replace(/&nbsp;/g, ' '))
  17350. }
  17351. }
  17352. })
  17353. me.fireEvent('aftergetcontent')
  17354. var content = root.toHtml(true)
  17355. sourceEditor = createSourceEditor(me.iframe.parentNode)
  17356. sourceEditor.setContent(content)
  17357. orgSetContent = me.setContent
  17358. me.setContent = function(html) {
  17359. //这里暂时不触发事件,防止报错
  17360. var root = UE.htmlparser(html)
  17361. me.filterInputRule(root)
  17362. html = root.toHtml()
  17363. sourceEditor.setContent(html)
  17364. }
  17365. setTimeout(function() {
  17366. sourceEditor.select()
  17367. me.addListener('fullscreenchanged', function() {
  17368. try {
  17369. sourceEditor.getCodeMirror().refresh()
  17370. } catch (e) {}
  17371. })
  17372. })
  17373. //重置getContent,源码模式下取值也能是最新的数据
  17374. oldGetContent = me.getContent
  17375. me.getContent = function() {
  17376. return (
  17377. sourceEditor.getContent() ||
  17378. '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17379. )
  17380. }
  17381. } else {
  17382. me.iframe.style.cssText = bakCssText
  17383. var cont =
  17384. sourceEditor.getContent() ||
  17385. '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17386. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  17387. cont = cont.replace(
  17388. new RegExp('[\\r\\t\\n ]*</?(\\w+)\\s*(?:[^>]*)>', 'g'),
  17389. function(a, b) {
  17390. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  17391. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, '')
  17392. }
  17393. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, '')
  17394. }
  17395. )
  17396. me.setContent = orgSetContent
  17397. me.setContent(cont)
  17398. sourceEditor.dispose()
  17399. sourceEditor = null
  17400. //还原getContent方法
  17401. me.getContent = oldGetContent
  17402. var first = me.body.firstChild
  17403. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  17404. if (!first) {
  17405. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17406. first = me.body.firstChild
  17407. }
  17408. //要在ifm为显示时ff才能取到selection,否则报错
  17409. //这里不能比较位置了
  17410. me.undoManger && me.undoManger.save(true)
  17411. if (browser.gecko) {
  17412. var input = document.createElement('input')
  17413. input.style.cssText = 'position:absolute;left:0;top:-32768px'
  17414. document.body.appendChild(input)
  17415. me.body.contentEditable = false
  17416. setTimeout(function() {
  17417. domUtils.setViewportOffset(input, {left: -32768, top: 0})
  17418. input.focus()
  17419. setTimeout(function() {
  17420. me.body.contentEditable = true
  17421. me.selection
  17422. .getRange()
  17423. .moveToAddress(bakAddress)
  17424. .select(true)
  17425. domUtils.remove(input)
  17426. })
  17427. })
  17428. } else {
  17429. //ie下有可能报错,比如在代码顶头的情况
  17430. try {
  17431. me.selection
  17432. .getRange()
  17433. .moveToAddress(bakAddress)
  17434. .select(true)
  17435. } catch (e) {}
  17436. }
  17437. }
  17438. this.fireEvent('sourcemodechanged', sourceMode)
  17439. },
  17440. queryCommandState: function() {
  17441. return sourceMode | 0
  17442. },
  17443. notNeedUndo: 1
  17444. }
  17445. var oldQueryCommandState = me.queryCommandState
  17446. me.queryCommandState = function(cmdName) {
  17447. cmdName = cmdName.toLowerCase()
  17448. if (sourceMode) {
  17449. //源码模式下可以开启的命令
  17450. return cmdName in
  17451. {
  17452. source: 1,
  17453. fullscreen: 1
  17454. }
  17455. ? 1
  17456. : -1
  17457. }
  17458. return oldQueryCommandState.apply(this, arguments)
  17459. }
  17460. if (opt.sourceEditor == 'codemirror') {
  17461. me.addListener('ready', function() {
  17462. utils.loadFile(
  17463. document,
  17464. {
  17465. src:
  17466. opt.codeMirrorJsUrl ||
  17467. opt.UEDITOR_HOME_URL + 'third-party/codemirror/codemirror.js',
  17468. tag: 'script',
  17469. type: 'text/javascript',
  17470. defer: 'defer'
  17471. },
  17472. function() {
  17473. if (opt.sourceEditorFirst) {
  17474. setTimeout(function() {
  17475. me.execCommand('source')
  17476. }, 0)
  17477. }
  17478. }
  17479. )
  17480. utils.loadFile(document, {
  17481. tag: 'link',
  17482. rel: 'stylesheet',
  17483. type: 'text/css',
  17484. href:
  17485. opt.codeMirrorCssUrl ||
  17486. opt.UEDITOR_HOME_URL + 'third-party/codemirror/codemirror.css'
  17487. })
  17488. })
  17489. }
  17490. }
  17491. })()
  17492. // plugins/enterkey.js
  17493. ///import core
  17494. ///import plugins/undo.js
  17495. ///commands 设置回车标签p或br
  17496. ///commandsName EnterKey
  17497. ///commandsTitle 设置回车标签p或br
  17498. /**
  17499. * @description 处理回车
  17500. * @author zhanyi
  17501. */
  17502. UE.plugins['enterkey'] = function() {
  17503. var hTag,
  17504. me = this,
  17505. tag = me.options.enterTag
  17506. me.addListener('keyup', function(type, evt) {
  17507. var keyCode = evt.keyCode || evt.which
  17508. if (keyCode == 13) {
  17509. var range = me.selection.getRange(),
  17510. start = range.startContainer,
  17511. doSave
  17512. //修正在h1-h6里边回车后不能嵌套p的问题
  17513. if (!browser.ie) {
  17514. if (/h\d/i.test(hTag)) {
  17515. if (browser.gecko) {
  17516. var h = domUtils.findParentByTagName(
  17517. start,
  17518. [
  17519. 'h1',
  17520. 'h2',
  17521. 'h3',
  17522. 'h4',
  17523. 'h5',
  17524. 'h6',
  17525. 'blockquote',
  17526. 'caption',
  17527. 'table'
  17528. ],
  17529. true
  17530. )
  17531. if (!h) {
  17532. me.document.execCommand('formatBlock', false, '<p>')
  17533. doSave = 1
  17534. }
  17535. } else {
  17536. //chrome remove div
  17537. if (start.nodeType == 1) {
  17538. var tmp = me.document.createTextNode(''),
  17539. div
  17540. range.insertNode(tmp)
  17541. div = domUtils.findParentByTagName(tmp, 'div', true)
  17542. if (div) {
  17543. var p = me.document.createElement('p')
  17544. while (div.firstChild) {
  17545. p.appendChild(div.firstChild)
  17546. }
  17547. div.parentNode.insertBefore(p, div)
  17548. domUtils.remove(div)
  17549. range.setStartBefore(tmp).setCursor()
  17550. doSave = 1
  17551. }
  17552. domUtils.remove(tmp)
  17553. }
  17554. }
  17555. if (me.undoManger && doSave) {
  17556. me.undoManger.save()
  17557. }
  17558. }
  17559. //没有站位符,会出现多行的问题
  17560. browser.opera && range.select()
  17561. } else {
  17562. me.fireEvent('saveScene', true, true)
  17563. }
  17564. }
  17565. })
  17566. me.addListener('keydown', function(type, evt) {
  17567. var keyCode = evt.keyCode || evt.which
  17568. if (keyCode == 13) {
  17569. //回车
  17570. if (me.fireEvent('beforeenterkeydown')) {
  17571. domUtils.preventDefault(evt)
  17572. return
  17573. }
  17574. me.fireEvent('saveScene', true, true)
  17575. hTag = ''
  17576. var range = me.selection.getRange()
  17577. if (!range.collapsed) {
  17578. //跨td不能删
  17579. var start = range.startContainer,
  17580. end = range.endContainer,
  17581. startTd = domUtils.findParentByTagName(start, 'td', true),
  17582. endTd = domUtils.findParentByTagName(end, 'td', true)
  17583. if (
  17584. (startTd && endTd && startTd !== endTd) ||
  17585. (!startTd && endTd) ||
  17586. (startTd && !endTd)
  17587. ) {
  17588. evt.preventDefault
  17589. ? evt.preventDefault()
  17590. : (evt.returnValue = false)
  17591. return
  17592. }
  17593. }
  17594. if (tag == 'p') {
  17595. if (!browser.ie) {
  17596. start = domUtils.findParentByTagName(
  17597. range.startContainer,
  17598. [
  17599. 'ol',
  17600. 'ul',
  17601. 'p',
  17602. 'h1',
  17603. 'h2',
  17604. 'h3',
  17605. 'h4',
  17606. 'h5',
  17607. 'h6',
  17608. 'blockquote',
  17609. 'caption'
  17610. ],
  17611. true
  17612. )
  17613. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  17614. //trace:2431
  17615. if (!start && !browser.opera) {
  17616. me.document.execCommand('formatBlock', false, '<p>')
  17617. if (browser.gecko) {
  17618. range = me.selection.getRange()
  17619. start = domUtils.findParentByTagName(
  17620. range.startContainer,
  17621. 'p',
  17622. true
  17623. )
  17624. start && domUtils.removeDirtyAttr(start)
  17625. }
  17626. } else {
  17627. hTag = start.tagName
  17628. start.tagName.toLowerCase() == 'p' &&
  17629. browser.gecko &&
  17630. domUtils.removeDirtyAttr(start)
  17631. }
  17632. }
  17633. } else {
  17634. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  17635. if (!range.collapsed) {
  17636. range.deleteContents()
  17637. start = range.startContainer
  17638. if (
  17639. start.nodeType == 1 &&
  17640. (start = start.childNodes[range.startOffset])
  17641. ) {
  17642. while (start.nodeType == 1) {
  17643. if (dtd.$empty[start.tagName]) {
  17644. range.setStartBefore(start).setCursor()
  17645. if (me.undoManger) {
  17646. me.undoManger.save()
  17647. }
  17648. return false
  17649. }
  17650. if (!start.firstChild) {
  17651. var br = range.document.createElement('br')
  17652. start.appendChild(br)
  17653. range.setStart(start, 0).setCursor()
  17654. if (me.undoManger) {
  17655. me.undoManger.save()
  17656. }
  17657. return false
  17658. }
  17659. start = start.firstChild
  17660. }
  17661. if (
  17662. start === range.startContainer.childNodes[range.startOffset]
  17663. ) {
  17664. br = range.document.createElement('br')
  17665. range.insertNode(br).setCursor()
  17666. } else {
  17667. range.setStart(start, 0).setCursor()
  17668. }
  17669. } else {
  17670. br = range.document.createElement('br')
  17671. range
  17672. .insertNode(br)
  17673. .setStartAfter(br)
  17674. .setCursor()
  17675. }
  17676. } else {
  17677. br = range.document.createElement('br')
  17678. range.insertNode(br)
  17679. var parent = br.parentNode
  17680. if (parent.lastChild === br) {
  17681. br.parentNode.insertBefore(br.cloneNode(true), br)
  17682. range.setStartBefore(br)
  17683. } else {
  17684. range.setStartAfter(br)
  17685. }
  17686. range.setCursor()
  17687. }
  17688. }
  17689. }
  17690. })
  17691. }
  17692. // plugins/keystrokes.js
  17693. /* 处理特殊键的兼容性问题 */
  17694. UE.plugins['keystrokes'] = function() {
  17695. var me = this
  17696. var collapsed = true
  17697. me.addListener('keydown', function(type, evt) {
  17698. var keyCode = evt.keyCode || evt.which,
  17699. rng = me.selection.getRange()
  17700. //处理全选的情况
  17701. if (
  17702. !rng.collapsed &&
  17703. !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) &&
  17704. ((keyCode >= 65 && keyCode <= 90) ||
  17705. (keyCode >= 48 && keyCode <= 57) ||
  17706. (keyCode >= 96 && keyCode <= 111) ||
  17707. {
  17708. 13: 1,
  17709. 8: 1,
  17710. 46: 1
  17711. }[keyCode])
  17712. ) {
  17713. var tmpNode = rng.startContainer
  17714. if (domUtils.isFillChar(tmpNode)) {
  17715. rng.setStartBefore(tmpNode)
  17716. }
  17717. tmpNode = rng.endContainer
  17718. if (domUtils.isFillChar(tmpNode)) {
  17719. rng.setEndAfter(tmpNode)
  17720. }
  17721. rng.txtToElmBoundary()
  17722. //结束边界可能放到了br的前边,要把br包含进来
  17723. // x[xxx]<br/>
  17724. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  17725. tmpNode = rng.endContainer.childNodes[rng.endOffset]
  17726. if (tmpNode && domUtils.isBr(tmpNode)) {
  17727. rng.setEndAfter(tmpNode)
  17728. }
  17729. }
  17730. if (rng.startOffset == 0) {
  17731. tmpNode = rng.startContainer
  17732. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  17733. tmpNode = rng.endContainer
  17734. if (
  17735. rng.endOffset ==
  17736. (tmpNode.nodeType == 3
  17737. ? tmpNode.nodeValue.length
  17738. : tmpNode.childNodes.length) &&
  17739. domUtils.isBoundaryNode(tmpNode, 'lastChild')
  17740. ) {
  17741. me.fireEvent('saveScene')
  17742. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17743. rng.setStart(me.body.firstChild, 0).setCursor(false, true)
  17744. me._selectionChange()
  17745. return
  17746. }
  17747. }
  17748. }
  17749. }
  17750. //处理backspace
  17751. if (keyCode == keymap.Backspace) {
  17752. rng = me.selection.getRange()
  17753. collapsed = rng.collapsed
  17754. if (me.fireEvent('delkeydown', evt)) {
  17755. return
  17756. }
  17757. var start, end
  17758. //避免按两次删除才能生效的问题
  17759. if (rng.collapsed && rng.inFillChar()) {
  17760. start = rng.startContainer
  17761. if (domUtils.isFillChar(start)) {
  17762. rng
  17763. .setStartBefore(start)
  17764. .shrinkBoundary(true)
  17765. .collapse(true)
  17766. domUtils.remove(start)
  17767. } else {
  17768. start.nodeValue = start.nodeValue.replace(
  17769. new RegExp('^' + domUtils.fillChar),
  17770. ''
  17771. )
  17772. rng.startOffset--
  17773. rng.collapse(true).select(true)
  17774. }
  17775. }
  17776. //解决选中control元素不能删除的问题
  17777. if ((start = rng.getClosedNode())) {
  17778. me.fireEvent('saveScene')
  17779. rng.setStartBefore(start)
  17780. domUtils.remove(start)
  17781. rng.setCursor()
  17782. me.fireEvent('saveScene')
  17783. domUtils.preventDefault(evt)
  17784. return
  17785. }
  17786. //阻止在table上的删除
  17787. if (!browser.ie) {
  17788. start = domUtils.findParentByTagName(
  17789. rng.startContainer,
  17790. 'table',
  17791. true
  17792. )
  17793. end = domUtils.findParentByTagName(rng.endContainer, 'table', true)
  17794. if ((start && !end) || (!start && end) || start !== end) {
  17795. evt.preventDefault()
  17796. return
  17797. }
  17798. }
  17799. }
  17800. //处理tab键的逻辑
  17801. if (keyCode == keymap.Tab) {
  17802. //不处理以下标签
  17803. var excludeTagNameForTabKey = {
  17804. ol: 1,
  17805. ul: 1,
  17806. table: 1
  17807. }
  17808. //处理组件里的tab按下事件
  17809. if (me.fireEvent('tabkeydown', evt)) {
  17810. domUtils.preventDefault(evt)
  17811. return
  17812. }
  17813. var range = me.selection.getRange()
  17814. me.fireEvent('saveScene')
  17815. for (
  17816. var i = 0,
  17817. txt = '',
  17818. tabSize = me.options.tabSize || 4,
  17819. tabNode = me.options.tabNode || '&nbsp;';
  17820. i < tabSize;
  17821. i++
  17822. ) {
  17823. txt += tabNode
  17824. }
  17825. var span = me.document.createElement('span')
  17826. span.innerHTML = txt + domUtils.fillChar
  17827. if (range.collapsed) {
  17828. range.insertNode(span.cloneNode(true).firstChild).setCursor(true)
  17829. } else {
  17830. var filterFn = function(node) {
  17831. return (
  17832. domUtils.isBlockElm(node) &&
  17833. !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  17834. )
  17835. }
  17836. //普通的情况
  17837. start = domUtils.findParent(range.startContainer, filterFn, true)
  17838. end = domUtils.findParent(range.endContainer, filterFn, true)
  17839. if (start && end && start === end) {
  17840. range.deleteContents()
  17841. range.insertNode(span.cloneNode(true).firstChild).setCursor(true)
  17842. } else {
  17843. var bookmark = range.createBookmark()
  17844. range.enlarge(true)
  17845. var bookmark2 = range.createBookmark(),
  17846. current = domUtils.getNextDomNode(
  17847. bookmark2.start,
  17848. false,
  17849. filterFn
  17850. )
  17851. while (
  17852. current &&
  17853. !(
  17854. domUtils.getPosition(current, bookmark2.end) &
  17855. domUtils.POSITION_FOLLOWING
  17856. )
  17857. ) {
  17858. current.insertBefore(
  17859. span.cloneNode(true).firstChild,
  17860. current.firstChild
  17861. )
  17862. current = domUtils.getNextDomNode(current, false, filterFn)
  17863. }
  17864. range
  17865. .moveToBookmark(bookmark2)
  17866. .moveToBookmark(bookmark)
  17867. .select()
  17868. }
  17869. }
  17870. domUtils.preventDefault(evt)
  17871. }
  17872. //trace:1634
  17873. //ff的del键在容器空的时候,也会删除
  17874. if (browser.gecko && keyCode == 46) {
  17875. range = me.selection.getRange()
  17876. if (range.collapsed) {
  17877. start = range.startContainer
  17878. if (domUtils.isEmptyBlock(start)) {
  17879. var parent = start.parentNode
  17880. while (
  17881. domUtils.getChildCount(parent) == 1 &&
  17882. !domUtils.isBody(parent)
  17883. ) {
  17884. start = parent
  17885. parent = parent.parentNode
  17886. }
  17887. if (start === parent.lastChild) evt.preventDefault()
  17888. return
  17889. }
  17890. }
  17891. }
  17892. })
  17893. me.addListener('keyup', function(type, evt) {
  17894. var keyCode = evt.keyCode || evt.which,
  17895. rng,
  17896. me = this
  17897. if (keyCode == keymap.Backspace) {
  17898. if (me.fireEvent('delkeyup')) {
  17899. return
  17900. }
  17901. rng = me.selection.getRange()
  17902. if (rng.collapsed) {
  17903. var tmpNode,
  17904. autoClearTagName = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
  17905. if (
  17906. (tmpNode = domUtils.findParentByTagName(
  17907. rng.startContainer,
  17908. autoClearTagName,
  17909. true
  17910. ))
  17911. ) {
  17912. if (domUtils.isEmptyBlock(tmpNode)) {
  17913. var pre = tmpNode.previousSibling
  17914. if (pre && pre.nodeName != 'TABLE') {
  17915. domUtils.remove(tmpNode)
  17916. rng.setStartAtLast(pre).setCursor(false, true)
  17917. return
  17918. } else {
  17919. var next = tmpNode.nextSibling
  17920. if (next && next.nodeName != 'TABLE') {
  17921. domUtils.remove(tmpNode)
  17922. rng.setStartAtFirst(next).setCursor(false, true)
  17923. return
  17924. }
  17925. }
  17926. }
  17927. }
  17928. //处理当删除到body时,要重新给p标签展位
  17929. if (domUtils.isBody(rng.startContainer)) {
  17930. var tmpNode = domUtils.createElement(me.document, 'p', {
  17931. innerHTML: browser.ie ? domUtils.fillChar : '<br/>'
  17932. })
  17933. rng
  17934. .insertNode(tmpNode)
  17935. .setStart(tmpNode, 0)
  17936. .setCursor(false, true)
  17937. }
  17938. }
  17939. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  17940. if (
  17941. !collapsed &&
  17942. (rng.startContainer.nodeType == 3 ||
  17943. (rng.startContainer.nodeType == 1 &&
  17944. domUtils.isEmptyBlock(rng.startContainer)))
  17945. ) {
  17946. if (browser.ie) {
  17947. var span = rng.document.createElement('span')
  17948. rng
  17949. .insertNode(span)
  17950. .setStartBefore(span)
  17951. .collapse(true)
  17952. rng.select()
  17953. domUtils.remove(span)
  17954. } else {
  17955. rng.select()
  17956. }
  17957. }
  17958. }
  17959. })
  17960. }
  17961. // plugins/fiximgclick.js
  17962. ///import core
  17963. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17964. ///commandsName FixImgClick
  17965. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17966. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  17967. UE.plugins['fiximgclick'] = (function() {
  17968. var elementUpdated = false
  17969. function Scale() {
  17970. this.editor = null
  17971. this.resizer = null
  17972. this.cover = null
  17973. this.doc = document
  17974. this.prePos = {x: 0, y: 0}
  17975. this.startPos = {x: 0, y: 0}
  17976. }
  17977. ;(function() {
  17978. var rect = [
  17979. //[left, top, width, height]
  17980. [0, 0, -1, -1],
  17981. [0, 0, 0, -1],
  17982. [0, 0, 1, -1],
  17983. [0, 0, -1, 0],
  17984. [0, 0, 1, 0],
  17985. [0, 0, -1, 1],
  17986. [0, 0, 0, 1],
  17987. [0, 0, 1, 1]
  17988. ]
  17989. Scale.prototype = {
  17990. init: function(editor) {
  17991. var me = this
  17992. me.editor = editor
  17993. me.startPos = this.prePos = {x: 0, y: 0}
  17994. me.dragId = -1
  17995. var hands = [],
  17996. cover = (me.cover = document.createElement('div')),
  17997. resizer = (me.resizer = document.createElement('div'))
  17998. cover.id = me.editor.ui.id + '_imagescale_cover'
  17999. cover.style.cssText =
  18000. 'position:absolute;display:none;z-index:' +
  18001. me.editor.options.zIndex +
  18002. ';filter:alpha(opacity=0); opacity:0;background:#CCC;'
  18003. domUtils.on(cover, 'mousedown click', function() {
  18004. me.hide()
  18005. })
  18006. for (i = 0; i < 8; i++) {
  18007. hands.push(
  18008. '<span class="edui-editor-imagescale-hand' + i + '"></span>'
  18009. )
  18010. }
  18011. resizer.id = me.editor.ui.id + '_imagescale'
  18012. resizer.className = 'edui-editor-imagescale'
  18013. resizer.innerHTML = hands.join('')
  18014. resizer.style.cssText +=
  18015. ';display:none;border:1px solid #3b77ff;z-index:' +
  18016. me.editor.options.zIndex +
  18017. ';'
  18018. me.editor.ui.getDom().appendChild(cover)
  18019. me.editor.ui.getDom().appendChild(resizer)
  18020. me.initStyle()
  18021. me.initEvents()
  18022. },
  18023. initStyle: function() {
  18024. utils.cssRule(
  18025. 'imagescale',
  18026. '.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;}' +
  18027. '.edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}' +
  18028. '.edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}' +
  18029. '.edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}' +
  18030. '.edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}' +
  18031. '.edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}' +
  18032. '.edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}' +
  18033. '.edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}' +
  18034. '.edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}' +
  18035. '.edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}'
  18036. )
  18037. },
  18038. initEvents: function() {
  18039. var me = this
  18040. me.startPos.x = me.startPos.y = 0
  18041. me.isDraging = false
  18042. },
  18043. _eventHandler: function(e) {
  18044. var me = this
  18045. switch (e.type) {
  18046. case 'mousedown':
  18047. var hand = e.target || e.srcElement,
  18048. hand
  18049. if (
  18050. hand.className.indexOf('edui-editor-imagescale-hand') != -1 &&
  18051. me.dragId == -1
  18052. ) {
  18053. me.dragId = hand.className.slice(-1)
  18054. me.startPos.x = me.prePos.x = e.clientX
  18055. me.startPos.y = me.prePos.y = e.clientY
  18056. domUtils.on(me.doc, 'mousemove', me.proxy(me._eventHandler, me))
  18057. }
  18058. break
  18059. case 'mousemove':
  18060. if (me.dragId != -1) {
  18061. me.updateContainerStyle(me.dragId, {
  18062. x: e.clientX - me.prePos.x,
  18063. y: e.clientY - me.prePos.y
  18064. })
  18065. me.prePos.x = e.clientX
  18066. me.prePos.y = e.clientY
  18067. elementUpdated = true
  18068. me.updateTargetElement()
  18069. }
  18070. break
  18071. case 'mouseup':
  18072. if (me.dragId != -1) {
  18073. me.updateContainerStyle(me.dragId, {
  18074. x: e.clientX - me.prePos.x,
  18075. y: e.clientY - me.prePos.y
  18076. })
  18077. me.updateTargetElement()
  18078. if (me.target.parentNode) me.attachTo(me.target)
  18079. me.dragId = -1
  18080. }
  18081. domUtils.un(me.doc, 'mousemove', me.proxy(me._eventHandler, me))
  18082. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  18083. if (elementUpdated) {
  18084. elementUpdated = false
  18085. me.editor.fireEvent('contentchange')
  18086. }
  18087. break
  18088. default:
  18089. break
  18090. }
  18091. },
  18092. updateTargetElement: function() {
  18093. var me = this
  18094. domUtils.setStyles(me.target, {
  18095. width: me.resizer.style.width,
  18096. height: me.resizer.style.height
  18097. })
  18098. me.target.width = parseInt(me.resizer.style.width)
  18099. me.target.height = parseInt(me.resizer.style.height)
  18100. me.attachTo(me.target)
  18101. },
  18102. updateContainerStyle: function(dir, offset) {
  18103. var me = this,
  18104. dom = me.resizer,
  18105. tmp
  18106. if (rect[dir][0] != 0) {
  18107. tmp = parseInt(dom.style.left) + offset.x
  18108. dom.style.left = me._validScaledProp('left', tmp) + 'px'
  18109. }
  18110. if (rect[dir][1] != 0) {
  18111. tmp = parseInt(dom.style.top) + offset.y
  18112. dom.style.top = me._validScaledProp('top', tmp) + 'px'
  18113. }
  18114. if (rect[dir][2] != 0) {
  18115. tmp = dom.clientWidth + rect[dir][2] * offset.x
  18116. dom.style.width = me._validScaledProp('width', tmp) + 'px'
  18117. }
  18118. if (rect[dir][3] != 0) {
  18119. tmp = dom.clientHeight + rect[dir][3] * offset.y
  18120. dom.style.height = me._validScaledProp('height', tmp) + 'px'
  18121. }
  18122. },
  18123. _validScaledProp: function(prop, value) {
  18124. var ele = this.resizer,
  18125. wrap = document
  18126. value = isNaN(value) ? 0 : value
  18127. switch (prop) {
  18128. case 'left':
  18129. return value < 0
  18130. ? 0
  18131. : value + ele.clientWidth > wrap.clientWidth
  18132. ? wrap.clientWidth - ele.clientWidth
  18133. : value
  18134. case 'top':
  18135. return value < 0
  18136. ? 0
  18137. : value + ele.clientHeight > wrap.clientHeight
  18138. ? wrap.clientHeight - ele.clientHeight
  18139. : value
  18140. case 'width':
  18141. return value <= 0
  18142. ? 1
  18143. : value + ele.offsetLeft > wrap.clientWidth
  18144. ? wrap.clientWidth - ele.offsetLeft
  18145. : value
  18146. case 'height':
  18147. return value <= 0
  18148. ? 1
  18149. : value + ele.offsetTop > wrap.clientHeight
  18150. ? wrap.clientHeight - ele.offsetTop
  18151. : value
  18152. }
  18153. },
  18154. hideCover: function() {
  18155. this.cover.style.display = 'none'
  18156. },
  18157. showCover: function() {
  18158. var me = this,
  18159. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  18160. iframePos = domUtils.getXY(me.editor.iframe)
  18161. domUtils.setStyles(me.cover, {
  18162. width: me.editor.iframe.offsetWidth + 'px',
  18163. height: me.editor.iframe.offsetHeight + 'px',
  18164. top: iframePos.y - editorPos.y + 'px',
  18165. left: iframePos.x - editorPos.x + 'px',
  18166. position: 'absolute',
  18167. display: ''
  18168. })
  18169. },
  18170. show: function(targetObj) {
  18171. var me = this
  18172. me.resizer.style.display = 'block'
  18173. if (targetObj) me.attachTo(targetObj)
  18174. domUtils.on(this.resizer, 'mousedown', me.proxy(me._eventHandler, me))
  18175. domUtils.on(me.doc, 'mouseup', me.proxy(me._eventHandler, me))
  18176. me.showCover()
  18177. me.editor.fireEvent('afterscaleshow', me)
  18178. me.editor.fireEvent('saveScene')
  18179. },
  18180. hide: function() {
  18181. var me = this
  18182. me.hideCover()
  18183. me.resizer.style.display = 'none'
  18184. domUtils.un(me.resizer, 'mousedown', me.proxy(me._eventHandler, me))
  18185. domUtils.un(me.doc, 'mouseup', me.proxy(me._eventHandler, me))
  18186. me.editor.fireEvent('afterscalehide', me)
  18187. },
  18188. proxy: function(fn, context) {
  18189. return function(e) {
  18190. return fn.apply(context || this, arguments)
  18191. }
  18192. },
  18193. attachTo: function(targetObj) {
  18194. var me = this,
  18195. target = (me.target = targetObj),
  18196. resizer = this.resizer,
  18197. imgPos = domUtils.getXY(target),
  18198. iframePos = domUtils.getXY(me.editor.iframe),
  18199. editorPos = domUtils.getXY(resizer.parentNode)
  18200. domUtils.setStyles(resizer, {
  18201. width: target.width + 'px',
  18202. height: target.height + 'px',
  18203. left:
  18204. iframePos.x +
  18205. imgPos.x -
  18206. me.editor.document.body.scrollLeft -
  18207. editorPos.x -
  18208. parseInt(resizer.style.borderLeftWidth) +
  18209. 'px',
  18210. top:
  18211. iframePos.y +
  18212. imgPos.y -
  18213. me.editor.document.body.scrollTop -
  18214. editorPos.y -
  18215. parseInt(resizer.style.borderTopWidth) +
  18216. 'px'
  18217. })
  18218. }
  18219. }
  18220. })()
  18221. return function() {
  18222. var me = this,
  18223. imageScale
  18224. me.setOpt('imageScaleEnabled', true)
  18225. if (!browser.ie && me.options.imageScaleEnabled) {
  18226. me.addListener('click', function(type, e) {
  18227. var range = me.selection.getRange(),
  18228. img = range.getClosedNode()
  18229. if (
  18230. img &&
  18231. img.tagName == 'IMG' &&
  18232. me.body.contentEditable != 'false'
  18233. ) {
  18234. if (
  18235. img.className.indexOf('edui-faked-music') != -1 ||
  18236. img.getAttribute('anchorname') ||
  18237. domUtils.hasClass(img, 'loadingclass') ||
  18238. domUtils.hasClass(img, 'loaderrorclass')
  18239. ) {
  18240. return
  18241. }
  18242. if (!imageScale) {
  18243. imageScale = new Scale()
  18244. imageScale.init(me)
  18245. me.ui.getDom().appendChild(imageScale.resizer)
  18246. var _keyDownHandler = function(e) {
  18247. imageScale.hide()
  18248. if (imageScale.target)
  18249. me.selection
  18250. .getRange()
  18251. .selectNode(imageScale.target)
  18252. .select()
  18253. },
  18254. _mouseDownHandler = function(e) {
  18255. var ele = e.target || e.srcElement
  18256. if (
  18257. ele &&
  18258. (ele.className === undefined ||
  18259. ele.className.indexOf('edui-editor-imagescale') == -1)
  18260. ) {
  18261. _keyDownHandler(e)
  18262. }
  18263. },
  18264. timer
  18265. me.addListener('afterscaleshow', function(e) {
  18266. me.addListener('beforekeydown', _keyDownHandler)
  18267. me.addListener('beforemousedown', _mouseDownHandler)
  18268. domUtils.on(document, 'keydown', _keyDownHandler)
  18269. domUtils.on(document, 'mousedown', _mouseDownHandler)
  18270. me.selection.getNative().removeAllRanges()
  18271. })
  18272. me.addListener('afterscalehide', function(e) {
  18273. me.removeListener('beforekeydown', _keyDownHandler)
  18274. me.removeListener('beforemousedown', _mouseDownHandler)
  18275. domUtils.un(document, 'keydown', _keyDownHandler)
  18276. domUtils.un(document, 'mousedown', _mouseDownHandler)
  18277. var target = imageScale.target
  18278. if (target.parentNode) {
  18279. me.selection
  18280. .getRange()
  18281. .selectNode(target)
  18282. .select()
  18283. }
  18284. })
  18285. //TODO 有iframe的情况,mousedown不能往下传。。
  18286. domUtils.on(imageScale.resizer, 'mousedown', function(e) {
  18287. me.selection.getNative().removeAllRanges()
  18288. var ele = e.target || e.srcElement
  18289. if (
  18290. ele &&
  18291. ele.className.indexOf('edui-editor-imagescale-hand') == -1
  18292. ) {
  18293. timer = setTimeout(function() {
  18294. imageScale.hide()
  18295. if (imageScale.target)
  18296. me.selection
  18297. .getRange()
  18298. .selectNode(ele)
  18299. .select()
  18300. }, 200)
  18301. }
  18302. })
  18303. domUtils.on(imageScale.resizer, 'mouseup', function(e) {
  18304. var ele = e.target || e.srcElement
  18305. if (
  18306. ele &&
  18307. ele.className.indexOf('edui-editor-imagescale-hand') == -1
  18308. ) {
  18309. clearTimeout(timer)
  18310. }
  18311. })
  18312. }
  18313. imageScale.show(img)
  18314. } else {
  18315. if (imageScale && imageScale.resizer.style.display != 'none')
  18316. imageScale.hide()
  18317. }
  18318. })
  18319. }
  18320. if (browser.webkit) {
  18321. me.addListener('click', function(type, e) {
  18322. if (e.target.tagName == 'IMG' && me.body.contentEditable != 'false') {
  18323. var range = new dom.Range(me.document)
  18324. range.selectNode(e.target).select()
  18325. }
  18326. })
  18327. }
  18328. }
  18329. })()
  18330. // plugins/autolink.js
  18331. ///import core
  18332. ///commands 为非ie浏览器自动添加a标签
  18333. ///commandsName AutoLink
  18334. ///commandsTitle 自动增加链接
  18335. /**
  18336. * @description 为非ie浏览器自动添加a标签
  18337. * @author zhanyi
  18338. */
  18339. UE.plugin.register(
  18340. 'autolink',
  18341. function() {
  18342. var cont = 0
  18343. return !browser.ie
  18344. ? {
  18345. bindEvents: {
  18346. reset: function() {
  18347. cont = 0
  18348. },
  18349. keydown: function(type, evt) {
  18350. var me = this
  18351. var keyCode = evt.keyCode || evt.which
  18352. if (keyCode == 32 || keyCode == 13) {
  18353. var sel = me.selection.getNative(),
  18354. range = sel.getRangeAt(0).cloneRange(),
  18355. offset,
  18356. charCode
  18357. var start = range.startContainer
  18358. while (start.nodeType == 1 && range.startOffset > 0) {
  18359. start =
  18360. range.startContainer.childNodes[range.startOffset - 1]
  18361. if (!start) {
  18362. break
  18363. }
  18364. range.setStart(
  18365. start,
  18366. start.nodeType == 1
  18367. ? start.childNodes.length
  18368. : start.nodeValue.length
  18369. )
  18370. range.collapse(true)
  18371. start = range.startContainer
  18372. }
  18373. do {
  18374. if (range.startOffset == 0) {
  18375. start = range.startContainer.previousSibling
  18376. while (start && start.nodeType == 1) {
  18377. start = start.lastChild
  18378. }
  18379. if (!start || domUtils.isFillChar(start)) {
  18380. break
  18381. }
  18382. offset = start.nodeValue.length
  18383. } else {
  18384. start = range.startContainer
  18385. offset = range.startOffset
  18386. }
  18387. range.setStart(start, offset - 1)
  18388. charCode = range.toString().charCodeAt(0)
  18389. } while (charCode != 160 && charCode != 32)
  18390. if (
  18391. range
  18392. .toString()
  18393. .replace(new RegExp(domUtils.fillChar, 'g'), '')
  18394. .match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)
  18395. ) {
  18396. while (range.toString().length) {
  18397. if (
  18398. /^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(
  18399. range.toString()
  18400. )
  18401. ) {
  18402. break
  18403. }
  18404. try {
  18405. range.setStart(
  18406. range.startContainer,
  18407. range.startOffset + 1
  18408. )
  18409. } catch (e) {
  18410. //trace:2121
  18411. var start = range.startContainer
  18412. while (!(next = start.nextSibling)) {
  18413. if (domUtils.isBody(start)) {
  18414. return
  18415. }
  18416. start = start.parentNode
  18417. }
  18418. range.setStart(next, 0)
  18419. }
  18420. }
  18421. //range的开始边界已经在a标签里的不再处理
  18422. if (
  18423. domUtils.findParentByTagName(
  18424. range.startContainer,
  18425. 'a',
  18426. true
  18427. )
  18428. ) {
  18429. return
  18430. }
  18431. var a = me.document.createElement('a'),
  18432. text = me.document.createTextNode(' '),
  18433. href
  18434. me.undoManger && me.undoManger.save()
  18435. a.appendChild(range.extractContents())
  18436. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, '')
  18437. href = a
  18438. .getAttribute('href')
  18439. .replace(new RegExp(domUtils.fillChar, 'g'), '')
  18440. href = /^(?:https?:\/\/)/gi.test(href)
  18441. ? href
  18442. : 'http://' + href
  18443. a.setAttribute('_src', utils.html(href))
  18444. a.href = utils.html(href)
  18445. range.insertNode(a)
  18446. a.parentNode.insertBefore(text, a.nextSibling)
  18447. range.setStart(text, 0)
  18448. range.collapse(true)
  18449. sel.removeAllRanges()
  18450. sel.addRange(range)
  18451. me.undoManger && me.undoManger.save()
  18452. }
  18453. }
  18454. }
  18455. }
  18456. }
  18457. : {}
  18458. },
  18459. function() {
  18460. var keyCodes = {
  18461. 37: 1,
  18462. 38: 1,
  18463. 39: 1,
  18464. 40: 1,
  18465. 13: 1,
  18466. 32: 1
  18467. }
  18468. function checkIsCludeLink(node) {
  18469. if (node.nodeType == 3) {
  18470. return null
  18471. }
  18472. if (node.nodeName == 'A') {
  18473. return node
  18474. }
  18475. var lastChild = node.lastChild
  18476. while (lastChild) {
  18477. if (lastChild.nodeName == 'A') {
  18478. return lastChild
  18479. }
  18480. if (lastChild.nodeType == 3) {
  18481. if (domUtils.isWhitespace(lastChild)) {
  18482. lastChild = lastChild.previousSibling
  18483. continue
  18484. }
  18485. return null
  18486. }
  18487. lastChild = lastChild.lastChild
  18488. }
  18489. }
  18490. browser.ie &&
  18491. this.addListener('keyup', function(cmd, evt) {
  18492. var me = this,
  18493. keyCode = evt.keyCode
  18494. if (keyCodes[keyCode]) {
  18495. var rng = me.selection.getRange()
  18496. var start = rng.startContainer
  18497. if (keyCode == 13) {
  18498. while (
  18499. start &&
  18500. !domUtils.isBody(start) &&
  18501. !domUtils.isBlockElm(start)
  18502. ) {
  18503. start = start.parentNode
  18504. }
  18505. if (start && !domUtils.isBody(start) && start.nodeName == 'P') {
  18506. var pre = start.previousSibling
  18507. if (pre && pre.nodeType == 1) {
  18508. var pre = checkIsCludeLink(pre)
  18509. if (pre && !pre.getAttribute('_href')) {
  18510. domUtils.remove(pre, true)
  18511. }
  18512. }
  18513. }
  18514. } else if (keyCode == 32) {
  18515. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  18516. start = start.previousSibling
  18517. if (
  18518. start &&
  18519. start.nodeName == 'A' &&
  18520. !start.getAttribute('_href')
  18521. ) {
  18522. domUtils.remove(start, true)
  18523. }
  18524. }
  18525. } else {
  18526. start = domUtils.findParentByTagName(start, 'a', true)
  18527. if (start && !start.getAttribute('_href')) {
  18528. var bk = rng.createBookmark()
  18529. domUtils.remove(start, true)
  18530. rng.moveToBookmark(bk).select(true)
  18531. }
  18532. }
  18533. }
  18534. })
  18535. }
  18536. )
  18537. // plugins/autoheight.js
  18538. ///import core
  18539. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  18540. ///commandsName AutoHeight,autoHeightEnabled
  18541. ///commandsTitle 自动增高
  18542. /**
  18543. * @description 自动伸展
  18544. * @author zhanyi
  18545. */
  18546. UE.plugins['autoheight'] = function() {
  18547. var me = this
  18548. //提供开关,就算加载也可以关闭
  18549. me.autoHeightEnabled = me.options.autoHeightEnabled !== false
  18550. if (!me.autoHeightEnabled) {
  18551. return
  18552. }
  18553. var bakOverflow,
  18554. lastHeight = 0,
  18555. options = me.options,
  18556. currentHeight,
  18557. timer
  18558. function adjustHeight() {
  18559. var me = this
  18560. clearTimeout(timer)
  18561. if (isFullscreen) return
  18562. if (
  18563. !me.queryCommandState ||
  18564. (me.queryCommandState && me.queryCommandState('source') != 1)
  18565. ) {
  18566. timer = setTimeout(function() {
  18567. var node = me.body.lastChild
  18568. while (node && node.nodeType != 1) {
  18569. node = node.previousSibling
  18570. }
  18571. if (node && node.nodeType == 1) {
  18572. node.style.clear = 'both'
  18573. currentHeight = Math.max(
  18574. domUtils.getXY(node).y + node.offsetHeight + 25,
  18575. Math.max(options.minFrameHeight, options.initialFrameHeight)
  18576. )
  18577. if (currentHeight != lastHeight) {
  18578. if (
  18579. currentHeight !== parseInt(me.iframe.parentNode.style.height)
  18580. ) {
  18581. me.iframe.parentNode.style.height = currentHeight + 'px'
  18582. }
  18583. me.body.style.height = currentHeight + 'px'
  18584. lastHeight = currentHeight
  18585. }
  18586. domUtils.removeStyle(node, 'clear')
  18587. }
  18588. }, 50)
  18589. }
  18590. }
  18591. var isFullscreen
  18592. me.addListener('fullscreenchanged', function(cmd, f) {
  18593. isFullscreen = f
  18594. })
  18595. me.addListener('destroy', function() {
  18596. me.removeListener(
  18597. 'contentchange afterinserthtml keyup mouseup',
  18598. adjustHeight
  18599. )
  18600. })
  18601. me.enableAutoHeight = function() {
  18602. var me = this
  18603. if (!me.autoHeightEnabled) {
  18604. return
  18605. }
  18606. var doc = me.document
  18607. me.autoHeightEnabled = true
  18608. bakOverflow = doc.body.style.overflowY
  18609. doc.body.style.overflowY = 'hidden'
  18610. me.addListener(
  18611. 'contentchange afterinserthtml keyup mouseup',
  18612. adjustHeight
  18613. )
  18614. //ff不给事件算得不对
  18615. setTimeout(
  18616. function() {
  18617. adjustHeight.call(me)
  18618. },
  18619. browser.gecko ? 100 : 0
  18620. )
  18621. me.fireEvent('autoheightchanged', me.autoHeightEnabled)
  18622. }
  18623. me.disableAutoHeight = function() {
  18624. me.body.style.overflowY = bakOverflow || ''
  18625. me.removeListener('contentchange', adjustHeight)
  18626. me.removeListener('keyup', adjustHeight)
  18627. me.removeListener('mouseup', adjustHeight)
  18628. me.autoHeightEnabled = false
  18629. me.fireEvent('autoheightchanged', me.autoHeightEnabled)
  18630. }
  18631. me.on('setHeight', function() {
  18632. me.disableAutoHeight()
  18633. })
  18634. me.addListener('ready', function() {
  18635. me.enableAutoHeight()
  18636. //trace:1764
  18637. var timer
  18638. domUtils.on(
  18639. browser.ie ? me.body : me.document,
  18640. browser.webkit ? 'dragover' : 'drop',
  18641. function() {
  18642. clearTimeout(timer)
  18643. timer = setTimeout(function() {
  18644. //trace:3681
  18645. adjustHeight.call(me)
  18646. }, 100)
  18647. }
  18648. )
  18649. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  18650. var lastScrollY
  18651. window.onscroll = function() {
  18652. if (lastScrollY === null) {
  18653. lastScrollY = this.scrollY
  18654. } else if (this.scrollY == 0 && lastScrollY != 0) {
  18655. me.window.scrollTo(0, 0)
  18656. lastScrollY = null
  18657. }
  18658. }
  18659. })
  18660. }
  18661. // plugins/autofloat.js
  18662. ///import core
  18663. ///commands 悬浮工具栏
  18664. ///commandsName AutoFloat,autoFloatEnabled
  18665. ///commandsTitle 悬浮工具栏
  18666. /**
  18667. * modified by chengchao01
  18668. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  18669. */
  18670. UE.plugins['autofloat'] = function() {
  18671. var me = this,
  18672. lang = me.getLang()
  18673. me.setOpt({
  18674. topOffset: 0
  18675. })
  18676. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  18677. topOffset = me.options.topOffset
  18678. //如果不固定toolbar的位置,则直接退出
  18679. if (!optsAutoFloatEnabled) {
  18680. return
  18681. }
  18682. var uiUtils = UE.ui.uiUtils,
  18683. LteIE6 = browser.ie && browser.version <= 6,
  18684. quirks = browser.quirks
  18685. function checkHasUI() {
  18686. if (!UE.ui) {
  18687. alert(lang.autofloatMsg)
  18688. return 0
  18689. }
  18690. return 1
  18691. }
  18692. function fixIE6FixedPos() {
  18693. var docStyle = document.body.style
  18694. docStyle.backgroundImage = 'url("about:blank")'
  18695. docStyle.backgroundAttachment = 'fixed'
  18696. }
  18697. var bakCssText,
  18698. placeHolder = document.createElement('div'),
  18699. toolbarBox,
  18700. orgTop,
  18701. getPosition,
  18702. flag = true //ie7模式下需要偏移
  18703. function setFloating() {
  18704. var toobarBoxPos = domUtils.getXY(toolbarBox),
  18705. origalFloat = domUtils.getComputedStyle(toolbarBox, 'position'),
  18706. origalLeft = domUtils.getComputedStyle(toolbarBox, 'left')
  18707. toolbarBox.style.width = toolbarBox.offsetWidth + 'px'
  18708. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1
  18709. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox)
  18710. if (LteIE6 || (quirks && browser.ie)) {
  18711. if (toolbarBox.style.position != 'absolute') {
  18712. toolbarBox.style.position = 'absolute'
  18713. }
  18714. toolbarBox.style.top =
  18715. (document.body.scrollTop || document.documentElement.scrollTop) -
  18716. orgTop +
  18717. topOffset +
  18718. 'px'
  18719. } else {
  18720. if (browser.ie7Compat && flag) {
  18721. flag = false
  18722. toolbarBox.style.left =
  18723. domUtils.getXY(toolbarBox).x -
  18724. document.documentElement.getBoundingClientRect().left +
  18725. 2 +
  18726. 'px'
  18727. }
  18728. if (toolbarBox.style.position != 'fixed') {
  18729. toolbarBox.style.position = 'fixed'
  18730. toolbarBox.style.top = topOffset + 'px'
  18731. ;(origalFloat == 'absolute' || origalFloat == 'relative') &&
  18732. parseFloat(origalLeft) &&
  18733. (toolbarBox.style.left = toobarBoxPos.x + 'px')
  18734. }
  18735. }
  18736. }
  18737. function unsetFloating() {
  18738. flag = true
  18739. if (placeHolder.parentNode) {
  18740. placeHolder.parentNode.removeChild(placeHolder)
  18741. }
  18742. toolbarBox.style.cssText = bakCssText
  18743. }
  18744. function updateFloating() {
  18745. var rect3 = getPosition(me.container)
  18746. var offset = me.options.toolbarTopOffset || 0
  18747. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  18748. setFloating()
  18749. } else {
  18750. unsetFloating()
  18751. }
  18752. }
  18753. var defer_updateFloating = utils.defer(
  18754. function() {
  18755. updateFloating()
  18756. },
  18757. browser.ie ? 200 : 100,
  18758. true
  18759. )
  18760. me.addListener('destroy', function() {
  18761. domUtils.un(window, ['scroll', 'resize'], updateFloating)
  18762. me.removeListener('keydown', defer_updateFloating)
  18763. })
  18764. me.addListener('ready', function() {
  18765. if (checkHasUI(me)) {
  18766. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  18767. if (!me.ui) {
  18768. return
  18769. }
  18770. getPosition = uiUtils.getClientRect
  18771. toolbarBox = me.ui.getDom('toolbarbox')
  18772. orgTop = getPosition(toolbarBox).top
  18773. bakCssText = toolbarBox.style.cssText
  18774. placeHolder.style.height = toolbarBox.offsetHeight + 'px'
  18775. if (LteIE6) {
  18776. fixIE6FixedPos()
  18777. }
  18778. domUtils.on(window, ['scroll', 'resize'], updateFloating)
  18779. me.addListener('keydown', defer_updateFloating)
  18780. me.addListener('beforefullscreenchange', function(t, enabled) {
  18781. if (enabled) {
  18782. unsetFloating()
  18783. }
  18784. })
  18785. me.addListener('fullscreenchanged', function(t, enabled) {
  18786. if (!enabled) {
  18787. updateFloating()
  18788. }
  18789. })
  18790. me.addListener('sourcemodechanged', function(t, enabled) {
  18791. setTimeout(function() {
  18792. updateFloating()
  18793. }, 0)
  18794. })
  18795. me.addListener('clearDoc', function() {
  18796. setTimeout(function() {
  18797. updateFloating()
  18798. }, 0)
  18799. })
  18800. }
  18801. })
  18802. }
  18803. // plugins/video.js
  18804. /**
  18805. * video插件, 为UEditor提供视频插入支持
  18806. * @file
  18807. * @since 1.2.6.1
  18808. */
  18809. UE.plugins['video'] = function() {
  18810. var me = this
  18811. /**
  18812. * 创建插入视频字符窜
  18813. * @param url 视频地址
  18814. * @param width 视频宽度
  18815. * @param height 视频高度
  18816. * @param align 视频对齐
  18817. * @param toEmbed 是否以flash代替显示
  18818. * @param addParagraph 是否需要添加P 标签
  18819. */
  18820. function creatInsertStr(url, width, height, id, align, classname, type) {
  18821. url = utils.unhtmlForUrl(url)
  18822. align = utils.unhtml(align)
  18823. classname = utils.unhtml(classname)
  18824. width = parseInt(width, 10) || 0
  18825. height = parseInt(height, 10) || 0
  18826. var str
  18827. switch (type) {
  18828. case 'image':
  18829. str =
  18830. '<img ' +
  18831. (id ? 'id="' + id + '"' : '') +
  18832. ' width="' +
  18833. width +
  18834. '" height="' +
  18835. height +
  18836. '" _url="' +
  18837. url +
  18838. '" class="' +
  18839. classname.replace(/\bvideo-js\b/, '') +
  18840. '"' +
  18841. ' src="' +
  18842. me.options.UEDITOR_HOME_URL +
  18843. 'themes/default/images/spacer.gif" style="background:url(' +
  18844. me.options.UEDITOR_HOME_URL +
  18845. 'themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;' +
  18846. (align ? 'float:' + align + ';' : '') +
  18847. '" />'
  18848. break
  18849. case 'embed':
  18850. str =
  18851. '<embed type="application/x-shockwave-flash" class="' +
  18852. classname +
  18853. '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  18854. ' src="' +
  18855. utils.html(url) +
  18856. '" width="' +
  18857. width +
  18858. '" height="' +
  18859. height +
  18860. '"' +
  18861. (align ? ' style="float:' + align + '"' : '') +
  18862. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >'
  18863. break
  18864. case 'video':
  18865. var ext = url.substr(url.lastIndexOf('.') + 1)
  18866. if (ext == 'ogv') ext = 'ogg'
  18867. str =
  18868. '<video' +
  18869. (id ? ' id="' + id + '"' : '') +
  18870. ' class="' +
  18871. classname +
  18872. ' video-js" ' +
  18873. (align ? ' style="float:' + align + '"' : '') +
  18874. ' controls preload="none" width="' +
  18875. width +
  18876. '" height="' +
  18877. height +
  18878. '" src="' +
  18879. url +
  18880. '" data-setup="{}">' +
  18881. '<source src="' +
  18882. url +
  18883. '" type="video/' +
  18884. ext +
  18885. '" /></video>'
  18886. break
  18887. }
  18888. return str
  18889. }
  18890. function switchImgAndVideo(root, img2video) {
  18891. utils.each(
  18892. root.getNodesByTagName(img2video ? 'img' : 'embed video'),
  18893. function(node) {
  18894. var className = node.getAttr('class')
  18895. if (className && className.indexOf('edui-faked-video') != -1) {
  18896. var html = creatInsertStr(
  18897. img2video ? node.getAttr('_url') : node.getAttr('src'),
  18898. node.getAttr('width'),
  18899. node.getAttr('height'),
  18900. null,
  18901. node.getStyle('float') || '',
  18902. className,
  18903. img2video ? 'embed' : 'image'
  18904. )
  18905. node.parentNode.replaceChild(UE.uNode.createElement(html), node)
  18906. }
  18907. if (className && className.indexOf('edui-upload-video') != -1) {
  18908. var html = creatInsertStr(
  18909. img2video ? node.getAttr('_url') : node.getAttr('src'),
  18910. node.getAttr('width'),
  18911. node.getAttr('height'),
  18912. null,
  18913. node.getStyle('float') || '',
  18914. className,
  18915. img2video ? 'video' : 'image'
  18916. )
  18917. node.parentNode.replaceChild(UE.uNode.createElement(html), node)
  18918. }
  18919. }
  18920. )
  18921. }
  18922. me.addOutputRule(function(root) {
  18923. switchImgAndVideo(root, true)
  18924. })
  18925. me.addInputRule(function(root) {
  18926. switchImgAndVideo(root)
  18927. })
  18928. /**
  18929. * 插入视频
  18930. * @command insertvideo
  18931. * @method execCommand
  18932. * @param { String } cmd 命令字符串
  18933. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  18934. * @example
  18935. * ```javascript
  18936. *
  18937. * var videoAttr = {
  18938. * //视频地址
  18939. * url: 'http://www.youku.com/xxx',
  18940. * //视频宽高值, 单位px
  18941. * width: 200,
  18942. * height: 100
  18943. * };
  18944. *
  18945. * //editor 是编辑器实例
  18946. * //向编辑器插入单个视频
  18947. * editor.execCommand( 'insertvideo', videoAttr );
  18948. * ```
  18949. */
  18950. /**
  18951. * 插入视频
  18952. * @command insertvideo
  18953. * @method execCommand
  18954. * @param { String } cmd 命令字符串
  18955. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  18956. * @example
  18957. * ```javascript
  18958. *
  18959. * var videoAttr1 = {
  18960. * //视频地址
  18961. * url: 'http://www.youku.com/xxx',
  18962. * //视频宽高值, 单位px
  18963. * width: 200,
  18964. * height: 100
  18965. * },
  18966. * videoAttr2 = {
  18967. * //视频地址
  18968. * url: 'http://www.youku.com/xxx',
  18969. * //视频宽高值, 单位px
  18970. * width: 200,
  18971. * height: 100
  18972. * }
  18973. *
  18974. * //editor 是编辑器实例
  18975. * //该方法将会向编辑器内插入两个视频
  18976. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  18977. * ```
  18978. */
  18979. /**
  18980. * 查询当前光标所在处是否是一个视频
  18981. * @command insertvideo
  18982. * @method queryCommandState
  18983. * @param { String } cmd 需要查询的命令字符串
  18984. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  18985. * @example
  18986. * ```javascript
  18987. *
  18988. * //editor 是编辑器实例
  18989. * editor.queryCommandState( 'insertvideo' );
  18990. * ```
  18991. */
  18992. me.commands['insertvideo'] = {
  18993. execCommand: function(cmd, videoObjs, type) {
  18994. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs]
  18995. var html = [],
  18996. id = 'tmpVedio',
  18997. cl
  18998. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  18999. vi = videoObjs[i]
  19000. cl =
  19001. type == 'upload' ? 'video-js vjs-default-skin' : 'edui-faked-video'
  19002. html.push(
  19003. creatInsertStr(
  19004. vi.url,
  19005. vi.width || 420,
  19006. vi.height || 280,
  19007. id + i,
  19008. null,
  19009. cl,
  19010. 'video'
  19011. )
  19012. )
  19013. }
  19014. me.execCommand('inserthtml', html.join(''), true)
  19015. var rng = this.selection.getRange()
  19016. for (var i = 0, len = videoObjs.length; i < len; i++) {
  19017. var img = this.document.getElementById('tmpVedio' + i)
  19018. domUtils.removeAttributes(img, 'id')
  19019. rng.selectNode(img).select()
  19020. me.execCommand('imagefloat', videoObjs[i].align)
  19021. }
  19022. },
  19023. queryCommandState: function() {
  19024. var img = me.selection.getRange().getClosedNode(),
  19025. flag =
  19026. img &&
  19027. (img.className == 'edui-faked-video' ||
  19028. img.className.indexOf('edui-upload-video') != -1)
  19029. return flag ? 1 : 0
  19030. }
  19031. }
  19032. }
  19033. // plugins/table.core.js
  19034. /**
  19035. * Created with JetBrains WebStorm.
  19036. * User: taoqili
  19037. * Date: 13-1-18
  19038. * Time: 上午11:09
  19039. * To change this template use File | Settings | File Templates.
  19040. */
  19041. /**
  19042. * UE表格操作类
  19043. * @param table
  19044. * @constructor
  19045. */
  19046. ;(function() {
  19047. var UETable = (UE.UETable = function(table) {
  19048. this.table = table
  19049. this.indexTable = []
  19050. this.selectedTds = []
  19051. this.cellsRange = {}
  19052. this.update(table)
  19053. })
  19054. //===以下为静态工具方法===
  19055. UETable.removeSelectedClass = function(cells) {
  19056. utils.each(cells, function(cell) {
  19057. domUtils.removeClasses(cell, 'selectTdClass')
  19058. })
  19059. }
  19060. UETable.addSelectedClass = function(cells) {
  19061. utils.each(cells, function(cell) {
  19062. domUtils.addClass(cell, 'selectTdClass')
  19063. })
  19064. }
  19065. UETable.isEmptyBlock = function(node) {
  19066. var reg = new RegExp(domUtils.fillChar, 'g')
  19067. if (
  19068. node[browser.ie ? 'innerText' : 'textContent']
  19069. .replace(/^\s*$/, '')
  19070. .replace(reg, '').length > 0
  19071. ) {
  19072. return 0
  19073. }
  19074. for (var i in dtd.$isNotEmpty)
  19075. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  19076. if (node.getElementsByTagName(i).length) {
  19077. return 0
  19078. }
  19079. }
  19080. return 1
  19081. }
  19082. UETable.getWidth = function(cell) {
  19083. if (!cell) return 0
  19084. return parseInt(domUtils.getComputedStyle(cell, 'width'), 10)
  19085. }
  19086. /**
  19087. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  19088. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  19089. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  19090. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  19091. */
  19092. UETable.getTableCellAlignState = function(cells) {
  19093. !utils.isArray(cells) && (cells = [cells])
  19094. var result = {},
  19095. status = ['align', 'valign'],
  19096. tempStatus = null,
  19097. isSame = true //状态是否相同
  19098. utils.each(cells, function(cellNode) {
  19099. utils.each(status, function(currentState) {
  19100. tempStatus = cellNode.getAttribute(currentState)
  19101. if (!result[currentState] && tempStatus) {
  19102. result[currentState] = tempStatus
  19103. } else if (
  19104. !result[currentState] ||
  19105. tempStatus !== result[currentState]
  19106. ) {
  19107. isSame = false
  19108. return false
  19109. }
  19110. })
  19111. return isSame
  19112. })
  19113. return isSame ? result : null
  19114. }
  19115. /**
  19116. * 根据当前选区获取相关的table信息
  19117. * @return {Object}
  19118. */
  19119. UETable.getTableItemsByRange = function(editor) {
  19120. var start = editor.selection.getStart()
  19121. //ff下会选中bookmark
  19122. if (
  19123. start &&
  19124. start.id &&
  19125. start.id.indexOf('_baidu_bookmark_start_') === 0 &&
  19126. start.nextSibling
  19127. ) {
  19128. start = start.nextSibling
  19129. }
  19130. //在table或者td边缘有可能存在选中tr的情况
  19131. var cell =
  19132. start && domUtils.findParentByTagName(start, ['td', 'th'], true),
  19133. tr = cell && cell.parentNode,
  19134. caption = start && domUtils.findParentByTagName(start, 'caption', true),
  19135. table = caption ? caption.parentNode : tr && tr.parentNode.parentNode
  19136. return {
  19137. cell: cell,
  19138. tr: tr,
  19139. table: table,
  19140. caption: caption
  19141. }
  19142. }
  19143. UETable.getUETableBySelected = function(editor) {
  19144. var table = UETable.getTableItemsByRange(editor).table
  19145. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  19146. return table.ueTable
  19147. }
  19148. return null
  19149. }
  19150. UETable.getDefaultValue = function(editor, table) {
  19151. var borderMap = {
  19152. thin: '0px',
  19153. medium: '1px',
  19154. thick: '2px'
  19155. },
  19156. tableBorder,
  19157. tdPadding,
  19158. tdBorder,
  19159. tmpValue
  19160. if (!table) {
  19161. table = editor.document.createElement('table')
  19162. table.insertRow(0).insertCell(0).innerHTML = 'xxx'
  19163. editor.body.appendChild(table)
  19164. var td = table.getElementsByTagName('td')[0]
  19165. tmpValue = domUtils.getComputedStyle(table, 'border-left-width')
  19166. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19167. tmpValue = domUtils.getComputedStyle(td, 'padding-left')
  19168. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19169. tmpValue = domUtils.getComputedStyle(td, 'border-left-width')
  19170. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19171. domUtils.remove(table)
  19172. return {
  19173. tableBorder: tableBorder,
  19174. tdPadding: tdPadding,
  19175. tdBorder: tdBorder
  19176. }
  19177. } else {
  19178. td = table.getElementsByTagName('td')[0]
  19179. tmpValue = domUtils.getComputedStyle(table, 'border-left-width')
  19180. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19181. tmpValue = domUtils.getComputedStyle(td, 'padding-left')
  19182. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19183. tmpValue = domUtils.getComputedStyle(td, 'border-left-width')
  19184. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19185. return {
  19186. tableBorder: tableBorder,
  19187. tdPadding: tdPadding,
  19188. tdBorder: tdBorder
  19189. }
  19190. }
  19191. }
  19192. /**
  19193. * 根据当前点击的td或者table获取索引对象
  19194. * @param tdOrTable
  19195. */
  19196. UETable.getUETable = function(tdOrTable) {
  19197. var tag = tdOrTable.tagName.toLowerCase()
  19198. tdOrTable =
  19199. tag == 'td' || tag == 'th' || tag == 'caption'
  19200. ? domUtils.findParentByTagName(tdOrTable, 'table', true)
  19201. : tdOrTable
  19202. if (!tdOrTable.ueTable) {
  19203. tdOrTable.ueTable = new UETable(tdOrTable)
  19204. }
  19205. return tdOrTable.ueTable
  19206. }
  19207. UETable.cloneCell = function(cell, ignoreMerge, keepPro) {
  19208. if (!cell || utils.isString(cell)) {
  19209. return this.table.ownerDocument.createElement(cell || 'td')
  19210. }
  19211. var flag = domUtils.hasClass(cell, 'selectTdClass')
  19212. flag && domUtils.removeClasses(cell, 'selectTdClass')
  19213. var tmpCell = cell.cloneNode(true)
  19214. if (ignoreMerge) {
  19215. tmpCell.rowSpan = tmpCell.colSpan = 1
  19216. }
  19217. //去掉宽高
  19218. !keepPro && domUtils.removeAttributes(tmpCell, 'width height')
  19219. !keepPro && domUtils.removeAttributes(tmpCell, 'style')
  19220. tmpCell.style.borderLeftStyle = ''
  19221. tmpCell.style.borderTopStyle = ''
  19222. tmpCell.style.borderLeftColor = cell.style.borderRightColor
  19223. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth
  19224. tmpCell.style.borderTopColor = cell.style.borderBottomColor
  19225. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth
  19226. flag && domUtils.addClass(cell, 'selectTdClass')
  19227. return tmpCell
  19228. }
  19229. UETable.prototype = {
  19230. getMaxRows: function() {
  19231. var rows = this.table.rows,
  19232. maxLen = 1
  19233. for (var i = 0, row; (row = rows[i]); i++) {
  19234. var currentMax = 1
  19235. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19236. currentMax = Math.max(cj.rowSpan || 1, currentMax)
  19237. }
  19238. maxLen = Math.max(currentMax + i, maxLen)
  19239. }
  19240. return maxLen
  19241. },
  19242. /**
  19243. * 获取当前表格的最大列数
  19244. */
  19245. getMaxCols: function() {
  19246. var rows = this.table.rows,
  19247. maxLen = 0,
  19248. cellRows = {}
  19249. for (var i = 0, row; (row = rows[i]); i++) {
  19250. var cellsNum = 0
  19251. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19252. cellsNum += cj.colSpan || 1
  19253. if (cj.rowSpan && cj.rowSpan > 1) {
  19254. for (var k = 1; k < cj.rowSpan; k++) {
  19255. if (!cellRows['row_' + (i + k)]) {
  19256. cellRows['row_' + (i + k)] = cj.colSpan || 1
  19257. } else {
  19258. cellRows['row_' + (i + k)]++
  19259. }
  19260. }
  19261. }
  19262. }
  19263. cellsNum += cellRows['row_' + i] || 0
  19264. maxLen = Math.max(cellsNum, maxLen)
  19265. }
  19266. return maxLen
  19267. },
  19268. getCellColIndex: function(cell) {},
  19269. /**
  19270. * 获取当前cell旁边的单元格,
  19271. * @param cell
  19272. * @param right
  19273. */
  19274. getHSideCell: function(cell, right) {
  19275. try {
  19276. var cellInfo = this.getCellInfo(cell),
  19277. previewRowIndex,
  19278. previewColIndex
  19279. var len = this.selectedTds.length,
  19280. range = this.cellsRange
  19281. //首行或者首列没有前置单元格
  19282. if (
  19283. (!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19284. (right &&
  19285. (!len
  19286. ? cellInfo.colIndex == this.colsNum - 1
  19287. : range.endColIndex == this.colsNum - 1))
  19288. )
  19289. return null
  19290. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex
  19291. previewColIndex = !right
  19292. ? !len
  19293. ? cellInfo.colIndex < 1
  19294. ? 0
  19295. : cellInfo.colIndex - 1
  19296. : range.beginColIndex - 1
  19297. : !len
  19298. ? cellInfo.colIndex + 1
  19299. : range.endColIndex + 1
  19300. return this.getCell(
  19301. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19302. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19303. )
  19304. } catch (e) {
  19305. showError(e)
  19306. }
  19307. },
  19308. getTabNextCell: function(cell, preRowIndex) {
  19309. var cellInfo = this.getCellInfo(cell),
  19310. rowIndex = preRowIndex || cellInfo.rowIndex,
  19311. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  19312. nextCell
  19313. try {
  19314. nextCell = this.getCell(
  19315. this.indexTable[rowIndex][colIndex].rowIndex,
  19316. this.indexTable[rowIndex][colIndex].cellIndex
  19317. )
  19318. } catch (e) {
  19319. try {
  19320. rowIndex = rowIndex * 1 + 1
  19321. colIndex = 0
  19322. nextCell = this.getCell(
  19323. this.indexTable[rowIndex][colIndex].rowIndex,
  19324. this.indexTable[rowIndex][colIndex].cellIndex
  19325. )
  19326. } catch (e) {}
  19327. }
  19328. return nextCell
  19329. },
  19330. /**
  19331. * 获取视觉上的后置单元格
  19332. * @param cell
  19333. * @param bottom
  19334. */
  19335. getVSideCell: function(cell, bottom, ignoreRange) {
  19336. try {
  19337. var cellInfo = this.getCellInfo(cell),
  19338. nextRowIndex,
  19339. nextColIndex
  19340. var len = this.selectedTds.length && !ignoreRange,
  19341. range = this.cellsRange
  19342. //末行或者末列没有后置单元格
  19343. if (
  19344. (!bottom && cellInfo.rowIndex == 0) ||
  19345. (bottom &&
  19346. (!len
  19347. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19348. : range.endRowIndex == this.rowsNum - 1))
  19349. )
  19350. return null
  19351. nextRowIndex = !bottom
  19352. ? !len
  19353. ? cellInfo.rowIndex - 1
  19354. : range.beginRowIndex - 1
  19355. : !len
  19356. ? cellInfo.rowIndex + cellInfo.rowSpan
  19357. : range.endRowIndex + 1
  19358. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex
  19359. return this.getCell(
  19360. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19361. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19362. )
  19363. } catch (e) {
  19364. showError(e)
  19365. }
  19366. },
  19367. /**
  19368. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  19369. */
  19370. getSameEndPosCells: function(cell, xOrY) {
  19371. try {
  19372. var flag = xOrY.toLowerCase() === 'x',
  19373. end =
  19374. domUtils.getXY(cell)[flag ? 'x' : 'y'] +
  19375. cell['offset' + (flag ? 'Width' : 'Height')],
  19376. rows = this.table.rows,
  19377. cells = null,
  19378. returns = []
  19379. for (var i = 0; i < this.rowsNum; i++) {
  19380. cells = rows[i].cells
  19381. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19382. var tmpEnd =
  19383. domUtils.getXY(tmpCell)[flag ? 'x' : 'y'] +
  19384. tmpCell['offset' + (flag ? 'Width' : 'Height')]
  19385. //对应行的td已经被上面行rowSpan了
  19386. if (tmpEnd > end && flag) break
  19387. if (cell == tmpCell || end == tmpEnd) {
  19388. //只获取单一的单元格
  19389. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  19390. if (tmpCell[flag ? 'colSpan' : 'rowSpan'] == 1) {
  19391. returns.push(tmpCell)
  19392. }
  19393. if (flag) break
  19394. }
  19395. }
  19396. }
  19397. return returns
  19398. } catch (e) {
  19399. showError(e)
  19400. }
  19401. },
  19402. setCellContent: function(cell, content) {
  19403. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : '<br />')
  19404. },
  19405. cloneCell: UETable.cloneCell,
  19406. /**
  19407. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  19408. */
  19409. getSameStartPosXCells: function(cell) {
  19410. try {
  19411. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  19412. rows = this.table.rows,
  19413. cells,
  19414. returns = []
  19415. for (var i = 0; i < this.rowsNum; i++) {
  19416. cells = rows[i].cells
  19417. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19418. var tmpStart = domUtils.getXY(tmpCell).x
  19419. if (tmpStart > start) break
  19420. if (tmpStart == start && tmpCell.colSpan == 1) {
  19421. returns.push(tmpCell)
  19422. break
  19423. }
  19424. }
  19425. }
  19426. return returns
  19427. } catch (e) {
  19428. showError(e)
  19429. }
  19430. },
  19431. /**
  19432. * 更新table对应的索引表
  19433. */
  19434. update: function(table) {
  19435. this.table = table || this.table
  19436. this.selectedTds = []
  19437. this.cellsRange = {}
  19438. this.indexTable = []
  19439. var rows = this.table.rows,
  19440. rowsNum = this.getMaxRows(),
  19441. dNum = rowsNum - rows.length,
  19442. colsNum = this.getMaxCols()
  19443. while (dNum--) {
  19444. this.table.insertRow(rows.length)
  19445. }
  19446. this.rowsNum = rowsNum
  19447. this.colsNum = colsNum
  19448. for (var i = 0, len = rows.length; i < len; i++) {
  19449. this.indexTable[i] = new Array(colsNum)
  19450. }
  19451. //填充索引表
  19452. for (var rowIndex = 0, row; (row = rows[rowIndex]); rowIndex++) {
  19453. for (
  19454. var cellIndex = 0, cell, cells = row.cells;
  19455. (cell = cells[cellIndex]);
  19456. cellIndex++
  19457. ) {
  19458. //修正整行被rowSpan时导致的行数计算错误
  19459. if (cell.rowSpan > rowsNum) {
  19460. cell.rowSpan = rowsNum
  19461. }
  19462. var colIndex = cellIndex,
  19463. rowSpan = cell.rowSpan || 1,
  19464. colSpan = cell.colSpan || 1
  19465. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  19466. while (this.indexTable[rowIndex][colIndex]) colIndex++
  19467. for (var j = 0; j < rowSpan; j++) {
  19468. for (var k = 0; k < colSpan; k++) {
  19469. this.indexTable[rowIndex + j][colIndex + k] = {
  19470. rowIndex: rowIndex,
  19471. cellIndex: cellIndex,
  19472. colIndex: colIndex,
  19473. rowSpan: rowSpan,
  19474. colSpan: colSpan
  19475. }
  19476. }
  19477. }
  19478. }
  19479. }
  19480. //修复残缺td
  19481. for (j = 0; j < rowsNum; j++) {
  19482. for (k = 0; k < colsNum; k++) {
  19483. if (this.indexTable[j][k] === undefined) {
  19484. row = rows[j]
  19485. cell = row.cells[row.cells.length - 1]
  19486. cell = cell
  19487. ? cell.cloneNode(true)
  19488. : this.table.ownerDocument.createElement('td')
  19489. this.setCellContent(cell)
  19490. if (cell.colSpan !== 1) cell.colSpan = 1
  19491. if (cell.rowSpan !== 1) cell.rowSpan = 1
  19492. row.appendChild(cell)
  19493. this.indexTable[j][k] = {
  19494. rowIndex: j,
  19495. cellIndex: cell.cellIndex,
  19496. colIndex: k,
  19497. rowSpan: 1,
  19498. colSpan: 1
  19499. }
  19500. }
  19501. }
  19502. }
  19503. //当框选后删除行或者列后撤销,需要重建选区。
  19504. var tds = domUtils.getElementsByTagName(this.table, 'td'),
  19505. selectTds = []
  19506. utils.each(tds, function(td) {
  19507. if (domUtils.hasClass(td, 'selectTdClass')) {
  19508. selectTds.push(td)
  19509. }
  19510. })
  19511. if (selectTds.length) {
  19512. var start = selectTds[0],
  19513. end = selectTds[selectTds.length - 1],
  19514. startInfo = this.getCellInfo(start),
  19515. endInfo = this.getCellInfo(end)
  19516. this.selectedTds = selectTds
  19517. this.cellsRange = {
  19518. beginRowIndex: startInfo.rowIndex,
  19519. beginColIndex: startInfo.colIndex,
  19520. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  19521. endColIndex: endInfo.colIndex + endInfo.colSpan - 1
  19522. }
  19523. }
  19524. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  19525. if (!domUtils.hasClass(this.table.rows[0], 'firstRow')) {
  19526. domUtils.addClass(this.table.rows[0], 'firstRow')
  19527. for (var i = 1; i < this.table.rows.length; i++) {
  19528. domUtils.removeClasses(this.table.rows[i], 'firstRow')
  19529. }
  19530. }
  19531. },
  19532. /**
  19533. * 获取单元格的索引信息
  19534. */
  19535. getCellInfo: function(cell) {
  19536. if (!cell) return
  19537. var cellIndex = cell.cellIndex,
  19538. rowIndex = cell.parentNode.rowIndex,
  19539. rowInfo = this.indexTable[rowIndex],
  19540. numCols = this.colsNum
  19541. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  19542. var cellInfo = rowInfo[colIndex]
  19543. if (
  19544. cellInfo.rowIndex === rowIndex &&
  19545. cellInfo.cellIndex === cellIndex
  19546. ) {
  19547. return cellInfo
  19548. }
  19549. }
  19550. },
  19551. /**
  19552. * 根据行列号获取单元格
  19553. */
  19554. getCell: function(rowIndex, cellIndex) {
  19555. return (
  19556. (rowIndex < this.rowsNum &&
  19557. this.table.rows[rowIndex].cells[cellIndex]) ||
  19558. null
  19559. )
  19560. },
  19561. /**
  19562. * 删除单元格
  19563. */
  19564. deleteCell: function(cell, rowIndex) {
  19565. rowIndex =
  19566. typeof rowIndex == 'number' ? rowIndex : cell.parentNode.rowIndex
  19567. var row = this.table.rows[rowIndex]
  19568. row.deleteCell(cell.cellIndex)
  19569. },
  19570. /**
  19571. * 根据始末两个单元格获取被框选的所有单元格范围
  19572. */
  19573. getCellsRange: function(cellA, cellB) {
  19574. function checkRange(
  19575. beginRowIndex,
  19576. beginColIndex,
  19577. endRowIndex,
  19578. endColIndex
  19579. ) {
  19580. var tmpBeginRowIndex = beginRowIndex,
  19581. tmpBeginColIndex = beginColIndex,
  19582. tmpEndRowIndex = endRowIndex,
  19583. tmpEndColIndex = endColIndex,
  19584. cellInfo,
  19585. colIndex,
  19586. rowIndex
  19587. // 通过indexTable检查是否存在超出TableRange上边界的情况
  19588. if (beginRowIndex > 0) {
  19589. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19590. cellInfo = me.indexTable[beginRowIndex][colIndex]
  19591. rowIndex = cellInfo.rowIndex
  19592. if (rowIndex < beginRowIndex) {
  19593. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex)
  19594. }
  19595. }
  19596. }
  19597. // 通过indexTable检查是否存在超出TableRange右边界的情况
  19598. if (endColIndex < me.colsNum) {
  19599. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19600. cellInfo = me.indexTable[rowIndex][endColIndex]
  19601. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1
  19602. if (colIndex > endColIndex) {
  19603. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex)
  19604. }
  19605. }
  19606. }
  19607. // 检查是否有超出TableRange下边界的情况
  19608. if (endRowIndex < me.rowsNum) {
  19609. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19610. cellInfo = me.indexTable[endRowIndex][colIndex]
  19611. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1
  19612. if (rowIndex > endRowIndex) {
  19613. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex)
  19614. }
  19615. }
  19616. }
  19617. // 检查是否有超出TableRange左边界的情况
  19618. if (beginColIndex > 0) {
  19619. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19620. cellInfo = me.indexTable[rowIndex][beginColIndex]
  19621. colIndex = cellInfo.colIndex
  19622. if (colIndex < beginColIndex) {
  19623. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex)
  19624. }
  19625. }
  19626. }
  19627. //递归调用直至所有完成所有框选单元格的扩展
  19628. if (
  19629. tmpBeginRowIndex != beginRowIndex ||
  19630. tmpBeginColIndex != beginColIndex ||
  19631. tmpEndRowIndex != endRowIndex ||
  19632. tmpEndColIndex != endColIndex
  19633. ) {
  19634. return checkRange(
  19635. tmpBeginRowIndex,
  19636. tmpBeginColIndex,
  19637. tmpEndRowIndex,
  19638. tmpEndColIndex
  19639. )
  19640. } else {
  19641. // 不需要扩展TableRange的情况
  19642. return {
  19643. beginRowIndex: beginRowIndex,
  19644. beginColIndex: beginColIndex,
  19645. endRowIndex: endRowIndex,
  19646. endColIndex: endColIndex
  19647. }
  19648. }
  19649. }
  19650. try {
  19651. var me = this,
  19652. cellAInfo = me.getCellInfo(cellA)
  19653. if (cellA === cellB) {
  19654. return {
  19655. beginRowIndex: cellAInfo.rowIndex,
  19656. beginColIndex: cellAInfo.colIndex,
  19657. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19658. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1
  19659. }
  19660. }
  19661. var cellBInfo = me.getCellInfo(cellB)
  19662. // 计算TableRange的四个边
  19663. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  19664. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  19665. endRowIndex = Math.max(
  19666. cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19667. cellBInfo.rowIndex + cellBInfo.rowSpan - 1
  19668. ),
  19669. endColIndex = Math.max(
  19670. cellAInfo.colIndex + cellAInfo.colSpan - 1,
  19671. cellBInfo.colIndex + cellBInfo.colSpan - 1
  19672. )
  19673. return checkRange(
  19674. beginRowIndex,
  19675. beginColIndex,
  19676. endRowIndex,
  19677. endColIndex
  19678. )
  19679. } catch (e) {
  19680. //throw e;
  19681. }
  19682. },
  19683. /**
  19684. * 依据cellsRange获取对应的单元格集合
  19685. */
  19686. getCells: function(range) {
  19687. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  19688. this.clearSelected()
  19689. var beginRowIndex = range.beginRowIndex,
  19690. beginColIndex = range.beginColIndex,
  19691. endRowIndex = range.endRowIndex,
  19692. endColIndex = range.endColIndex,
  19693. cellInfo,
  19694. rowIndex,
  19695. colIndex,
  19696. tdHash = {},
  19697. returnTds = []
  19698. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  19699. for (var j = beginColIndex; j <= endColIndex; j++) {
  19700. cellInfo = this.indexTable[i][j]
  19701. rowIndex = cellInfo.rowIndex
  19702. colIndex = cellInfo.colIndex
  19703. // 如果Cells里已经包含了此Cell则跳过
  19704. var key = rowIndex + '|' + colIndex
  19705. if (tdHash[key]) continue
  19706. tdHash[key] = 1
  19707. if (
  19708. rowIndex < i ||
  19709. colIndex < j ||
  19710. rowIndex + cellInfo.rowSpan - 1 > endRowIndex ||
  19711. colIndex + cellInfo.colSpan - 1 > endColIndex
  19712. ) {
  19713. return null
  19714. }
  19715. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex))
  19716. }
  19717. }
  19718. return returnTds
  19719. },
  19720. /**
  19721. * 清理已经选中的单元格
  19722. */
  19723. clearSelected: function() {
  19724. UETable.removeSelectedClass(this.selectedTds)
  19725. this.selectedTds = []
  19726. this.cellsRange = {}
  19727. },
  19728. /**
  19729. * 根据range设置已经选中的单元格
  19730. */
  19731. setSelected: function(range) {
  19732. var cells = this.getCells(range)
  19733. UETable.addSelectedClass(cells)
  19734. this.selectedTds = cells
  19735. this.cellsRange = range
  19736. },
  19737. isFullRow: function() {
  19738. var range = this.cellsRange
  19739. return range.endColIndex - range.beginColIndex + 1 == this.colsNum
  19740. },
  19741. isFullCol: function() {
  19742. var range = this.cellsRange,
  19743. table = this.table,
  19744. ths = table.getElementsByTagName('th'),
  19745. rows = range.endRowIndex - range.beginRowIndex + 1
  19746. return !ths.length
  19747. ? rows == this.rowsNum
  19748. : rows == this.rowsNum || rows == this.rowsNum - 1
  19749. },
  19750. /**
  19751. * 获取视觉上的前置单元格,默认是左边,top传入时
  19752. * @param cell
  19753. * @param top
  19754. */
  19755. getNextCell: function(cell, bottom, ignoreRange) {
  19756. try {
  19757. var cellInfo = this.getCellInfo(cell),
  19758. nextRowIndex,
  19759. nextColIndex
  19760. var len = this.selectedTds.length && !ignoreRange,
  19761. range = this.cellsRange
  19762. //末行或者末列没有后置单元格
  19763. if (
  19764. (!bottom && cellInfo.rowIndex == 0) ||
  19765. (bottom &&
  19766. (!len
  19767. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19768. : range.endRowIndex == this.rowsNum - 1))
  19769. )
  19770. return null
  19771. nextRowIndex = !bottom
  19772. ? !len
  19773. ? cellInfo.rowIndex - 1
  19774. : range.beginRowIndex - 1
  19775. : !len
  19776. ? cellInfo.rowIndex + cellInfo.rowSpan
  19777. : range.endRowIndex + 1
  19778. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex
  19779. return this.getCell(
  19780. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19781. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19782. )
  19783. } catch (e) {
  19784. showError(e)
  19785. }
  19786. },
  19787. getPreviewCell: function(cell, top) {
  19788. try {
  19789. var cellInfo = this.getCellInfo(cell),
  19790. previewRowIndex,
  19791. previewColIndex
  19792. var len = this.selectedTds.length,
  19793. range = this.cellsRange
  19794. //首行或者首列没有前置单元格
  19795. if (
  19796. (!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19797. (top &&
  19798. (!len
  19799. ? cellInfo.rowIndex > this.colsNum - 1
  19800. : range.endColIndex == this.colsNum - 1))
  19801. )
  19802. return null
  19803. previewRowIndex = !top
  19804. ? !len
  19805. ? cellInfo.rowIndex
  19806. : range.beginRowIndex
  19807. : !len
  19808. ? cellInfo.rowIndex < 1
  19809. ? 0
  19810. : cellInfo.rowIndex - 1
  19811. : range.beginRowIndex
  19812. previewColIndex = !top
  19813. ? !len
  19814. ? cellInfo.colIndex < 1
  19815. ? 0
  19816. : cellInfo.colIndex - 1
  19817. : range.beginColIndex - 1
  19818. : !len
  19819. ? cellInfo.colIndex
  19820. : range.endColIndex + 1
  19821. return this.getCell(
  19822. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19823. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19824. )
  19825. } catch (e) {
  19826. showError(e)
  19827. }
  19828. },
  19829. /**
  19830. * 移动单元格中的内容
  19831. */
  19832. moveContent: function(cellTo, cellFrom) {
  19833. if (UETable.isEmptyBlock(cellFrom)) return
  19834. if (UETable.isEmptyBlock(cellTo)) {
  19835. cellTo.innerHTML = cellFrom.innerHTML
  19836. return
  19837. }
  19838. var child = cellTo.lastChild
  19839. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  19840. cellTo.appendChild(cellTo.ownerDocument.createElement('br'))
  19841. }
  19842. while ((child = cellFrom.firstChild)) {
  19843. cellTo.appendChild(child)
  19844. }
  19845. },
  19846. /**
  19847. * 向右合并单元格
  19848. */
  19849. mergeRight: function(cell) {
  19850. var cellInfo = this.getCellInfo(cell),
  19851. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  19852. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  19853. rightCell = this.getCell(
  19854. rightCellInfo.rowIndex,
  19855. rightCellInfo.cellIndex
  19856. )
  19857. //合并
  19858. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan
  19859. //被合并的单元格不应存在宽度属性
  19860. cell.removeAttribute('width')
  19861. //移动内容
  19862. this.moveContent(cell, rightCell)
  19863. //删掉被合并的Cell
  19864. this.deleteCell(rightCell, rightCellInfo.rowIndex)
  19865. this.update()
  19866. },
  19867. /**
  19868. * 向下合并单元格
  19869. */
  19870. mergeDown: function(cell) {
  19871. var cellInfo = this.getCellInfo(cell),
  19872. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  19873. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  19874. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex)
  19875. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan
  19876. cell.removeAttribute('height')
  19877. this.moveContent(cell, downCell)
  19878. this.deleteCell(downCell, downCellInfo.rowIndex)
  19879. this.update()
  19880. },
  19881. /**
  19882. * 合并整个range中的内容
  19883. */
  19884. mergeRange: function() {
  19885. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  19886. var range = this.cellsRange,
  19887. leftTopCell = this.getCell(
  19888. range.beginRowIndex,
  19889. this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex
  19890. )
  19891. if (
  19892. leftTopCell.tagName == 'TH' &&
  19893. range.endRowIndex !== range.beginRowIndex
  19894. ) {
  19895. var index = this.indexTable,
  19896. info = this.getCellInfo(leftTopCell)
  19897. leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex)
  19898. range = this.getCellsRange(
  19899. leftTopCell,
  19900. this.getCell(
  19901. index[this.rowsNum - 1][info.colIndex].rowIndex,
  19902. index[this.rowsNum - 1][info.colIndex].cellIndex
  19903. )
  19904. )
  19905. }
  19906. // 删除剩余的Cells
  19907. var cells = this.getCells(range)
  19908. for (var i = 0, ci; (ci = cells[i++]); ) {
  19909. if (ci !== leftTopCell) {
  19910. this.moveContent(leftTopCell, ci)
  19911. this.deleteCell(ci)
  19912. }
  19913. }
  19914. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  19915. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1
  19916. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute('height')
  19917. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1
  19918. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute('width')
  19919. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  19920. leftTopCell.colSpan = 1
  19921. }
  19922. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  19923. var rowIndex = leftTopCell.parentNode.rowIndex
  19924. //解决IE下的表格操作问题
  19925. if (this.table.deleteRow) {
  19926. for (
  19927. var i = rowIndex + 1,
  19928. curIndex = rowIndex + 1,
  19929. len = leftTopCell.rowSpan;
  19930. i < len;
  19931. i++
  19932. ) {
  19933. this.table.deleteRow(curIndex)
  19934. }
  19935. } else {
  19936. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  19937. var row = this.table.rows[rowIndex + 1]
  19938. row.parentNode.removeChild(row)
  19939. }
  19940. }
  19941. leftTopCell.rowSpan = 1
  19942. }
  19943. this.update()
  19944. },
  19945. /**
  19946. * 插入一行单元格
  19947. */
  19948. insertRow: function(rowIndex, sourceCell) {
  19949. var numCols = this.colsNum,
  19950. table = this.table,
  19951. row = table.insertRow(rowIndex),
  19952. cell,
  19953. isInsertTitle =
  19954. typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH'
  19955. function replaceTdToTh(colIndex, cell, tableRow) {
  19956. if (colIndex == 0) {
  19957. var tr = tableRow.nextSibling || tableRow.previousSibling,
  19958. th = tr.cells[colIndex]
  19959. if (th.tagName == 'TH') {
  19960. th = cell.ownerDocument.createElement('th')
  19961. th.appendChild(cell.firstChild)
  19962. tableRow.insertBefore(th, cell)
  19963. domUtils.remove(cell)
  19964. }
  19965. } else {
  19966. if (cell.tagName == 'TH') {
  19967. var td = cell.ownerDocument.createElement('td')
  19968. td.appendChild(cell.firstChild)
  19969. tableRow.insertBefore(td, cell)
  19970. domUtils.remove(cell)
  19971. }
  19972. }
  19973. }
  19974. //首行直接插入,无需考虑部分单元格被rowspan的情况
  19975. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  19976. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  19977. cell = this.cloneCell(sourceCell, true)
  19978. this.setCellContent(cell)
  19979. cell.getAttribute('vAlign') &&
  19980. cell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  19981. row.appendChild(cell)
  19982. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row)
  19983. }
  19984. } else {
  19985. var infoRow = this.indexTable[rowIndex],
  19986. cellIndex = 0
  19987. for (colIndex = 0; colIndex < numCols; colIndex++) {
  19988. var cellInfo = infoRow[colIndex]
  19989. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  19990. if (cellInfo.rowIndex < rowIndex) {
  19991. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  19992. cell.rowSpan = cellInfo.rowSpan + 1
  19993. } else {
  19994. cell = this.cloneCell(sourceCell, true)
  19995. this.setCellContent(cell)
  19996. row.appendChild(cell)
  19997. }
  19998. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row)
  19999. }
  20000. }
  20001. //框选时插入不触发contentchange,需要手动更新索引。
  20002. this.update()
  20003. return row
  20004. },
  20005. /**
  20006. * 删除一行单元格
  20007. * @param rowIndex
  20008. */
  20009. deleteRow: function(rowIndex) {
  20010. var row = this.table.rows[rowIndex],
  20011. infoRow = this.indexTable[rowIndex],
  20012. colsNum = this.colsNum,
  20013. count = 0 //处理计数
  20014. for (var colIndex = 0; colIndex < colsNum; ) {
  20015. var cellInfo = infoRow[colIndex],
  20016. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  20017. if (cell.rowSpan > 1) {
  20018. if (cellInfo.rowIndex == rowIndex) {
  20019. var clone = cell.cloneNode(true)
  20020. clone.rowSpan = cell.rowSpan - 1
  20021. clone.innerHTML = ''
  20022. cell.rowSpan = 1
  20023. var nextRowIndex = rowIndex + 1,
  20024. nextRow = this.table.rows[nextRowIndex],
  20025. insertCellIndex,
  20026. preMerged =
  20027. this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count
  20028. if (preMerged < colIndex) {
  20029. insertCellIndex = colIndex - preMerged - 1
  20030. //nextRow.insertCell(insertCellIndex);
  20031. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone)
  20032. } else {
  20033. if (nextRow.cells.length)
  20034. nextRow.insertBefore(clone, nextRow.cells[0])
  20035. }
  20036. count += 1
  20037. //cell.parentNode.removeChild(cell);
  20038. }
  20039. }
  20040. colIndex += cell.colSpan || 1
  20041. }
  20042. var deleteTds = [],
  20043. cacheMap = {}
  20044. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  20045. var tmpRowIndex = infoRow[colIndex].rowIndex,
  20046. tmpCellIndex = infoRow[colIndex].cellIndex,
  20047. key = tmpRowIndex + '_' + tmpCellIndex
  20048. if (cacheMap[key]) continue
  20049. cacheMap[key] = 1
  20050. cell = this.getCell(tmpRowIndex, tmpCellIndex)
  20051. deleteTds.push(cell)
  20052. }
  20053. var mergeTds = []
  20054. utils.each(deleteTds, function(td) {
  20055. if (td.rowSpan == 1) {
  20056. td.parentNode.removeChild(td)
  20057. } else {
  20058. mergeTds.push(td)
  20059. }
  20060. })
  20061. utils.each(mergeTds, function(td) {
  20062. td.rowSpan--
  20063. })
  20064. row.parentNode.removeChild(row)
  20065. //浏览器方法本身存在bug,采用自定义方法删除
  20066. //this.table.deleteRow(rowIndex);
  20067. this.update()
  20068. },
  20069. insertCol: function(colIndex, sourceCell, defaultValue) {
  20070. var rowsNum = this.rowsNum,
  20071. rowIndex = 0,
  20072. tableRow,
  20073. cell,
  20074. backWidth = parseInt(
  20075. (this.table.offsetWidth -
  20076. (this.colsNum + 1) * 20 -
  20077. (this.colsNum + 1)) /
  20078. (this.colsNum + 1),
  20079. 10
  20080. ),
  20081. isInsertTitleCol =
  20082. typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH'
  20083. function replaceTdToTh(rowIndex, cell, tableRow) {
  20084. if (rowIndex == 0) {
  20085. var th = cell.nextSibling || cell.previousSibling
  20086. if (th.tagName == 'TH') {
  20087. th = cell.ownerDocument.createElement('th')
  20088. th.appendChild(cell.firstChild)
  20089. tableRow.insertBefore(th, cell)
  20090. domUtils.remove(cell)
  20091. }
  20092. } else {
  20093. if (cell.tagName == 'TH') {
  20094. var td = cell.ownerDocument.createElement('td')
  20095. td.appendChild(cell.firstChild)
  20096. tableRow.insertBefore(td, cell)
  20097. domUtils.remove(cell)
  20098. }
  20099. }
  20100. }
  20101. var preCell
  20102. if (colIndex == 0 || colIndex == this.colsNum) {
  20103. for (; rowIndex < rowsNum; rowIndex++) {
  20104. tableRow = this.table.rows[rowIndex]
  20105. preCell =
  20106. tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length]
  20107. cell = this.cloneCell(sourceCell, true) //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  20108. this.setCellContent(cell)
  20109. cell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20110. preCell && cell.setAttribute('width', preCell.getAttribute('width'))
  20111. if (!colIndex) {
  20112. tableRow.insertBefore(cell, tableRow.cells[0])
  20113. } else {
  20114. domUtils.insertAfter(
  20115. tableRow.cells[tableRow.cells.length - 1],
  20116. cell
  20117. )
  20118. }
  20119. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  20120. }
  20121. } else {
  20122. for (; rowIndex < rowsNum; rowIndex++) {
  20123. var cellInfo = this.indexTable[rowIndex][colIndex]
  20124. if (cellInfo.colIndex < colIndex) {
  20125. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  20126. cell.colSpan = cellInfo.colSpan + 1
  20127. } else {
  20128. tableRow = this.table.rows[rowIndex]
  20129. preCell = tableRow.cells[cellInfo.cellIndex]
  20130. cell = this.cloneCell(sourceCell, true) //tableRow.insertCell(cellInfo.cellIndex);
  20131. this.setCellContent(cell)
  20132. cell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20133. preCell &&
  20134. cell.setAttribute('width', preCell.getAttribute('width'))
  20135. //防止IE下报错
  20136. preCell
  20137. ? tableRow.insertBefore(cell, preCell)
  20138. : tableRow.appendChild(cell)
  20139. }
  20140. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  20141. }
  20142. }
  20143. //框选时插入不触发contentchange,需要手动更新索引
  20144. this.update()
  20145. this.updateWidth(
  20146. backWidth,
  20147. defaultValue || {tdPadding: 10, tdBorder: 1}
  20148. )
  20149. },
  20150. updateWidth: function(width, defaultValue) {
  20151. var table = this.table,
  20152. tmpWidth =
  20153. UETable.getWidth(table) -
  20154. defaultValue.tdPadding * 2 -
  20155. defaultValue.tdBorder +
  20156. width
  20157. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  20158. table.setAttribute('width', tmpWidth)
  20159. return
  20160. }
  20161. var tds = domUtils.getElementsByTagName(this.table, 'td th')
  20162. utils.each(tds, function(td) {
  20163. td.setAttribute('width', width)
  20164. })
  20165. },
  20166. deleteCol: function(colIndex) {
  20167. var indexTable = this.indexTable,
  20168. tableRows = this.table.rows,
  20169. backTableWidth = this.table.getAttribute('width'),
  20170. backTdWidth = 0,
  20171. rowsNum = this.rowsNum,
  20172. cacheMap = {}
  20173. for (var rowIndex = 0; rowIndex < rowsNum; ) {
  20174. var infoRow = indexTable[rowIndex],
  20175. cellInfo = infoRow[colIndex],
  20176. key = cellInfo.rowIndex + '_' + cellInfo.colIndex
  20177. // 跳过已经处理过的Cell
  20178. if (cacheMap[key]) continue
  20179. cacheMap[key] = 1
  20180. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  20181. if (!backTdWidth)
  20182. backTdWidth =
  20183. cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0)
  20184. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  20185. if (cell.colSpan > 1) {
  20186. cell.colSpan--
  20187. } else {
  20188. tableRows[rowIndex].deleteCell(cellInfo.cellIndex)
  20189. }
  20190. rowIndex += cellInfo.rowSpan || 1
  20191. }
  20192. this.table.setAttribute('width', backTableWidth - backTdWidth)
  20193. this.update()
  20194. },
  20195. splitToCells: function(cell) {
  20196. var me = this,
  20197. cells = this.splitToRows(cell)
  20198. utils.each(cells, function(cell) {
  20199. me.splitToCols(cell)
  20200. })
  20201. },
  20202. splitToRows: function(cell) {
  20203. var cellInfo = this.getCellInfo(cell),
  20204. rowIndex = cellInfo.rowIndex,
  20205. colIndex = cellInfo.colIndex,
  20206. results = []
  20207. // 修改Cell的rowSpan
  20208. cell.rowSpan = 1
  20209. results.push(cell)
  20210. // 补齐单元格
  20211. for (
  20212. var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan;
  20213. i < endRow;
  20214. i++
  20215. ) {
  20216. if (i == rowIndex) continue
  20217. var tableRow = this.table.rows[i],
  20218. tmpCell = tableRow.insertCell(
  20219. colIndex - this.getPreviewMergedCellsNum(i, colIndex)
  20220. )
  20221. tmpCell.colSpan = cellInfo.colSpan
  20222. this.setCellContent(tmpCell)
  20223. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20224. tmpCell.setAttribute('align', cell.getAttribute('align'))
  20225. if (cell.style.cssText) {
  20226. tmpCell.style.cssText = cell.style.cssText
  20227. }
  20228. results.push(tmpCell)
  20229. }
  20230. this.update()
  20231. return results
  20232. },
  20233. getPreviewMergedCellsNum: function(rowIndex, colIndex) {
  20234. var indexRow = this.indexTable[rowIndex],
  20235. num = 0
  20236. for (var i = 0; i < colIndex; ) {
  20237. var colSpan = indexRow[i].colSpan,
  20238. tmpRowIndex = indexRow[i].rowIndex
  20239. num += colSpan - (tmpRowIndex == rowIndex ? 1 : 0)
  20240. i += colSpan
  20241. }
  20242. return num
  20243. },
  20244. splitToCols: function(cell) {
  20245. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  20246. cellInfo = this.getCellInfo(cell),
  20247. rowIndex = cellInfo.rowIndex,
  20248. colIndex = cellInfo.colIndex,
  20249. results = []
  20250. // 修改Cell的rowSpan
  20251. cell.colSpan = 1
  20252. cell.setAttribute('width', backWidth)
  20253. results.push(cell)
  20254. // 补齐单元格
  20255. for (
  20256. var j = colIndex, endCol = colIndex + cellInfo.colSpan;
  20257. j < endCol;
  20258. j++
  20259. ) {
  20260. if (j == colIndex) continue
  20261. var tableRow = this.table.rows[rowIndex],
  20262. tmpCell = tableRow.insertCell(
  20263. this.indexTable[rowIndex][j].cellIndex + 1
  20264. )
  20265. tmpCell.rowSpan = cellInfo.rowSpan
  20266. this.setCellContent(tmpCell)
  20267. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20268. tmpCell.setAttribute('align', cell.getAttribute('align'))
  20269. tmpCell.setAttribute('width', backWidth)
  20270. if (cell.style.cssText) {
  20271. tmpCell.style.cssText = cell.style.cssText
  20272. }
  20273. //处理th的情况
  20274. if (cell.tagName == 'TH') {
  20275. var th = cell.ownerDocument.createElement('th')
  20276. th.appendChild(tmpCell.firstChild)
  20277. th.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20278. th.rowSpan = tmpCell.rowSpan
  20279. tableRow.insertBefore(th, tmpCell)
  20280. domUtils.remove(tmpCell)
  20281. }
  20282. results.push(tmpCell)
  20283. }
  20284. this.update()
  20285. return results
  20286. },
  20287. isLastCell: function(cell, rowsNum, colsNum) {
  20288. rowsNum = rowsNum || this.rowsNum
  20289. colsNum = colsNum || this.colsNum
  20290. var cellInfo = this.getCellInfo(cell)
  20291. return (
  20292. cellInfo.rowIndex + cellInfo.rowSpan == rowsNum &&
  20293. cellInfo.colIndex + cellInfo.colSpan == colsNum
  20294. )
  20295. },
  20296. getLastCell: function(cells) {
  20297. cells = cells || this.table.getElementsByTagName('td')
  20298. var firstInfo = this.getCellInfo(cells[0])
  20299. var me = this,
  20300. last = cells[0],
  20301. tr = last.parentNode,
  20302. cellsNum = 0,
  20303. cols = 0,
  20304. rows
  20305. utils.each(cells, function(cell) {
  20306. if (cell.parentNode == tr) cols += cell.colSpan || 1
  20307. cellsNum += cell.rowSpan * cell.colSpan || 1
  20308. })
  20309. rows = cellsNum / cols
  20310. utils.each(cells, function(cell) {
  20311. if (me.isLastCell(cell, rows, cols)) {
  20312. last = cell
  20313. return false
  20314. }
  20315. })
  20316. return last
  20317. },
  20318. selectRow: function(rowIndex) {
  20319. var indexRow = this.indexTable[rowIndex],
  20320. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  20321. end = this.getCell(
  20322. indexRow[this.colsNum - 1].rowIndex,
  20323. indexRow[this.colsNum - 1].cellIndex
  20324. ),
  20325. range = this.getCellsRange(start, end)
  20326. this.setSelected(range)
  20327. },
  20328. selectTable: function() {
  20329. var tds = this.table.getElementsByTagName('td'),
  20330. range = this.getCellsRange(tds[0], tds[tds.length - 1])
  20331. this.setSelected(range)
  20332. },
  20333. setBackground: function(cells, value) {
  20334. if (typeof value === 'string') {
  20335. utils.each(cells, function(cell) {
  20336. cell.style.backgroundColor = value
  20337. })
  20338. } else if (typeof value === 'object') {
  20339. value = utils.extend(
  20340. {
  20341. repeat: true,
  20342. colorList: ['#ddd', '#fff']
  20343. },
  20344. value
  20345. )
  20346. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  20347. count = 0,
  20348. colors = value.colorList,
  20349. getColor = function(list, index, repeat) {
  20350. return list[index]
  20351. ? list[index]
  20352. : repeat
  20353. ? list[index % list.length]
  20354. : ''
  20355. }
  20356. for (var i = 0, cell; (cell = cells[i++]); ) {
  20357. var cellInfo = this.getCellInfo(cell)
  20358. cell.style.backgroundColor = getColor(
  20359. colors,
  20360. rowIndex + count == cellInfo.rowIndex ? count : ++count,
  20361. value.repeat
  20362. )
  20363. }
  20364. }
  20365. },
  20366. removeBackground: function(cells) {
  20367. utils.each(cells, function(cell) {
  20368. cell.style.backgroundColor = ''
  20369. })
  20370. }
  20371. }
  20372. function showError(e) {}
  20373. })()
  20374. // plugins/table.cmds.js
  20375. /**
  20376. * Created with JetBrains PhpStorm.
  20377. * User: taoqili
  20378. * Date: 13-2-20
  20379. * Time: 下午6:25
  20380. * To change this template use File | Settings | File Templates.
  20381. */
  20382. ;(function() {
  20383. var UT = UE.UETable,
  20384. getTableItemsByRange = function(editor) {
  20385. return UT.getTableItemsByRange(editor)
  20386. },
  20387. getUETableBySelected = function(editor) {
  20388. return UT.getUETableBySelected(editor)
  20389. },
  20390. getDefaultValue = function(editor, table) {
  20391. return UT.getDefaultValue(editor, table)
  20392. },
  20393. getUETable = function(tdOrTable) {
  20394. return UT.getUETable(tdOrTable)
  20395. }
  20396. UE.commands['inserttable'] = {
  20397. queryCommandState: function() {
  20398. return getTableItemsByRange(this).table ? -1 : 0
  20399. },
  20400. execCommand: function(cmd, opt) {
  20401. function createTable(opt, tdWidth) {
  20402. var html = [],
  20403. rowsNum = opt.numRows,
  20404. colsNum = opt.numCols
  20405. for (var r = 0; r < rowsNum; r++) {
  20406. html.push('<tr' + (r == 0 ? ' class="firstRow"' : '') + '>')
  20407. for (var c = 0; c < colsNum; c++) {
  20408. html.push(
  20409. '<td width="' +
  20410. tdWidth +
  20411. '" vAlign="' +
  20412. opt.tdvalign +
  20413. '" >' +
  20414. (browser.ie && browser.version < 11
  20415. ? domUtils.fillChar
  20416. : '<br/>') +
  20417. '</td>'
  20418. )
  20419. }
  20420. html.push('</tr>')
  20421. }
  20422. //禁止指定table-width
  20423. return '<table><tbody>' + html.join('') + '</tbody></table>'
  20424. }
  20425. if (!opt) {
  20426. opt = utils.extend(
  20427. {},
  20428. {
  20429. numCols: this.options.defaultCols,
  20430. numRows: this.options.defaultRows,
  20431. tdvalign: this.options.tdvalign
  20432. }
  20433. )
  20434. }
  20435. var me = this
  20436. var range = this.selection.getRange(),
  20437. start = range.startContainer,
  20438. firstParentBlock =
  20439. domUtils.findParent(
  20440. start,
  20441. function(node) {
  20442. return domUtils.isBlockElm(node)
  20443. },
  20444. true
  20445. ) || me.body
  20446. var defaultValue = getDefaultValue(me),
  20447. tableWidth = firstParentBlock.offsetWidth,
  20448. tdWidth = Math.floor(
  20449. tableWidth / opt.numCols -
  20450. defaultValue.tdPadding * 2 -
  20451. defaultValue.tdBorder
  20452. )
  20453. //todo其他属性
  20454. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign)
  20455. me.execCommand('inserthtml', createTable(opt, tdWidth))
  20456. }
  20457. }
  20458. UE.commands['insertparagraphbeforetable'] = {
  20459. queryCommandState: function() {
  20460. return getTableItemsByRange(this).cell ? 0 : -1
  20461. },
  20462. execCommand: function() {
  20463. var table = getTableItemsByRange(this).table
  20464. if (table) {
  20465. var p = this.document.createElement('p')
  20466. p.innerHTML = browser.ie ? '&nbsp;' : '<br />'
  20467. table.parentNode.insertBefore(p, table)
  20468. this.selection
  20469. .getRange()
  20470. .setStart(p, 0)
  20471. .setCursor()
  20472. }
  20473. }
  20474. }
  20475. UE.commands['deletetable'] = {
  20476. queryCommandState: function() {
  20477. var rng = this.selection.getRange()
  20478. return domUtils.findParentByTagName(rng.startContainer, 'table', true)
  20479. ? 0
  20480. : -1
  20481. },
  20482. execCommand: function(cmd, table) {
  20483. var rng = this.selection.getRange()
  20484. table =
  20485. table ||
  20486. domUtils.findParentByTagName(rng.startContainer, 'table', true)
  20487. if (table) {
  20488. var next = table.nextSibling
  20489. if (!next) {
  20490. next = domUtils.createElement(this.document, 'p', {
  20491. innerHTML: browser.ie ? domUtils.fillChar : '<br/>'
  20492. })
  20493. table.parentNode.insertBefore(next, table)
  20494. }
  20495. domUtils.remove(table)
  20496. rng = this.selection.getRange()
  20497. if (next.nodeType == 3) {
  20498. rng.setStartBefore(next)
  20499. } else {
  20500. rng.setStart(next, 0)
  20501. }
  20502. rng.setCursor(false, true)
  20503. this.fireEvent('tablehasdeleted')
  20504. }
  20505. }
  20506. }
  20507. UE.commands['cellalign'] = {
  20508. queryCommandState: function() {
  20509. return getSelectedArr(this).length ? 0 : -1
  20510. },
  20511. execCommand: function(cmd, align) {
  20512. var selectedTds = getSelectedArr(this)
  20513. if (selectedTds.length) {
  20514. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20515. ci.setAttribute('align', align)
  20516. }
  20517. }
  20518. }
  20519. }
  20520. UE.commands['cellvalign'] = {
  20521. queryCommandState: function() {
  20522. return getSelectedArr(this).length ? 0 : -1
  20523. },
  20524. execCommand: function(cmd, valign) {
  20525. var selectedTds = getSelectedArr(this)
  20526. if (selectedTds.length) {
  20527. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20528. ci.setAttribute('vAlign', valign)
  20529. }
  20530. }
  20531. }
  20532. }
  20533. UE.commands['insertcaption'] = {
  20534. queryCommandState: function() {
  20535. var table = getTableItemsByRange(this).table
  20536. if (table) {
  20537. return table.getElementsByTagName('caption').length == 0 ? 1 : -1
  20538. }
  20539. return -1
  20540. },
  20541. execCommand: function() {
  20542. var table = getTableItemsByRange(this).table
  20543. if (table) {
  20544. var caption = this.document.createElement('caption')
  20545. caption.innerHTML = browser.ie ? domUtils.fillChar : '<br/>'
  20546. table.insertBefore(caption, table.firstChild)
  20547. var range = this.selection.getRange()
  20548. range.setStart(caption, 0).setCursor()
  20549. }
  20550. }
  20551. }
  20552. UE.commands['deletecaption'] = {
  20553. queryCommandState: function() {
  20554. var rng = this.selection.getRange(),
  20555. table = domUtils.findParentByTagName(rng.startContainer, 'table')
  20556. if (table) {
  20557. return table.getElementsByTagName('caption').length == 0 ? -1 : 1
  20558. }
  20559. return -1
  20560. },
  20561. execCommand: function() {
  20562. var rng = this.selection.getRange(),
  20563. table = domUtils.findParentByTagName(rng.startContainer, 'table')
  20564. if (table) {
  20565. domUtils.remove(table.getElementsByTagName('caption')[0])
  20566. var range = this.selection.getRange()
  20567. range.setStart(table.rows[0].cells[0], 0).setCursor()
  20568. }
  20569. }
  20570. }
  20571. UE.commands['inserttitle'] = {
  20572. queryCommandState: function() {
  20573. var table = getTableItemsByRange(this).table
  20574. if (table) {
  20575. var firstRow = table.rows[0]
  20576. return firstRow.cells[
  20577. firstRow.cells.length - 1
  20578. ].tagName.toLowerCase() != 'th'
  20579. ? 0
  20580. : -1
  20581. }
  20582. return -1
  20583. },
  20584. execCommand: function() {
  20585. var table = getTableItemsByRange(this).table
  20586. if (table) {
  20587. getUETable(table).insertRow(0, 'th')
  20588. }
  20589. var th = table.getElementsByTagName('th')[0]
  20590. this.selection
  20591. .getRange()
  20592. .setStart(th, 0)
  20593. .setCursor(false, true)
  20594. }
  20595. }
  20596. UE.commands['deletetitle'] = {
  20597. queryCommandState: function() {
  20598. var table = getTableItemsByRange(this).table
  20599. if (table) {
  20600. var firstRow = table.rows[0]
  20601. return firstRow.cells[
  20602. firstRow.cells.length - 1
  20603. ].tagName.toLowerCase() == 'th'
  20604. ? 0
  20605. : -1
  20606. }
  20607. return -1
  20608. },
  20609. execCommand: function() {
  20610. var table = getTableItemsByRange(this).table
  20611. if (table) {
  20612. domUtils.remove(table.rows[0])
  20613. }
  20614. var td = table.getElementsByTagName('td')[0]
  20615. this.selection
  20616. .getRange()
  20617. .setStart(td, 0)
  20618. .setCursor(false, true)
  20619. }
  20620. }
  20621. UE.commands['inserttitlecol'] = {
  20622. queryCommandState: function() {
  20623. var table = getTableItemsByRange(this).table
  20624. if (table) {
  20625. var lastRow = table.rows[table.rows.length - 1]
  20626. return lastRow.getElementsByTagName('th').length ? -1 : 0
  20627. }
  20628. return -1
  20629. },
  20630. execCommand: function(cmd) {
  20631. var table = getTableItemsByRange(this).table
  20632. if (table) {
  20633. getUETable(table).insertCol(0, 'th')
  20634. }
  20635. resetTdWidth(table, this)
  20636. var th = table.getElementsByTagName('th')[0]
  20637. this.selection
  20638. .getRange()
  20639. .setStart(th, 0)
  20640. .setCursor(false, true)
  20641. }
  20642. }
  20643. UE.commands['deletetitlecol'] = {
  20644. queryCommandState: function() {
  20645. var table = getTableItemsByRange(this).table
  20646. if (table) {
  20647. var lastRow = table.rows[table.rows.length - 1]
  20648. return lastRow.getElementsByTagName('th').length ? 0 : -1
  20649. }
  20650. return -1
  20651. },
  20652. execCommand: function() {
  20653. var table = getTableItemsByRange(this).table
  20654. if (table) {
  20655. for (var i = 0; i < table.rows.length; i++) {
  20656. domUtils.remove(table.rows[i].children[0])
  20657. }
  20658. }
  20659. resetTdWidth(table, this)
  20660. var td = table.getElementsByTagName('td')[0]
  20661. this.selection
  20662. .getRange()
  20663. .setStart(td, 0)
  20664. .setCursor(false, true)
  20665. }
  20666. }
  20667. UE.commands['mergeright'] = {
  20668. queryCommandState: function(cmd) {
  20669. var tableItems = getTableItemsByRange(this),
  20670. table = tableItems.table,
  20671. cell = tableItems.cell
  20672. if (!table || !cell) return -1
  20673. var ut = getUETable(table)
  20674. if (ut.selectedTds.length) return -1
  20675. var cellInfo = ut.getCellInfo(cell),
  20676. rightColIndex = cellInfo.colIndex + cellInfo.colSpan
  20677. if (rightColIndex >= ut.colsNum) return -1 // 如果处于最右边则不能向右合并
  20678. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  20679. rightCell =
  20680. table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex]
  20681. if (!rightCell || cell.tagName != rightCell.tagName) return -1 // TH和TD不能相互合并
  20682. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20683. return rightCellInfo.rowIndex == cellInfo.rowIndex &&
  20684. rightCellInfo.rowSpan == cellInfo.rowSpan
  20685. ? 0
  20686. : -1
  20687. },
  20688. execCommand: function(cmd) {
  20689. var rng = this.selection.getRange(),
  20690. bk = rng.createBookmark(true)
  20691. var cell = getTableItemsByRange(this).cell,
  20692. ut = getUETable(cell)
  20693. ut.mergeRight(cell)
  20694. rng.moveToBookmark(bk).select()
  20695. }
  20696. }
  20697. UE.commands['mergedown'] = {
  20698. queryCommandState: function(cmd) {
  20699. var tableItems = getTableItemsByRange(this),
  20700. table = tableItems.table,
  20701. cell = tableItems.cell
  20702. if (!table || !cell) return -1
  20703. var ut = getUETable(table)
  20704. if (ut.selectedTds.length) return -1
  20705. var cellInfo = ut.getCellInfo(cell),
  20706. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan
  20707. if (downRowIndex >= ut.rowsNum) return -1 // 如果处于最下边则不能向下合并
  20708. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  20709. downCell =
  20710. table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex]
  20711. if (!downCell || cell.tagName != downCell.tagName) return -1 // TH和TD不能相互合并
  20712. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20713. return downCellInfo.colIndex == cellInfo.colIndex &&
  20714. downCellInfo.colSpan == cellInfo.colSpan
  20715. ? 0
  20716. : -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.mergeDown(cell)
  20724. rng.moveToBookmark(bk).select()
  20725. }
  20726. }
  20727. UE.commands['mergecells'] = {
  20728. queryCommandState: function() {
  20729. return getUETableBySelected(this) ? 0 : -1
  20730. },
  20731. execCommand: function() {
  20732. var ut = getUETableBySelected(this)
  20733. if (ut && ut.selectedTds.length) {
  20734. var cell = ut.selectedTds[0]
  20735. ut.mergeRange()
  20736. var rng = this.selection.getRange()
  20737. if (domUtils.isEmptyBlock(cell)) {
  20738. rng.setStart(cell, 0).collapse(true)
  20739. } else {
  20740. rng.selectNodeContents(cell)
  20741. }
  20742. rng.select()
  20743. }
  20744. }
  20745. }
  20746. UE.commands['insertrow'] = {
  20747. queryCommandState: function() {
  20748. var tableItems = getTableItemsByRange(this),
  20749. cell = tableItems.cell
  20750. return cell &&
  20751. (cell.tagName == 'TD' ||
  20752. (cell.tagName == 'TH' &&
  20753. tableItems.tr !== tableItems.table.rows[0])) &&
  20754. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20755. ? 0
  20756. : -1
  20757. },
  20758. execCommand: function() {
  20759. var rng = this.selection.getRange(),
  20760. bk = rng.createBookmark(true)
  20761. var tableItems = getTableItemsByRange(this),
  20762. cell = tableItems.cell,
  20763. table = tableItems.table,
  20764. ut = getUETable(table),
  20765. cellInfo = ut.getCellInfo(cell)
  20766. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  20767. if (!ut.selectedTds.length) {
  20768. ut.insertRow(cellInfo.rowIndex, cell)
  20769. } else {
  20770. var range = ut.cellsRange
  20771. for (
  20772. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20773. i < len;
  20774. i++
  20775. ) {
  20776. ut.insertRow(range.beginRowIndex, cell)
  20777. }
  20778. }
  20779. rng.moveToBookmark(bk).select()
  20780. if (table.getAttribute('interlaced') === 'enabled')
  20781. this.fireEvent('interlacetable', table)
  20782. }
  20783. }
  20784. //后插入行
  20785. UE.commands['insertrownext'] = {
  20786. queryCommandState: function() {
  20787. var tableItems = getTableItemsByRange(this),
  20788. cell = tableItems.cell
  20789. return cell &&
  20790. cell.tagName == 'TD' &&
  20791. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20792. ? 0
  20793. : -1
  20794. },
  20795. execCommand: function() {
  20796. var rng = this.selection.getRange(),
  20797. bk = rng.createBookmark(true)
  20798. var tableItems = getTableItemsByRange(this),
  20799. cell = tableItems.cell,
  20800. table = tableItems.table,
  20801. ut = getUETable(table),
  20802. cellInfo = ut.getCellInfo(cell)
  20803. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  20804. if (!ut.selectedTds.length) {
  20805. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell)
  20806. } else {
  20807. var range = ut.cellsRange
  20808. for (
  20809. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20810. i < len;
  20811. i++
  20812. ) {
  20813. ut.insertRow(range.endRowIndex + 1, cell)
  20814. }
  20815. }
  20816. rng.moveToBookmark(bk).select()
  20817. if (table.getAttribute('interlaced') === 'enabled')
  20818. this.fireEvent('interlacetable', table)
  20819. }
  20820. }
  20821. UE.commands['deleterow'] = {
  20822. queryCommandState: function() {
  20823. var tableItems = getTableItemsByRange(this)
  20824. return tableItems.cell ? 0 : -1
  20825. },
  20826. execCommand: function() {
  20827. var cell = getTableItemsByRange(this).cell,
  20828. ut = getUETable(cell),
  20829. cellsRange = ut.cellsRange,
  20830. cellInfo = ut.getCellInfo(cell),
  20831. preCell = ut.getVSideCell(cell),
  20832. nextCell = ut.getVSideCell(cell, true),
  20833. rng = this.selection.getRange()
  20834. if (utils.isEmptyObject(cellsRange)) {
  20835. ut.deleteRow(cellInfo.rowIndex)
  20836. } else {
  20837. for (
  20838. var i = cellsRange.beginRowIndex;
  20839. i < cellsRange.endRowIndex + 1;
  20840. i++
  20841. ) {
  20842. ut.deleteRow(cellsRange.beginRowIndex)
  20843. }
  20844. }
  20845. var table = ut.table
  20846. if (!table.getElementsByTagName('td').length) {
  20847. var nextSibling = table.nextSibling
  20848. domUtils.remove(table)
  20849. if (nextSibling) {
  20850. rng.setStart(nextSibling, 0).setCursor(false, true)
  20851. }
  20852. } else {
  20853. if (
  20854. cellInfo.rowSpan == 1 ||
  20855. cellInfo.rowSpan ==
  20856. cellsRange.endRowIndex - cellsRange.beginRowIndex + 1
  20857. ) {
  20858. if (nextCell || preCell)
  20859. rng.selectNodeContents(nextCell || preCell).setCursor(false, true)
  20860. } else {
  20861. var newCell = ut.getCell(
  20862. cellInfo.rowIndex,
  20863. ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex
  20864. )
  20865. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true)
  20866. }
  20867. }
  20868. if (table.getAttribute('interlaced') === 'enabled')
  20869. this.fireEvent('interlacetable', table)
  20870. }
  20871. }
  20872. UE.commands['insertcol'] = {
  20873. queryCommandState: function(cmd) {
  20874. var tableItems = getTableItemsByRange(this),
  20875. cell = tableItems.cell
  20876. return cell &&
  20877. (cell.tagName == 'TD' ||
  20878. (cell.tagName == 'TH' && cell !== tableItems.tr.cells[0])) &&
  20879. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20880. ? 0
  20881. : -1
  20882. },
  20883. execCommand: function(cmd) {
  20884. var rng = this.selection.getRange(),
  20885. bk = rng.createBookmark(true)
  20886. if (this.queryCommandState(cmd) == -1) return
  20887. var cell = getTableItemsByRange(this).cell,
  20888. ut = getUETable(cell),
  20889. cellInfo = ut.getCellInfo(cell)
  20890. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  20891. if (!ut.selectedTds.length) {
  20892. ut.insertCol(cellInfo.colIndex, cell)
  20893. } else {
  20894. var range = ut.cellsRange
  20895. for (
  20896. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20897. i < len;
  20898. i++
  20899. ) {
  20900. ut.insertCol(range.beginColIndex, cell)
  20901. }
  20902. }
  20903. rng.moveToBookmark(bk).select(true)
  20904. }
  20905. }
  20906. UE.commands['insertcolnext'] = {
  20907. queryCommandState: function() {
  20908. var tableItems = getTableItemsByRange(this),
  20909. cell = tableItems.cell
  20910. return cell &&
  20911. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20912. ? 0
  20913. : -1
  20914. },
  20915. execCommand: function() {
  20916. var rng = this.selection.getRange(),
  20917. bk = rng.createBookmark(true)
  20918. var cell = getTableItemsByRange(this).cell,
  20919. ut = getUETable(cell),
  20920. cellInfo = ut.getCellInfo(cell)
  20921. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  20922. if (!ut.selectedTds.length) {
  20923. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell)
  20924. } else {
  20925. var range = ut.cellsRange
  20926. for (
  20927. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20928. i < len;
  20929. i++
  20930. ) {
  20931. ut.insertCol(range.endColIndex + 1, cell)
  20932. }
  20933. }
  20934. rng.moveToBookmark(bk).select()
  20935. }
  20936. }
  20937. UE.commands['deletecol'] = {
  20938. queryCommandState: function() {
  20939. var tableItems = getTableItemsByRange(this)
  20940. return tableItems.cell ? 0 : -1
  20941. },
  20942. execCommand: function() {
  20943. var cell = getTableItemsByRange(this).cell,
  20944. ut = getUETable(cell),
  20945. range = ut.cellsRange,
  20946. cellInfo = ut.getCellInfo(cell),
  20947. preCell = ut.getHSideCell(cell),
  20948. nextCell = ut.getHSideCell(cell, true)
  20949. if (utils.isEmptyObject(range)) {
  20950. ut.deleteCol(cellInfo.colIndex)
  20951. } else {
  20952. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  20953. ut.deleteCol(range.beginColIndex)
  20954. }
  20955. }
  20956. var table = ut.table,
  20957. rng = this.selection.getRange()
  20958. if (!table.getElementsByTagName('td').length) {
  20959. var nextSibling = table.nextSibling
  20960. domUtils.remove(table)
  20961. if (nextSibling) {
  20962. rng.setStart(nextSibling, 0).setCursor(false, true)
  20963. }
  20964. } else {
  20965. if (domUtils.inDoc(cell, this.document)) {
  20966. rng.setStart(cell, 0).setCursor(false, true)
  20967. } else {
  20968. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  20969. rng.selectNodeContents(nextCell).setCursor(false, true)
  20970. } else {
  20971. if (preCell && domUtils.inDoc(preCell, this.document)) {
  20972. rng.selectNodeContents(preCell).setCursor(true, true)
  20973. }
  20974. }
  20975. }
  20976. }
  20977. }
  20978. }
  20979. UE.commands['splittocells'] = {
  20980. queryCommandState: function() {
  20981. var tableItems = getTableItemsByRange(this),
  20982. cell = tableItems.cell
  20983. if (!cell) return -1
  20984. var ut = getUETable(tableItems.table)
  20985. if (ut.selectedTds.length > 0) return -1
  20986. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1
  20987. },
  20988. execCommand: function() {
  20989. var rng = this.selection.getRange(),
  20990. bk = rng.createBookmark(true)
  20991. var cell = getTableItemsByRange(this).cell,
  20992. ut = getUETable(cell)
  20993. ut.splitToCells(cell)
  20994. rng.moveToBookmark(bk).select()
  20995. }
  20996. }
  20997. UE.commands['splittorows'] = {
  20998. queryCommandState: function() {
  20999. var tableItems = getTableItemsByRange(this),
  21000. cell = tableItems.cell
  21001. if (!cell) return -1
  21002. var ut = getUETable(tableItems.table)
  21003. if (ut.selectedTds.length > 0) return -1
  21004. return cell && cell.rowSpan > 1 ? 0 : -1
  21005. },
  21006. execCommand: function() {
  21007. var rng = this.selection.getRange(),
  21008. bk = rng.createBookmark(true)
  21009. var cell = getTableItemsByRange(this).cell,
  21010. ut = getUETable(cell)
  21011. ut.splitToRows(cell)
  21012. rng.moveToBookmark(bk).select()
  21013. }
  21014. }
  21015. UE.commands['splittocols'] = {
  21016. queryCommandState: function() {
  21017. var tableItems = getTableItemsByRange(this),
  21018. cell = tableItems.cell
  21019. if (!cell) return -1
  21020. var ut = getUETable(tableItems.table)
  21021. if (ut.selectedTds.length > 0) return -1
  21022. return cell && cell.colSpan > 1 ? 0 : -1
  21023. },
  21024. execCommand: function() {
  21025. var rng = this.selection.getRange(),
  21026. bk = rng.createBookmark(true)
  21027. var cell = getTableItemsByRange(this).cell,
  21028. ut = getUETable(cell)
  21029. ut.splitToCols(cell)
  21030. rng.moveToBookmark(bk).select()
  21031. }
  21032. }
  21033. UE.commands['adaptbytext'] = UE.commands['adaptbywindow'] = {
  21034. queryCommandState: function() {
  21035. return getTableItemsByRange(this).table ? 0 : -1
  21036. },
  21037. execCommand: function(cmd) {
  21038. var tableItems = getTableItemsByRange(this),
  21039. table = tableItems.table
  21040. if (table) {
  21041. if (cmd == 'adaptbywindow') {
  21042. resetTdWidth(table, this)
  21043. } else {
  21044. var cells = domUtils.getElementsByTagName(table, 'td th')
  21045. utils.each(cells, function(cell) {
  21046. cell.removeAttribute('width')
  21047. })
  21048. table.removeAttribute('width')
  21049. }
  21050. }
  21051. }
  21052. }
  21053. //平均分配各列
  21054. UE.commands['averagedistributecol'] = {
  21055. queryCommandState: function() {
  21056. var ut = getUETableBySelected(this)
  21057. if (!ut) return -1
  21058. return ut.isFullRow() || ut.isFullCol() ? 0 : -1
  21059. },
  21060. execCommand: function(cmd) {
  21061. var me = this,
  21062. ut = getUETableBySelected(me)
  21063. function getAverageWidth() {
  21064. var tb = ut.table,
  21065. averageWidth,
  21066. sumWidth = 0,
  21067. colsNum = 0,
  21068. tbAttr = getDefaultValue(me, tb)
  21069. if (ut.isFullRow()) {
  21070. sumWidth = tb.offsetWidth
  21071. colsNum = ut.colsNum
  21072. } else {
  21073. var begin = ut.cellsRange.beginColIndex,
  21074. end = ut.cellsRange.endColIndex,
  21075. node
  21076. for (var i = begin; i <= end; ) {
  21077. node = ut.selectedTds[i]
  21078. sumWidth += node.offsetWidth
  21079. i += node.colSpan
  21080. colsNum += 1
  21081. }
  21082. }
  21083. averageWidth =
  21084. Math.ceil(sumWidth / colsNum) -
  21085. tbAttr.tdBorder * 2 -
  21086. tbAttr.tdPadding * 2
  21087. return averageWidth
  21088. }
  21089. function setAverageWidth(averageWidth) {
  21090. utils.each(domUtils.getElementsByTagName(ut.table, 'th'), function(
  21091. node
  21092. ) {
  21093. node.setAttribute('width', '')
  21094. })
  21095. var cells = ut.isFullRow()
  21096. ? domUtils.getElementsByTagName(ut.table, 'td')
  21097. : ut.selectedTds
  21098. utils.each(cells, function(node) {
  21099. if (node.colSpan == 1) {
  21100. node.setAttribute('width', averageWidth)
  21101. }
  21102. })
  21103. }
  21104. if (ut && ut.selectedTds.length) {
  21105. setAverageWidth(getAverageWidth())
  21106. }
  21107. }
  21108. }
  21109. //平均分配各行
  21110. UE.commands['averagedistributerow'] = {
  21111. queryCommandState: function() {
  21112. var ut = getUETableBySelected(this)
  21113. if (!ut) return -1
  21114. if (ut.selectedTds && /th/gi.test(ut.selectedTds[0].tagName)) return -1
  21115. return ut.isFullRow() || ut.isFullCol() ? 0 : -1
  21116. },
  21117. execCommand: function(cmd) {
  21118. var me = this,
  21119. ut = getUETableBySelected(me)
  21120. function getAverageHeight() {
  21121. var averageHeight,
  21122. rowNum,
  21123. sumHeight = 0,
  21124. tb = ut.table,
  21125. tbAttr = getDefaultValue(me, tb),
  21126. tdpadding = parseInt(
  21127. domUtils.getComputedStyle(
  21128. tb.getElementsByTagName('td')[0],
  21129. 'padding-top'
  21130. )
  21131. )
  21132. if (ut.isFullCol()) {
  21133. var captionArr = domUtils.getElementsByTagName(tb, 'caption'),
  21134. thArr = domUtils.getElementsByTagName(tb, 'th'),
  21135. captionHeight,
  21136. thHeight
  21137. if (captionArr.length > 0) {
  21138. captionHeight = captionArr[0].offsetHeight
  21139. }
  21140. if (thArr.length > 0) {
  21141. thHeight = thArr[0].offsetHeight
  21142. }
  21143. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0)
  21144. rowNum = thArr.length == 0 ? ut.rowsNum : ut.rowsNum - 1
  21145. } else {
  21146. var begin = ut.cellsRange.beginRowIndex,
  21147. end = ut.cellsRange.endRowIndex,
  21148. count = 0,
  21149. trs = domUtils.getElementsByTagName(tb, 'tr')
  21150. for (var i = begin; i <= end; i++) {
  21151. sumHeight += trs[i].offsetHeight
  21152. count += 1
  21153. }
  21154. rowNum = count
  21155. }
  21156. //ie8下是混杂模式
  21157. if (browser.ie && browser.version < 9) {
  21158. averageHeight = Math.ceil(sumHeight / rowNum)
  21159. } else {
  21160. averageHeight =
  21161. Math.ceil(sumHeight / rowNum) -
  21162. tbAttr.tdBorder * 2 -
  21163. tdpadding * 2
  21164. }
  21165. return averageHeight
  21166. }
  21167. function setAverageHeight(averageHeight) {
  21168. var cells = ut.isFullCol()
  21169. ? domUtils.getElementsByTagName(ut.table, 'td')
  21170. : ut.selectedTds
  21171. utils.each(cells, function(node) {
  21172. if (node.rowSpan == 1) {
  21173. node.setAttribute('height', averageHeight)
  21174. }
  21175. })
  21176. }
  21177. if (ut && ut.selectedTds.length) {
  21178. setAverageHeight(getAverageHeight())
  21179. }
  21180. }
  21181. }
  21182. //单元格对齐方式
  21183. UE.commands['cellalignment'] = {
  21184. queryCommandState: function() {
  21185. return getTableItemsByRange(this).table ? 0 : -1
  21186. },
  21187. execCommand: function(cmd, data) {
  21188. var me = this,
  21189. ut = getUETableBySelected(me)
  21190. if (!ut) {
  21191. var start = me.selection.getStart(),
  21192. cell =
  21193. start &&
  21194. domUtils.findParentByTagName(start, ['td', 'th', 'caption'], true)
  21195. if (!/caption/gi.test(cell.tagName)) {
  21196. domUtils.setAttributes(cell, data)
  21197. } else {
  21198. cell.style.textAlign = data.align
  21199. cell.style.verticalAlign = data.vAlign
  21200. }
  21201. me.selection.getRange().setCursor(true)
  21202. } else {
  21203. utils.each(ut.selectedTds, function(cell) {
  21204. domUtils.setAttributes(cell, data)
  21205. })
  21206. }
  21207. },
  21208. /**
  21209. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  21210. * @see UE.UETable.getTableCellAlignState
  21211. */
  21212. queryCommandValue: function(cmd) {
  21213. var activeMenuCell = getTableItemsByRange(this).cell
  21214. if (!activeMenuCell) {
  21215. activeMenuCell = getSelectedArr(this)[0]
  21216. }
  21217. if (!activeMenuCell) {
  21218. return null
  21219. } else {
  21220. //获取同时选中的其他单元格
  21221. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds
  21222. !cells.length && (cells = activeMenuCell)
  21223. return UE.UETable.getTableCellAlignState(cells)
  21224. }
  21225. }
  21226. }
  21227. //表格对齐方式
  21228. UE.commands['tablealignment'] = {
  21229. queryCommandState: function() {
  21230. if (browser.ie && browser.version < 8) {
  21231. return -1
  21232. }
  21233. return getTableItemsByRange(this).table ? 0 : -1
  21234. },
  21235. execCommand: function(cmd, value) {
  21236. var me = this,
  21237. start = me.selection.getStart(),
  21238. table = start && domUtils.findParentByTagName(start, ['table'], true)
  21239. if (table) {
  21240. table.setAttribute('align', value)
  21241. }
  21242. }
  21243. }
  21244. //表格属性
  21245. UE.commands['edittable'] = {
  21246. queryCommandState: function() {
  21247. return getTableItemsByRange(this).table ? 0 : -1
  21248. },
  21249. execCommand: function(cmd, color) {
  21250. var rng = this.selection.getRange(),
  21251. table = domUtils.findParentByTagName(rng.startContainer, 'table')
  21252. if (table) {
  21253. var arr = domUtils
  21254. .getElementsByTagName(table, 'td')
  21255. .concat(
  21256. domUtils.getElementsByTagName(table, 'th'),
  21257. domUtils.getElementsByTagName(table, 'caption')
  21258. )
  21259. utils.each(arr, function(node) {
  21260. node.style.borderColor = color
  21261. })
  21262. }
  21263. }
  21264. }
  21265. //单元格属性
  21266. UE.commands['edittd'] = {
  21267. queryCommandState: function() {
  21268. return getTableItemsByRange(this).table ? 0 : -1
  21269. },
  21270. execCommand: function(cmd, bkColor) {
  21271. var me = this,
  21272. ut = getUETableBySelected(me)
  21273. if (!ut) {
  21274. var start = me.selection.getStart(),
  21275. cell =
  21276. start &&
  21277. domUtils.findParentByTagName(start, ['td', 'th', 'caption'], true)
  21278. if (cell) {
  21279. cell.style.backgroundColor = bkColor
  21280. }
  21281. } else {
  21282. utils.each(ut.selectedTds, function(cell) {
  21283. cell.style.backgroundColor = bkColor
  21284. })
  21285. }
  21286. }
  21287. }
  21288. UE.commands['settablebackground'] = {
  21289. queryCommandState: function() {
  21290. return getSelectedArr(this).length > 1 ? 0 : -1
  21291. },
  21292. execCommand: function(cmd, value) {
  21293. var cells, ut
  21294. cells = getSelectedArr(this)
  21295. ut = getUETable(cells[0])
  21296. ut.setBackground(cells, value)
  21297. }
  21298. }
  21299. UE.commands['cleartablebackground'] = {
  21300. queryCommandState: function() {
  21301. var cells = getSelectedArr(this)
  21302. if (!cells.length) return -1
  21303. for (var i = 0, cell; (cell = cells[i++]); ) {
  21304. if (cell.style.backgroundColor !== '') return 0
  21305. }
  21306. return -1
  21307. },
  21308. execCommand: function() {
  21309. var cells = getSelectedArr(this),
  21310. ut = getUETable(cells[0])
  21311. ut.removeBackground(cells)
  21312. }
  21313. }
  21314. UE.commands['interlacetable'] = UE.commands['uninterlacetable'] = {
  21315. queryCommandState: function(cmd) {
  21316. var table = getTableItemsByRange(this).table
  21317. if (!table) return -1
  21318. var interlaced = table.getAttribute('interlaced')
  21319. if (cmd == 'interlacetable') {
  21320. //TODO 待定
  21321. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  21322. return interlaced === 'enabled' ? -1 : 0
  21323. } else {
  21324. return !interlaced || interlaced === 'disabled' ? -1 : 0
  21325. }
  21326. },
  21327. execCommand: function(cmd, classList) {
  21328. var table = getTableItemsByRange(this).table
  21329. if (cmd == 'interlacetable') {
  21330. table.setAttribute('interlaced', 'enabled')
  21331. this.fireEvent('interlacetable', table, classList)
  21332. } else {
  21333. table.setAttribute('interlaced', 'disabled')
  21334. this.fireEvent('uninterlacetable', table)
  21335. }
  21336. }
  21337. }
  21338. UE.commands['setbordervisible'] = {
  21339. queryCommandState: function(cmd) {
  21340. var table = getTableItemsByRange(this).table
  21341. if (!table) return -1
  21342. return 0
  21343. },
  21344. execCommand: function() {
  21345. var table = getTableItemsByRange(this).table
  21346. utils.each(domUtils.getElementsByTagName(table, 'td'), function(td) {
  21347. td.style.borderWidth = '1px'
  21348. td.style.borderStyle = 'solid'
  21349. })
  21350. }
  21351. }
  21352. function resetTdWidth(table, editor) {
  21353. var tds = domUtils.getElementsByTagName(table, 'td th')
  21354. utils.each(tds, function(td) {
  21355. td.removeAttribute('width')
  21356. })
  21357. table.setAttribute(
  21358. 'width',
  21359. getTableWidth(editor, true, getDefaultValue(editor, table))
  21360. )
  21361. var tdsWidths = []
  21362. setTimeout(function() {
  21363. utils.each(tds, function(td) {
  21364. td.colSpan == 1 && tdsWidths.push(td.offsetWidth)
  21365. })
  21366. utils.each(tds, function(td, i) {
  21367. td.colSpan == 1 && td.setAttribute('width', tdsWidths[i] + '')
  21368. })
  21369. }, 0)
  21370. }
  21371. function getTableWidth(editor, needIEHack, defaultValue) {
  21372. var body = editor.body
  21373. return (
  21374. body.offsetWidth -
  21375. (needIEHack
  21376. ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2
  21377. : 0) -
  21378. defaultValue.tableBorder * 2 -
  21379. (editor.options.offsetWidth || 0)
  21380. )
  21381. }
  21382. function getSelectedArr(editor) {
  21383. var cell = getTableItemsByRange(editor).cell
  21384. if (cell) {
  21385. var ut = getUETable(cell)
  21386. return ut.selectedTds.length ? ut.selectedTds : [cell]
  21387. } else {
  21388. return []
  21389. }
  21390. }
  21391. })()
  21392. // plugins/table.action.js
  21393. /**
  21394. * Created with JetBrains PhpStorm.
  21395. * User: taoqili
  21396. * Date: 12-10-12
  21397. * Time: 上午10:05
  21398. * To change this template use File | Settings | File Templates.
  21399. */
  21400. UE.plugins['table'] = function() {
  21401. var me = this,
  21402. tabTimer = null,
  21403. //拖动计时器
  21404. tableDragTimer = null,
  21405. //双击计时器
  21406. tableResizeTimer = null,
  21407. //单元格最小宽度
  21408. cellMinWidth = 5,
  21409. isInResizeBuffer = false,
  21410. //单元格边框大小
  21411. cellBorderWidth = 5,
  21412. //鼠标偏移距离
  21413. offsetOfTableCell = 10,
  21414. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  21415. singleClickState = 0,
  21416. userActionStatus = null,
  21417. //双击允许的时间范围
  21418. dblclickTime = 360,
  21419. UT = UE.UETable,
  21420. getUETable = function(tdOrTable) {
  21421. return UT.getUETable(tdOrTable)
  21422. },
  21423. getUETableBySelected = function(editor) {
  21424. return UT.getUETableBySelected(editor)
  21425. },
  21426. getDefaultValue = function(editor, table) {
  21427. return UT.getDefaultValue(editor, table)
  21428. },
  21429. removeSelectedClass = function(cells) {
  21430. return UT.removeSelectedClass(cells)
  21431. }
  21432. function showError(e) {
  21433. // throw e;
  21434. }
  21435. me.ready(function() {
  21436. var me = this
  21437. var orgGetText = me.selection.getText
  21438. me.selection.getText = function() {
  21439. var table = getUETableBySelected(me)
  21440. if (table) {
  21441. var str = ''
  21442. utils.each(table.selectedTds, function(td) {
  21443. str += td[browser.ie ? 'innerText' : 'textContent']
  21444. })
  21445. return str
  21446. } else {
  21447. return orgGetText.call(me.selection)
  21448. }
  21449. }
  21450. })
  21451. //处理拖动及框选相关方法
  21452. var startTd = null, //鼠标按下时的锚点td
  21453. currentTd = null, //当前鼠标经过时的td
  21454. onDrag = '', //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  21455. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  21456. dragButton = null,
  21457. dragOver = false,
  21458. dragLine = null, //模拟的拖动线
  21459. dragTd = null //发生拖动的目标td
  21460. var mousedown = false,
  21461. //todo 判断混乱模式
  21462. needIEHack = true
  21463. me.setOpt({
  21464. maxColNum: 20,
  21465. maxRowNum: 100,
  21466. defaultCols: 5,
  21467. defaultRows: 5,
  21468. tdvalign: 'top',
  21469. cursorpath: me.options.UEDITOR_HOME_URL + 'themes/default/images/cursor_',
  21470. tableDragable: false,
  21471. classList: [
  21472. 'ue-table-interlace-color-single',
  21473. 'ue-table-interlace-color-double'
  21474. ]
  21475. })
  21476. me.getUETable = getUETable
  21477. var commands = {
  21478. deletetable: 1,
  21479. inserttable: 1,
  21480. cellvalign: 1,
  21481. insertcaption: 1,
  21482. deletecaption: 1,
  21483. inserttitle: 1,
  21484. deletetitle: 1,
  21485. mergeright: 1,
  21486. mergedown: 1,
  21487. mergecells: 1,
  21488. insertrow: 1,
  21489. insertrownext: 1,
  21490. deleterow: 1,
  21491. insertcol: 1,
  21492. insertcolnext: 1,
  21493. deletecol: 1,
  21494. splittocells: 1,
  21495. splittorows: 1,
  21496. splittocols: 1,
  21497. adaptbytext: 1,
  21498. adaptbywindow: 1,
  21499. adaptbycustomer: 1,
  21500. insertparagraph: 1,
  21501. insertparagraphbeforetable: 1,
  21502. averagedistributecol: 1,
  21503. averagedistributerow: 1
  21504. }
  21505. me.ready(function() {
  21506. utils.cssRule(
  21507. 'table',
  21508. //选中的td上的样式
  21509. '.selectTdClass{background-color:#edf5fa !important}' +
  21510. 'table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}' +
  21511. //插入的表格的默认样式
  21512. 'table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  21513. 'td,th{padding: 5px 10px;border: 1px solid #DDD;}' +
  21514. 'caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  21515. 'th{border-top:1px solid #BBB;background-color:#F7F7F7;}' +
  21516. 'table tr.firstRow th{border-top-width:2px;}' +
  21517. '.ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }' +
  21518. 'td p{margin:0;padding:0;}',
  21519. me.document
  21520. )
  21521. var tableCopyList, isFullCol, isFullRow
  21522. //注册del/backspace事件
  21523. me.addListener('keydown', function(cmd, evt) {
  21524. var me = this
  21525. var keyCode = evt.keyCode || evt.which
  21526. if (keyCode == 8) {
  21527. var ut = getUETableBySelected(me)
  21528. if (ut && ut.selectedTds.length) {
  21529. if (ut.isFullCol()) {
  21530. me.execCommand('deletecol')
  21531. } else if (ut.isFullRow()) {
  21532. me.execCommand('deleterow')
  21533. } else {
  21534. me.fireEvent('delcells')
  21535. }
  21536. domUtils.preventDefault(evt)
  21537. }
  21538. var caption = domUtils.findParentByTagName(
  21539. me.selection.getStart(),
  21540. 'caption',
  21541. true
  21542. ),
  21543. range = me.selection.getRange()
  21544. if (range.collapsed && caption && isEmptyBlock(caption)) {
  21545. me.fireEvent('saveScene')
  21546. var table = caption.parentNode
  21547. domUtils.remove(caption)
  21548. if (table) {
  21549. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true)
  21550. }
  21551. me.fireEvent('saveScene')
  21552. }
  21553. }
  21554. if (keyCode == 46) {
  21555. ut = getUETableBySelected(me)
  21556. if (ut) {
  21557. me.fireEvent('saveScene')
  21558. for (var i = 0, ci; (ci = ut.selectedTds[i++]); ) {
  21559. domUtils.fillNode(me.document, ci)
  21560. }
  21561. me.fireEvent('saveScene')
  21562. domUtils.preventDefault(evt)
  21563. }
  21564. }
  21565. if (keyCode == 13) {
  21566. var rng = me.selection.getRange(),
  21567. caption = domUtils.findParentByTagName(
  21568. rng.startContainer,
  21569. 'caption',
  21570. true
  21571. )
  21572. if (caption) {
  21573. var table = domUtils.findParentByTagName(caption, 'table')
  21574. if (!rng.collapsed) {
  21575. rng.deleteContents()
  21576. me.fireEvent('saveScene')
  21577. } else {
  21578. if (caption) {
  21579. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true)
  21580. }
  21581. }
  21582. domUtils.preventDefault(evt)
  21583. return
  21584. }
  21585. if (rng.collapsed) {
  21586. var table = domUtils.findParentByTagName(
  21587. rng.startContainer,
  21588. 'table'
  21589. )
  21590. if (table) {
  21591. var cell = table.rows[0].cells[0],
  21592. start = domUtils.findParentByTagName(
  21593. me.selection.getStart(),
  21594. ['td', 'th'],
  21595. true
  21596. ),
  21597. preNode = table.previousSibling
  21598. if (
  21599. cell === start &&
  21600. (!preNode ||
  21601. (preNode.nodeType == 1 && preNode.tagName == 'TABLE')) &&
  21602. domUtils.isStartInblock(rng)
  21603. ) {
  21604. var first = domUtils.findParent(
  21605. me.selection.getStart(),
  21606. function(n) {
  21607. return domUtils.isBlockElm(n)
  21608. },
  21609. true
  21610. )
  21611. if (
  21612. first &&
  21613. (/t(h|d)/i.test(first.tagName) || first === start.firstChild)
  21614. ) {
  21615. me.execCommand('insertparagraphbeforetable')
  21616. domUtils.preventDefault(evt)
  21617. }
  21618. }
  21619. }
  21620. }
  21621. }
  21622. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == '67') {
  21623. tableCopyList = null
  21624. var ut = getUETableBySelected(me)
  21625. if (ut) {
  21626. var tds = ut.selectedTds
  21627. isFullCol = ut.isFullCol()
  21628. isFullRow = ut.isFullRow()
  21629. tableCopyList = [[ut.cloneCell(tds[0], null, true)]]
  21630. for (var i = 1, ci; (ci = tds[i]); i++) {
  21631. if (ci.parentNode !== tds[i - 1].parentNode) {
  21632. tableCopyList.push([ut.cloneCell(ci, null, true)])
  21633. } else {
  21634. tableCopyList[tableCopyList.length - 1].push(
  21635. ut.cloneCell(ci, null, true)
  21636. )
  21637. }
  21638. }
  21639. }
  21640. }
  21641. })
  21642. me.addListener('tablehasdeleted', function() {
  21643. toggleDraggableState(this, false, '', null)
  21644. if (dragButton) domUtils.remove(dragButton)
  21645. })
  21646. me.addListener('beforepaste', function(cmd, html) {
  21647. var me = this
  21648. var rng = me.selection.getRange()
  21649. if (domUtils.findParentByTagName(rng.startContainer, 'caption', true)) {
  21650. var div = me.document.createElement('div')
  21651. div.innerHTML = html.html
  21652. //trace:3729
  21653. html.html = div[browser.ie9below ? 'innerText' : 'textContent']
  21654. return
  21655. }
  21656. var table = getUETableBySelected(me)
  21657. if (tableCopyList) {
  21658. me.fireEvent('saveScene')
  21659. var rng = me.selection.getRange()
  21660. var td = domUtils.findParentByTagName(
  21661. rng.startContainer,
  21662. ['td', 'th'],
  21663. true
  21664. ),
  21665. tmpNode,
  21666. preNode
  21667. if (td) {
  21668. var ut = getUETable(td)
  21669. if (isFullRow) {
  21670. var rowIndex = ut.getCellInfo(td).rowIndex
  21671. if (td.tagName == 'TH') {
  21672. rowIndex++
  21673. }
  21674. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21675. var tr = ut.insertRow(rowIndex++, 'td')
  21676. for (var j = 0, cj; (cj = ci[j]); j++) {
  21677. var cell = tr.cells[j]
  21678. if (!cell) {
  21679. cell = tr.insertCell(j)
  21680. }
  21681. cell.innerHTML = cj.innerHTML
  21682. cj.getAttribute('width') &&
  21683. cell.setAttribute('width', cj.getAttribute('width'))
  21684. cj.getAttribute('vAlign') &&
  21685. cell.setAttribute('vAlign', cj.getAttribute('vAlign'))
  21686. cj.getAttribute('align') &&
  21687. cell.setAttribute('align', cj.getAttribute('align'))
  21688. cj.style.cssText && (cell.style.cssText = cj.style.cssText)
  21689. }
  21690. for (var j = 0, cj; (cj = tr.cells[j]); j++) {
  21691. if (!ci[j]) break
  21692. cj.innerHTML = ci[j].innerHTML
  21693. ci[j].getAttribute('width') &&
  21694. cj.setAttribute('width', ci[j].getAttribute('width'))
  21695. ci[j].getAttribute('vAlign') &&
  21696. cj.setAttribute('vAlign', ci[j].getAttribute('vAlign'))
  21697. ci[j].getAttribute('align') &&
  21698. cj.setAttribute('align', ci[j].getAttribute('align'))
  21699. ci[j].style.cssText &&
  21700. (cj.style.cssText = ci[j].style.cssText)
  21701. }
  21702. }
  21703. } else {
  21704. if (isFullCol) {
  21705. cellInfo = ut.getCellInfo(td)
  21706. var maxColNum = 0
  21707. for (var j = 0, ci = tableCopyList[0], cj; (cj = ci[j++]); ) {
  21708. maxColNum += cj.colSpan || 1
  21709. }
  21710. me.__hasEnterExecCommand = true
  21711. for (i = 0; i < maxColNum; i++) {
  21712. me.execCommand('insertcol')
  21713. }
  21714. me.__hasEnterExecCommand = false
  21715. td = ut.table.rows[0].cells[cellInfo.cellIndex]
  21716. if (td.tagName == 'TH') {
  21717. td = ut.table.rows[1].cells[cellInfo.cellIndex]
  21718. }
  21719. }
  21720. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21721. tmpNode = td
  21722. for (var j = 0, cj; (cj = ci[j++]); ) {
  21723. if (td) {
  21724. td.innerHTML = cj.innerHTML
  21725. //todo 定制处理
  21726. cj.getAttribute('width') &&
  21727. td.setAttribute('width', cj.getAttribute('width'))
  21728. cj.getAttribute('vAlign') &&
  21729. td.setAttribute('vAlign', cj.getAttribute('vAlign'))
  21730. cj.getAttribute('align') &&
  21731. td.setAttribute('align', cj.getAttribute('align'))
  21732. cj.style.cssText && (td.style.cssText = cj.style.cssText)
  21733. preNode = td
  21734. td = td.nextSibling
  21735. } else {
  21736. var cloneTd = cj.cloneNode(true)
  21737. domUtils.removeAttributes(cloneTd, [
  21738. 'class',
  21739. 'rowSpan',
  21740. 'colSpan'
  21741. ])
  21742. preNode.parentNode.appendChild(cloneTd)
  21743. }
  21744. }
  21745. td = ut.getNextCell(tmpNode, true, true)
  21746. if (!tableCopyList[i]) break
  21747. if (!td) {
  21748. var cellInfo = ut.getCellInfo(tmpNode)
  21749. ut.table.insertRow(ut.table.rows.length)
  21750. ut.update()
  21751. td = ut.getVSideCell(tmpNode, true)
  21752. }
  21753. }
  21754. }
  21755. ut.update()
  21756. } else {
  21757. table = me.document.createElement('table')
  21758. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21759. var tr = table.insertRow(table.rows.length)
  21760. for (var j = 0, cj; (cj = ci[j++]); ) {
  21761. cloneTd = UT.cloneCell(cj, null, true)
  21762. domUtils.removeAttributes(cloneTd, ['class'])
  21763. tr.appendChild(cloneTd)
  21764. }
  21765. if (j == 2 && cloneTd.rowSpan > 1) {
  21766. cloneTd.rowSpan = 1
  21767. }
  21768. }
  21769. var defaultValue = getDefaultValue(me),
  21770. width =
  21771. me.body.offsetWidth -
  21772. (needIEHack
  21773. ? parseInt(
  21774. domUtils.getComputedStyle(me.body, 'margin-left'),
  21775. 10
  21776. ) * 2
  21777. : 0) -
  21778. defaultValue.tableBorder * 2 -
  21779. (me.options.offsetWidth || 0)
  21780. me.execCommand(
  21781. 'insertHTML',
  21782. '<table ' +
  21783. (isFullCol && isFullRow ? 'width="' + width + '"' : '') +
  21784. '>' +
  21785. table.innerHTML
  21786. .replace(/>\s*</g, '><')
  21787. .replace(/\bth\b/gi, 'td') +
  21788. '</table>'
  21789. )
  21790. }
  21791. me.fireEvent('contentchange')
  21792. me.fireEvent('saveScene')
  21793. html.html = ''
  21794. return true
  21795. } else {
  21796. var div = me.document.createElement('div'),
  21797. tables
  21798. div.innerHTML = html.html
  21799. tables = div.getElementsByTagName('table')
  21800. if (domUtils.findParentByTagName(me.selection.getStart(), 'table')) {
  21801. utils.each(tables, function(t) {
  21802. domUtils.remove(t)
  21803. })
  21804. if (
  21805. domUtils.findParentByTagName(
  21806. me.selection.getStart(),
  21807. 'caption',
  21808. true
  21809. )
  21810. ) {
  21811. div.innerHTML = div[browser.ie ? 'innerText' : 'textContent']
  21812. }
  21813. } else {
  21814. utils.each(tables, function(table) {
  21815. removeStyleSize(table, true)
  21816. domUtils.removeAttributes(table, ['style', 'border'])
  21817. utils.each(domUtils.getElementsByTagName(table, 'td'), function(
  21818. td
  21819. ) {
  21820. if (isEmptyBlock(td)) {
  21821. domUtils.fillNode(me.document, td)
  21822. }
  21823. removeStyleSize(td, true)
  21824. // domUtils.removeAttributes(td, ['style'])
  21825. })
  21826. })
  21827. }
  21828. html.html = div.innerHTML
  21829. }
  21830. })
  21831. me.addListener('afterpaste', function() {
  21832. utils.each(domUtils.getElementsByTagName(me.body, 'table'), function(
  21833. table
  21834. ) {
  21835. if (table.offsetWidth > me.body.offsetWidth) {
  21836. var defaultValue = getDefaultValue(me, table)
  21837. table.style.width =
  21838. me.body.offsetWidth -
  21839. (needIEHack
  21840. ? parseInt(
  21841. domUtils.getComputedStyle(me.body, 'margin-left'),
  21842. 10
  21843. ) * 2
  21844. : 0) -
  21845. defaultValue.tableBorder * 2 -
  21846. (me.options.offsetWidth || 0) +
  21847. 'px'
  21848. }
  21849. })
  21850. })
  21851. me.addListener('blur', function() {
  21852. tableCopyList = null
  21853. })
  21854. var timer
  21855. me.addListener('keydown', function() {
  21856. clearTimeout(timer)
  21857. timer = setTimeout(function() {
  21858. var rng = me.selection.getRange(),
  21859. cell = domUtils.findParentByTagName(
  21860. rng.startContainer,
  21861. ['th', 'td'],
  21862. true
  21863. )
  21864. if (cell) {
  21865. var table = cell.parentNode.parentNode.parentNode
  21866. if (table.offsetWidth > table.getAttribute('width')) {
  21867. cell.style.wordBreak = 'break-all'
  21868. }
  21869. }
  21870. }, 100)
  21871. })
  21872. me.addListener('selectionchange', function() {
  21873. toggleDraggableState(me, false, '', null)
  21874. })
  21875. //内容变化时触发索引更新
  21876. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  21877. me.addListener('contentchange', function() {
  21878. var me = this
  21879. //尽可能排除一些不需要更新的状况
  21880. hideDragLine(me)
  21881. if (getUETableBySelected(me)) return
  21882. var rng = me.selection.getRange()
  21883. var start = rng.startContainer
  21884. start = domUtils.findParentByTagName(start, ['td', 'th'], true)
  21885. utils.each(
  21886. domUtils.getElementsByTagName(me.document, 'table'),
  21887. function(table) {
  21888. if (me.fireEvent('excludetable', table) === true) return
  21889. table.ueTable = new UT(table)
  21890. //trace:3742
  21891. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  21892. //
  21893. // if (domUtils.isEmptyBlock(td) && td !== start) {
  21894. // domUtils.fillNode(me.document, td);
  21895. // if (browser.ie && browser.version == 6) {
  21896. // td.innerHTML = '&nbsp;'
  21897. // }
  21898. // }
  21899. // });
  21900. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  21901. // if (domUtils.isEmptyBlock(th) && th !== start) {
  21902. // domUtils.fillNode(me.document, th);
  21903. // if (browser.ie && browser.version == 6) {
  21904. // th.innerHTML = '&nbsp;'
  21905. // }
  21906. // }
  21907. // });
  21908. table.onmouseover = function() {
  21909. me.fireEvent('tablemouseover', table)
  21910. }
  21911. table.onmousemove = function() {
  21912. me.fireEvent('tablemousemove', table)
  21913. me.options.tableDragable && toggleDragButton(true, this, me)
  21914. utils.defer(function() {
  21915. me.fireEvent('contentchange', 50)
  21916. }, true)
  21917. }
  21918. table.onmouseout = function() {
  21919. me.fireEvent('tablemouseout', table)
  21920. toggleDraggableState(me, false, '', null)
  21921. hideDragLine(me)
  21922. }
  21923. table.onclick = function(evt) {
  21924. evt = me.window.event || evt
  21925. var target = getParentTdOrTh(evt.target || evt.srcElement)
  21926. if (!target) return
  21927. var ut = getUETable(target),
  21928. table = ut.table,
  21929. cellInfo = ut.getCellInfo(target),
  21930. cellsRange,
  21931. rng = me.selection.getRange()
  21932. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  21933. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  21934. // ut.setSelected(cellsRange);
  21935. // return;
  21936. // }
  21937. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  21938. //
  21939. // return;
  21940. // }
  21941. if (inTableSide(table, target, evt, true)) {
  21942. var endTdCol = ut.getCell(
  21943. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex,
  21944. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex
  21945. )
  21946. if (evt.shiftKey && ut.selectedTds.length) {
  21947. if (ut.selectedTds[0] !== endTdCol) {
  21948. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol)
  21949. ut.setSelected(cellsRange)
  21950. } else {
  21951. rng && rng.selectNodeContents(endTdCol).select()
  21952. }
  21953. } else {
  21954. if (target !== endTdCol) {
  21955. cellsRange = ut.getCellsRange(target, endTdCol)
  21956. ut.setSelected(cellsRange)
  21957. } else {
  21958. rng && rng.selectNodeContents(endTdCol).select()
  21959. }
  21960. }
  21961. return
  21962. }
  21963. if (inTableSide(table, target, evt)) {
  21964. var endTdRow = ut.getCell(
  21965. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex,
  21966. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex
  21967. )
  21968. if (evt.shiftKey && ut.selectedTds.length) {
  21969. if (ut.selectedTds[0] !== endTdRow) {
  21970. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow)
  21971. ut.setSelected(cellsRange)
  21972. } else {
  21973. rng && rng.selectNodeContents(endTdRow).select()
  21974. }
  21975. } else {
  21976. if (target !== endTdRow) {
  21977. cellsRange = ut.getCellsRange(target, endTdRow)
  21978. ut.setSelected(cellsRange)
  21979. } else {
  21980. rng && rng.selectNodeContents(endTdRow).select()
  21981. }
  21982. }
  21983. }
  21984. }
  21985. }
  21986. )
  21987. switchBorderColor(me, true)
  21988. })
  21989. domUtils.on(me.document, 'mousemove', mouseMoveEvent)
  21990. domUtils.on(me.document, 'mouseout', function(evt) {
  21991. var target = evt.target || evt.srcElement
  21992. if (target.tagName == 'TABLE') {
  21993. toggleDraggableState(me, false, '', null)
  21994. }
  21995. })
  21996. /**
  21997. * 表格隔行变色
  21998. */
  21999. me.addListener('interlacetable', function(type, table, classList) {
  22000. if (!table) return
  22001. var me = this,
  22002. rows = table.rows,
  22003. len = rows.length,
  22004. getClass = function(list, index, repeat) {
  22005. return list[index]
  22006. ? list[index]
  22007. : repeat
  22008. ? list[index % list.length]
  22009. : ''
  22010. }
  22011. for (var i = 0; i < len; i++) {
  22012. rows[i].className = getClass(
  22013. classList || me.options.classList,
  22014. i,
  22015. true
  22016. )
  22017. }
  22018. })
  22019. me.addListener('uninterlacetable', function(type, table) {
  22020. if (!table) return
  22021. var me = this,
  22022. rows = table.rows,
  22023. classList = me.options.classList,
  22024. len = rows.length
  22025. for (var i = 0; i < len; i++) {
  22026. domUtils.removeClasses(rows[i], classList)
  22027. }
  22028. })
  22029. me.addListener('mousedown', mouseDownEvent)
  22030. me.addListener('mouseup', mouseUpEvent)
  22031. //拖动的时候触发mouseup
  22032. domUtils.on(me.body, 'dragstart', function(evt) {
  22033. mouseUpEvent.call(me, 'dragstart', evt)
  22034. })
  22035. me.addOutputRule(function(root) {
  22036. utils.each(root.getNodesByTagName('div'), function(n) {
  22037. if (n.getAttr('id') == 'ue_tableDragLine') {
  22038. n.parentNode.removeChild(n)
  22039. }
  22040. })
  22041. })
  22042. var currentRowIndex = 0
  22043. me.addListener('mousedown', function() {
  22044. currentRowIndex = 0
  22045. })
  22046. me.addListener('tabkeydown', function() {
  22047. var range = this.selection.getRange(),
  22048. common = range.getCommonAncestor(true, true),
  22049. table = domUtils.findParentByTagName(common, 'table')
  22050. if (table) {
  22051. if (domUtils.findParentByTagName(common, 'caption', true)) {
  22052. var cell = domUtils.getElementsByTagName(table, 'th td')
  22053. if (cell && cell.length) {
  22054. range.setStart(cell[0], 0).setCursor(false, true)
  22055. }
  22056. } else {
  22057. var cell = domUtils.findParentByTagName(common, ['td', 'th'], true),
  22058. ua = getUETable(cell)
  22059. currentRowIndex =
  22060. cell.rowSpan > 1 ? currentRowIndex : ua.getCellInfo(cell).rowIndex
  22061. var nextCell = ua.getTabNextCell(cell, currentRowIndex)
  22062. if (nextCell) {
  22063. if (isEmptyBlock(nextCell)) {
  22064. range.setStart(nextCell, 0).setCursor(false, true)
  22065. } else {
  22066. range.selectNodeContents(nextCell).select()
  22067. }
  22068. } else {
  22069. me.fireEvent('saveScene')
  22070. me.__hasEnterExecCommand = true
  22071. this.execCommand('insertrownext')
  22072. me.__hasEnterExecCommand = false
  22073. range = this.selection.getRange()
  22074. range
  22075. .setStart(table.rows[table.rows.length - 1].cells[0], 0)
  22076. .setCursor()
  22077. me.fireEvent('saveScene')
  22078. }
  22079. }
  22080. return true
  22081. }
  22082. })
  22083. browser.ie &&
  22084. me.addListener('selectionchange', function() {
  22085. toggleDraggableState(this, false, '', null)
  22086. })
  22087. me.addListener('keydown', function(type, evt) {
  22088. var me = this
  22089. //处理在表格的最后一个输入tab产生新的表格
  22090. var keyCode = evt.keyCode || evt.which
  22091. if (keyCode == 8 || keyCode == 46) {
  22092. return
  22093. }
  22094. var notCtrlKey =
  22095. !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey
  22096. notCtrlKey &&
  22097. removeSelectedClass(domUtils.getElementsByTagName(me.body, 'td'))
  22098. var ut = getUETableBySelected(me)
  22099. if (!ut) return
  22100. notCtrlKey && ut.clearSelected()
  22101. })
  22102. me.addListener('beforegetcontent', function() {
  22103. switchBorderColor(this, false)
  22104. browser.ie &&
  22105. utils.each(this.document.getElementsByTagName('caption'), function(
  22106. ci
  22107. ) {
  22108. if (domUtils.isEmptyNode(ci)) {
  22109. ci.innerHTML = '&nbsp;'
  22110. }
  22111. })
  22112. })
  22113. me.addListener('aftergetcontent', function() {
  22114. switchBorderColor(this, true)
  22115. })
  22116. me.addListener('getAllHtml', function() {
  22117. removeSelectedClass(me.document.getElementsByTagName('td'))
  22118. })
  22119. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  22120. me.addListener('fullscreenchanged', function(type, fullscreen) {
  22121. if (!fullscreen) {
  22122. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  22123. tables = domUtils.getElementsByTagName(this.body, 'table')
  22124. utils.each(tables, function(table) {
  22125. if (table.offsetWidth < me.body.offsetWidth) return false
  22126. var tds = domUtils.getElementsByTagName(table, 'td'),
  22127. backWidths = []
  22128. utils.each(tds, function(td) {
  22129. backWidths.push(td.offsetWidth)
  22130. })
  22131. for (var i = 0, td; (td = tds[i]); i++) {
  22132. td.setAttribute('width', Math.floor(backWidths[i] * ratio))
  22133. }
  22134. table.setAttribute(
  22135. 'width',
  22136. Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me)))
  22137. )
  22138. })
  22139. }
  22140. })
  22141. //重写execCommand命令,用于处理框选时的处理
  22142. var oldExecCommand = me.execCommand
  22143. me.execCommand = function(cmd, datatat) {
  22144. var me = this,
  22145. args = arguments
  22146. cmd = cmd.toLowerCase()
  22147. var ut = getUETableBySelected(me),
  22148. tds,
  22149. range = new dom.Range(me.document),
  22150. cmdFun = me.commands[cmd] || UE.commands[cmd],
  22151. result
  22152. if (!cmdFun) return
  22153. if (
  22154. ut &&
  22155. !commands[cmd] &&
  22156. !cmdFun.notNeedUndo &&
  22157. !me.__hasEnterExecCommand
  22158. ) {
  22159. me.__hasEnterExecCommand = true
  22160. me.fireEvent('beforeexeccommand', cmd)
  22161. tds = ut.selectedTds
  22162. var lastState = -2,
  22163. lastValue = -2,
  22164. value,
  22165. state
  22166. for (var i = 0, td; (td = tds[i]); i++) {
  22167. if (isEmptyBlock(td)) {
  22168. range.setStart(td, 0).setCursor(false, true)
  22169. } else {
  22170. range.selectNode(td).select(true)
  22171. }
  22172. state = me.queryCommandState(cmd)
  22173. value = me.queryCommandValue(cmd)
  22174. if (state != -1) {
  22175. if (lastState !== state || lastValue !== value) {
  22176. me._ignoreContentChange = true
  22177. result = oldExecCommand.apply(me, arguments)
  22178. me._ignoreContentChange = false
  22179. }
  22180. lastState = me.queryCommandState(cmd)
  22181. lastValue = me.queryCommandValue(cmd)
  22182. if (domUtils.isEmptyBlock(td)) {
  22183. domUtils.fillNode(me.document, td)
  22184. }
  22185. }
  22186. }
  22187. range
  22188. .setStart(tds[0], 0)
  22189. .shrinkBoundary(true)
  22190. .setCursor(false, true)
  22191. me.fireEvent('contentchange')
  22192. me.fireEvent('afterexeccommand', cmd)
  22193. me.__hasEnterExecCommand = false
  22194. me._selectionChange()
  22195. } else {
  22196. result = oldExecCommand.apply(me, arguments)
  22197. }
  22198. return result
  22199. }
  22200. })
  22201. /**
  22202. * 删除obj的宽高style,改成属性宽高
  22203. * @param obj
  22204. * @param replaceToProperty
  22205. */
  22206. function removeStyleSize(obj, replaceToProperty) {
  22207. removeStyle(obj, 'width', true)
  22208. removeStyle(obj, 'height', true)
  22209. }
  22210. function removeStyle(obj, styleName, replaceToProperty) {
  22211. if (obj.style[styleName]) {
  22212. replaceToProperty &&
  22213. obj.setAttribute(styleName, parseInt(obj.style[styleName], 10))
  22214. obj.style[styleName] = ''
  22215. }
  22216. }
  22217. function getParentTdOrTh(ele) {
  22218. if (ele.tagName == 'TD' || ele.tagName == 'TH') return ele
  22219. var td
  22220. if (
  22221. (td =
  22222. domUtils.findParentByTagName(ele, 'td', true) ||
  22223. domUtils.findParentByTagName(ele, 'th', true))
  22224. )
  22225. return td
  22226. return null
  22227. }
  22228. function isEmptyBlock(node) {
  22229. var reg = new RegExp(domUtils.fillChar, 'g')
  22230. if (
  22231. node[browser.ie ? 'innerText' : 'textContent']
  22232. .replace(/^\s*$/, '')
  22233. .replace(reg, '').length > 0
  22234. ) {
  22235. return 0
  22236. }
  22237. for (var n in dtd.$isNotEmpty) {
  22238. if (node.getElementsByTagName(n).length) {
  22239. return 0
  22240. }
  22241. }
  22242. return 1
  22243. }
  22244. function mouseCoords(evt) {
  22245. if (evt.pageX || evt.pageY) {
  22246. return {x: evt.pageX, y: evt.pageY}
  22247. }
  22248. return {
  22249. x:
  22250. evt.clientX +
  22251. me.document.body.scrollLeft -
  22252. me.document.body.clientLeft,
  22253. y: evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  22254. }
  22255. }
  22256. function mouseMoveEvent(evt) {
  22257. if (isEditorDisabled()) {
  22258. return
  22259. }
  22260. try {
  22261. //普通状态下鼠标移动
  22262. var target = getParentTdOrTh(evt.target || evt.srcElement),
  22263. pos
  22264. //区分用户的行为是拖动还是双击
  22265. if (isInResizeBuffer) {
  22266. me.body.style.webkitUserSelect = 'none'
  22267. if (
  22268. Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell ||
  22269. Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell
  22270. ) {
  22271. clearTableDragTimer()
  22272. isInResizeBuffer = false
  22273. singleClickState = 0
  22274. //drag action
  22275. tableBorderDrag(evt)
  22276. }
  22277. }
  22278. //修改单元格大小时的鼠标移动
  22279. if (onDrag && dragTd) {
  22280. singleClickState = 0
  22281. me.body.style.webkitUserSelect = 'none'
  22282. me.selection
  22283. .getNative()
  22284. [browser.ie9below ? 'empty' : 'removeAllRanges']()
  22285. pos = mouseCoords(evt)
  22286. toggleDraggableState(me, true, onDrag, pos, target)
  22287. if (onDrag == 'h') {
  22288. dragLine.style.left = getPermissionX(dragTd, evt) + 'px'
  22289. } else if (onDrag == 'v') {
  22290. dragLine.style.top = getPermissionY(dragTd, evt) + 'px'
  22291. }
  22292. return
  22293. }
  22294. //当鼠标处于table上时,修改移动过程中的光标状态
  22295. if (target) {
  22296. //针对使用table作为容器的组件不触发拖拽效果
  22297. if (me.fireEvent('excludetable', target) === true) return
  22298. pos = mouseCoords(evt)
  22299. var state = getRelation(target, pos),
  22300. table = domUtils.findParentByTagName(target, 'table', true)
  22301. if (inTableSide(table, target, evt, true)) {
  22302. if (me.fireEvent('excludetable', table) === true) return
  22303. me.body.style.cursor =
  22304. 'url(' + me.options.cursorpath + 'h.png),pointer'
  22305. } else if (inTableSide(table, target, evt)) {
  22306. if (me.fireEvent('excludetable', table) === true) return
  22307. me.body.style.cursor =
  22308. 'url(' + me.options.cursorpath + 'v.png),pointer'
  22309. } else {
  22310. me.body.style.cursor = 'text'
  22311. var curCell = target
  22312. if (/\d/.test(state)) {
  22313. state = state.replace(/\d/, '')
  22314. target = getUETable(target).getPreviewCell(target, state == 'v')
  22315. }
  22316. //位于第一行的顶部或者第一列的左边时不可拖动
  22317. toggleDraggableState(
  22318. me,
  22319. target ? !!state : false,
  22320. target ? state : '',
  22321. pos,
  22322. target
  22323. )
  22324. }
  22325. } else {
  22326. toggleDragButton(false, table, me)
  22327. }
  22328. } catch (e) {
  22329. showError(e)
  22330. }
  22331. }
  22332. var dragButtonTimer
  22333. function toggleDragButton(show, table, editor) {
  22334. if (!show) {
  22335. if (dragOver) return
  22336. dragButtonTimer = setTimeout(function() {
  22337. !dragOver &&
  22338. dragButton &&
  22339. dragButton.parentNode &&
  22340. dragButton.parentNode.removeChild(dragButton)
  22341. }, 2000)
  22342. } else {
  22343. createDragButton(table, editor)
  22344. }
  22345. }
  22346. function createDragButton(table, editor) {
  22347. var pos = domUtils.getXY(table),
  22348. doc = table.ownerDocument
  22349. if (dragButton && dragButton.parentNode) return dragButton
  22350. dragButton = doc.createElement('div')
  22351. dragButton.contentEditable = false
  22352. dragButton.innerHTML = ''
  22353. dragButton.style.cssText =
  22354. 'width:15px;height:15px;background-image:url(' +
  22355. editor.options.UEDITOR_HOME_URL +
  22356. 'dialogs/table/dragicon.png);position: absolute;cursor:move;top:' +
  22357. (pos.y - 15) +
  22358. 'px;left:' +
  22359. pos.x +
  22360. 'px;'
  22361. domUtils.unSelectable(dragButton)
  22362. dragButton.onmouseover = function(evt) {
  22363. dragOver = true
  22364. }
  22365. dragButton.onmouseout = function(evt) {
  22366. dragOver = false
  22367. }
  22368. domUtils.on(dragButton, 'click', function(type, evt) {
  22369. doClick(evt, this)
  22370. })
  22371. domUtils.on(dragButton, 'dblclick', function(type, evt) {
  22372. doDblClick(evt)
  22373. })
  22374. domUtils.on(dragButton, 'dragstart', function(type, evt) {
  22375. domUtils.preventDefault(evt)
  22376. })
  22377. var timer
  22378. function doClick(evt, button) {
  22379. // 部分浏览器下需要清理
  22380. clearTimeout(timer)
  22381. timer = setTimeout(function() {
  22382. editor.fireEvent('tableClicked', table, button)
  22383. }, 300)
  22384. }
  22385. function doDblClick(evt) {
  22386. clearTimeout(timer)
  22387. var ut = getUETable(table),
  22388. start = table.rows[0].cells[0],
  22389. end = ut.getLastCell(),
  22390. range = ut.getCellsRange(start, end)
  22391. editor.selection
  22392. .getRange()
  22393. .setStart(start, 0)
  22394. .setCursor(false, true)
  22395. ut.setSelected(range)
  22396. }
  22397. doc.body.appendChild(dragButton)
  22398. }
  22399. // function inPosition(table, pos) {
  22400. // var tablePos = domUtils.getXY(table),
  22401. // width = table.offsetWidth,
  22402. // height = table.offsetHeight;
  22403. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  22404. // return "topLeft";
  22405. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  22406. // return "bottomRight";
  22407. // }
  22408. // }
  22409. function inTableSide(table, cell, evt, top) {
  22410. var pos = mouseCoords(evt),
  22411. state = getRelation(cell, pos)
  22412. if (top) {
  22413. var caption = table.getElementsByTagName('caption')[0],
  22414. capHeight = caption ? caption.offsetHeight : 0
  22415. return state == 'v1' && pos.y - domUtils.getXY(table).y - capHeight < 8
  22416. } else {
  22417. return state == 'h1' && pos.x - domUtils.getXY(table).x < 8
  22418. }
  22419. }
  22420. /**
  22421. * 获取拖动时允许的X轴坐标
  22422. * @param dragTd
  22423. * @param evt
  22424. */
  22425. function getPermissionX(dragTd, evt) {
  22426. var ut = getUETable(dragTd)
  22427. if (ut) {
  22428. var preTd = ut.getSameEndPosCells(dragTd, 'x')[0],
  22429. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  22430. mouseX = mouseCoords(evt).x,
  22431. left =
  22432. (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  22433. right = nextTd
  22434. ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20
  22435. : me.body.offsetWidth + 5 ||
  22436. parseInt(domUtils.getComputedStyle(me.body, 'width'), 10)
  22437. left += cellMinWidth
  22438. right -= cellMinWidth
  22439. return mouseX < left ? left : mouseX > right ? right : mouseX
  22440. }
  22441. }
  22442. /**
  22443. * 获取拖动时允许的Y轴坐标
  22444. */
  22445. function getPermissionY(dragTd, evt) {
  22446. try {
  22447. var top = domUtils.getXY(dragTd).y,
  22448. mousePosY = mouseCoords(evt).y
  22449. return mousePosY < top ? top : mousePosY
  22450. } catch (e) {
  22451. showError(e)
  22452. }
  22453. }
  22454. /**
  22455. * 移动状态切换
  22456. */
  22457. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  22458. try {
  22459. editor.body.style.cursor =
  22460. dir == 'h' ? 'col-resize' : dir == 'v' ? 'row-resize' : 'text'
  22461. if (browser.ie) {
  22462. if (dir && !mousedown && !getUETableBySelected(editor)) {
  22463. getDragLine(editor, editor.document)
  22464. showDragLineAt(dir, cell)
  22465. } else {
  22466. hideDragLine(editor)
  22467. }
  22468. }
  22469. onBorder = draggable
  22470. } catch (e) {
  22471. showError(e)
  22472. }
  22473. }
  22474. /**
  22475. * 获取与UETable相关的resize line
  22476. * @param uetable UETable对象
  22477. */
  22478. function getResizeLineByUETable() {
  22479. var lineId = '_UETableResizeLine',
  22480. line = this.document.getElementById(lineId)
  22481. if (!line) {
  22482. line = this.document.createElement('div')
  22483. line.id = lineId
  22484. line.contnetEditable = false
  22485. line.setAttribute('unselectable', 'on')
  22486. var styles = {
  22487. width: 2 * cellBorderWidth + 1 + 'px',
  22488. position: 'absolute',
  22489. 'z-index': 100000,
  22490. cursor: 'col-resize',
  22491. background: 'red',
  22492. display: 'none'
  22493. }
  22494. //切换状态
  22495. line.onmouseout = function() {
  22496. this.style.display = 'none'
  22497. }
  22498. utils.extend(line.style, styles)
  22499. this.document.body.appendChild(line)
  22500. }
  22501. return line
  22502. }
  22503. /**
  22504. * 更新resize-line
  22505. */
  22506. function updateResizeLine(cell, uetable) {
  22507. var line = getResizeLineByUETable.call(this),
  22508. table = uetable.table,
  22509. styles = {
  22510. top: domUtils.getXY(table).y + 'px',
  22511. left:
  22512. domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + 'px',
  22513. display: 'block',
  22514. height: table.offsetHeight + 'px'
  22515. }
  22516. utils.extend(line.style, styles)
  22517. }
  22518. /**
  22519. * 显示resize-line
  22520. */
  22521. function showResizeLine(cell) {
  22522. var uetable = getUETable(cell)
  22523. updateResizeLine.call(this, cell, uetable)
  22524. }
  22525. /**
  22526. * 获取鼠标与当前单元格的相对位置
  22527. * @param ele
  22528. * @param mousePos
  22529. */
  22530. function getRelation(ele, mousePos) {
  22531. var elePos = domUtils.getXY(ele)
  22532. if (!elePos) {
  22533. return ''
  22534. }
  22535. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  22536. return 'h'
  22537. }
  22538. if (mousePos.x - elePos.x < cellBorderWidth) {
  22539. return 'h1'
  22540. }
  22541. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  22542. return 'v'
  22543. }
  22544. if (mousePos.y - elePos.y < cellBorderWidth) {
  22545. return 'v1'
  22546. }
  22547. return ''
  22548. }
  22549. function mouseDownEvent(type, evt) {
  22550. if (isEditorDisabled()) {
  22551. return
  22552. }
  22553. userActionStatus = {
  22554. x: evt.clientX,
  22555. y: evt.clientY
  22556. }
  22557. //右键菜单单独处理
  22558. if (evt.button == 2) {
  22559. var ut = getUETableBySelected(me),
  22560. flag = false
  22561. if (ut) {
  22562. var td = getTargetTd(me, evt)
  22563. utils.each(ut.selectedTds, function(ti) {
  22564. if (ti === td) {
  22565. flag = true
  22566. }
  22567. })
  22568. if (!flag) {
  22569. removeSelectedClass(domUtils.getElementsByTagName(me.body, 'th td'))
  22570. ut.clearSelected()
  22571. } else {
  22572. td = ut.selectedTds[0]
  22573. setTimeout(function() {
  22574. me.selection
  22575. .getRange()
  22576. .setStart(td, 0)
  22577. .setCursor(false, true)
  22578. }, 0)
  22579. }
  22580. }
  22581. } else {
  22582. tableClickHander(evt)
  22583. }
  22584. }
  22585. //清除表格的计时器
  22586. function clearTableTimer() {
  22587. tabTimer && clearTimeout(tabTimer)
  22588. tabTimer = null
  22589. }
  22590. //双击收缩
  22591. function tableDbclickHandler(evt) {
  22592. singleClickState = 0
  22593. evt = evt || me.window.event
  22594. var target = getParentTdOrTh(evt.target || evt.srcElement)
  22595. if (target) {
  22596. var h
  22597. if ((h = getRelation(target, mouseCoords(evt)))) {
  22598. hideDragLine(me)
  22599. if (h == 'h1') {
  22600. h = 'h'
  22601. if (
  22602. inTableSide(
  22603. domUtils.findParentByTagName(target, 'table'),
  22604. target,
  22605. evt
  22606. )
  22607. ) {
  22608. me.execCommand('adaptbywindow')
  22609. } else {
  22610. target = getUETable(target).getPreviewCell(target)
  22611. if (target) {
  22612. var rng = me.selection.getRange()
  22613. rng.selectNodeContents(target).setCursor(true, true)
  22614. }
  22615. }
  22616. }
  22617. if (h == 'h') {
  22618. var ut = getUETable(target),
  22619. table = ut.table,
  22620. cells = getCellsByMoveBorder(target, table, true)
  22621. cells = extractArray(cells, 'left')
  22622. ut.width = ut.offsetWidth
  22623. var oldWidth = [],
  22624. newWidth = []
  22625. utils.each(cells, function(cell) {
  22626. oldWidth.push(cell.offsetWidth)
  22627. })
  22628. utils.each(cells, function(cell) {
  22629. cell.removeAttribute('width')
  22630. })
  22631. window.setTimeout(function() {
  22632. //是否允许改变
  22633. var changeable = true
  22634. utils.each(cells, function(cell, index) {
  22635. var width = cell.offsetWidth
  22636. if (width > oldWidth[index]) {
  22637. changeable = false
  22638. return false
  22639. }
  22640. newWidth.push(width)
  22641. })
  22642. var change = changeable ? newWidth : oldWidth
  22643. utils.each(cells, function(cell, index) {
  22644. cell.width = change[index] - getTabcellSpace()
  22645. })
  22646. }, 0)
  22647. // minWidth -= cellMinWidth;
  22648. //
  22649. // table.removeAttribute("width");
  22650. // utils.each(cells, function (cell) {
  22651. // cell.style.width = "";
  22652. // cell.width -= minWidth;
  22653. // });
  22654. }
  22655. }
  22656. }
  22657. }
  22658. function tableClickHander(evt) {
  22659. removeSelectedClass(domUtils.getElementsByTagName(me.body, 'td th'))
  22660. //trace:3113
  22661. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  22662. utils.each(me.document.getElementsByTagName('table'), function(t) {
  22663. t.ueTable = null
  22664. })
  22665. startTd = getTargetTd(me, evt)
  22666. if (!startTd) return
  22667. var table = domUtils.findParentByTagName(startTd, 'table', true)
  22668. ut = getUETable(table)
  22669. ut && ut.clearSelected()
  22670. //判断当前鼠标状态
  22671. if (!onBorder) {
  22672. me.document.body.style.webkitUserSelect = ''
  22673. mousedown = true
  22674. me.addListener('mouseover', mouseOverEvent)
  22675. } else {
  22676. //边框上的动作处理
  22677. borderActionHandler(evt)
  22678. }
  22679. }
  22680. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  22681. function borderActionHandler(evt) {
  22682. if (browser.ie) {
  22683. evt = reconstruct(evt)
  22684. }
  22685. clearTableDragTimer()
  22686. //是否正在等待resize的缓冲中
  22687. isInResizeBuffer = true
  22688. tableDragTimer = setTimeout(function() {
  22689. tableBorderDrag(evt)
  22690. }, dblclickTime)
  22691. }
  22692. function extractArray(originArr, key) {
  22693. var result = [],
  22694. tmp = null
  22695. for (var i = 0, len = originArr.length; i < len; i++) {
  22696. tmp = originArr[i][key]
  22697. if (tmp) {
  22698. result.push(tmp)
  22699. }
  22700. }
  22701. return result
  22702. }
  22703. function clearTableDragTimer() {
  22704. tableDragTimer && clearTimeout(tableDragTimer)
  22705. tableDragTimer = null
  22706. }
  22707. function reconstruct(obj) {
  22708. var attrs = [
  22709. 'pageX',
  22710. 'pageY',
  22711. 'clientX',
  22712. 'clientY',
  22713. 'srcElement',
  22714. 'target'
  22715. ],
  22716. newObj = {}
  22717. if (obj) {
  22718. for (var i = 0, key, val; (key = attrs[i]); i++) {
  22719. val = obj[key]
  22720. val && (newObj[key] = val)
  22721. }
  22722. }
  22723. return newObj
  22724. }
  22725. //边框拖动
  22726. function tableBorderDrag(evt) {
  22727. isInResizeBuffer = false
  22728. startTd = evt.target || evt.srcElement
  22729. if (!startTd) return
  22730. var state = getRelation(startTd, mouseCoords(evt))
  22731. if (/\d/.test(state)) {
  22732. state = state.replace(/\d/, '')
  22733. startTd = getUETable(startTd).getPreviewCell(startTd, state == 'v')
  22734. }
  22735. hideDragLine(me)
  22736. getDragLine(me, me.document)
  22737. me.fireEvent('saveScene')
  22738. showDragLineAt(state, startTd)
  22739. mousedown = true
  22740. //拖动开始
  22741. onDrag = state
  22742. dragTd = startTd
  22743. }
  22744. function mouseUpEvent(type, evt) {
  22745. if (isEditorDisabled()) {
  22746. return
  22747. }
  22748. clearTableDragTimer()
  22749. isInResizeBuffer = false
  22750. if (onBorder) {
  22751. singleClickState = ++singleClickState % 3
  22752. userActionStatus = {
  22753. x: evt.clientX,
  22754. y: evt.clientY
  22755. }
  22756. tableResizeTimer = setTimeout(function() {
  22757. singleClickState > 0 && singleClickState--
  22758. }, dblclickTime)
  22759. if (singleClickState === 2) {
  22760. singleClickState = 0
  22761. tableDbclickHandler(evt)
  22762. return
  22763. }
  22764. }
  22765. if (evt.button == 2) return
  22766. var me = this
  22767. //清除表格上原生跨选问题
  22768. var range = me.selection.getRange(),
  22769. start = domUtils.findParentByTagName(
  22770. range.startContainer,
  22771. 'table',
  22772. true
  22773. ),
  22774. end = domUtils.findParentByTagName(range.endContainer, 'table', true)
  22775. if (start || end) {
  22776. if (start === end) {
  22777. start = domUtils.findParentByTagName(
  22778. range.startContainer,
  22779. ['td', 'th', 'caption'],
  22780. true
  22781. )
  22782. end = domUtils.findParentByTagName(
  22783. range.endContainer,
  22784. ['td', 'th', 'caption'],
  22785. true
  22786. )
  22787. if (start !== end) {
  22788. me.selection.clearRange()
  22789. }
  22790. } else {
  22791. me.selection.clearRange()
  22792. }
  22793. }
  22794. mousedown = false
  22795. me.document.body.style.webkitUserSelect = ''
  22796. //拖拽状态下的mouseUP
  22797. if (onDrag && dragTd) {
  22798. me.selection
  22799. .getNative()
  22800. [browser.ie9below ? 'empty' : 'removeAllRanges']()
  22801. singleClickState = 0
  22802. dragLine = me.document.getElementById('ue_tableDragLine')
  22803. // trace 3973
  22804. if (dragLine) {
  22805. var dragTdPos = domUtils.getXY(dragTd),
  22806. dragLinePos = domUtils.getXY(dragLine)
  22807. switch (onDrag) {
  22808. case 'h':
  22809. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x)
  22810. break
  22811. case 'v':
  22812. changeRowHeight(
  22813. dragTd,
  22814. dragLinePos.y - dragTdPos.y - dragTd.offsetHeight
  22815. )
  22816. break
  22817. default:
  22818. }
  22819. onDrag = ''
  22820. dragTd = null
  22821. hideDragLine(me)
  22822. me.fireEvent('saveScene')
  22823. return
  22824. }
  22825. }
  22826. //正常状态下的mouseup
  22827. if (!startTd) {
  22828. var target = domUtils.findParentByTagName(
  22829. evt.target || evt.srcElement,
  22830. 'td',
  22831. true
  22832. )
  22833. if (!target)
  22834. target = domUtils.findParentByTagName(
  22835. evt.target || evt.srcElement,
  22836. 'th',
  22837. true
  22838. )
  22839. if (target && (target.tagName == 'TD' || target.tagName == 'TH')) {
  22840. if (me.fireEvent('excludetable', target) === true) return
  22841. range = new dom.Range(me.document)
  22842. range.setStart(target, 0).setCursor(false, true)
  22843. }
  22844. } else {
  22845. var ut = getUETable(startTd),
  22846. cell = ut ? ut.selectedTds[0] : null
  22847. if (cell) {
  22848. range = new dom.Range(me.document)
  22849. if (domUtils.isEmptyBlock(cell)) {
  22850. range.setStart(cell, 0).setCursor(false, true)
  22851. } else {
  22852. range
  22853. .selectNodeContents(cell)
  22854. .shrinkBoundary()
  22855. .setCursor(false, true)
  22856. }
  22857. } else {
  22858. range = me.selection.getRange().shrinkBoundary()
  22859. if (!range.collapsed) {
  22860. var start = domUtils.findParentByTagName(
  22861. range.startContainer,
  22862. ['td', 'th'],
  22863. true
  22864. ),
  22865. end = domUtils.findParentByTagName(
  22866. range.endContainer,
  22867. ['td', 'th'],
  22868. true
  22869. )
  22870. //在table里边的不能清除
  22871. if (
  22872. (start && !end) ||
  22873. (!start && end) ||
  22874. (start && end && start !== end)
  22875. ) {
  22876. range.setCursor(false, true)
  22877. }
  22878. }
  22879. }
  22880. startTd = null
  22881. me.removeListener('mouseover', mouseOverEvent)
  22882. }
  22883. me._selectionChange(250, evt)
  22884. }
  22885. function mouseOverEvent(type, evt) {
  22886. if (isEditorDisabled()) {
  22887. return
  22888. }
  22889. var me = this,
  22890. tar = evt.target || evt.srcElement
  22891. currentTd =
  22892. domUtils.findParentByTagName(tar, 'td', true) ||
  22893. domUtils.findParentByTagName(tar, 'th', true)
  22894. //需要判断两个TD是否位于同一个表格内
  22895. if (
  22896. startTd &&
  22897. currentTd &&
  22898. ((startTd.tagName == 'TD' && currentTd.tagName == 'TD') ||
  22899. (startTd.tagName == 'TH' && currentTd.tagName == 'TH')) &&
  22900. domUtils.findParentByTagName(startTd, 'table') ==
  22901. domUtils.findParentByTagName(currentTd, 'table')
  22902. ) {
  22903. var ut = getUETable(currentTd)
  22904. if (startTd != currentTd) {
  22905. me.document.body.style.webkitUserSelect = 'none'
  22906. me.selection
  22907. .getNative()
  22908. [browser.ie9below ? 'empty' : 'removeAllRanges']()
  22909. var range = ut.getCellsRange(startTd, currentTd)
  22910. ut.setSelected(range)
  22911. } else {
  22912. me.document.body.style.webkitUserSelect = ''
  22913. ut.clearSelected()
  22914. }
  22915. }
  22916. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  22917. }
  22918. function setCellHeight(cell, height, backHeight) {
  22919. var lineHight = parseInt(
  22920. domUtils.getComputedStyle(cell, 'line-height'),
  22921. 10
  22922. ),
  22923. tmpHeight = backHeight + height
  22924. height = tmpHeight < lineHight ? lineHight : tmpHeight
  22925. if (cell.style.height) cell.style.height = ''
  22926. cell.rowSpan == 1
  22927. ? cell.setAttribute('height', height)
  22928. : cell.removeAttribute && cell.removeAttribute('height')
  22929. }
  22930. function getWidth(cell) {
  22931. if (!cell) return 0
  22932. return parseInt(domUtils.getComputedStyle(cell, 'width'), 10)
  22933. }
  22934. function changeColWidth(cell, changeValue) {
  22935. var ut = getUETable(cell)
  22936. if (ut) {
  22937. //根据当前移动的边框获取相关的单元格
  22938. var table = ut.table,
  22939. cells = getCellsByMoveBorder(cell, table)
  22940. table.style.width = ''
  22941. table.removeAttribute('width')
  22942. //修正改变量
  22943. changeValue = correctChangeValue(changeValue, cell, cells)
  22944. if (cell.nextSibling) {
  22945. var i = 0
  22946. utils.each(cells, function(cellGroup) {
  22947. cellGroup.left.width = +cellGroup.left.width + changeValue
  22948. cellGroup.right &&
  22949. (cellGroup.right.width = +cellGroup.right.width - changeValue)
  22950. })
  22951. } else {
  22952. utils.each(cells, function(cellGroup) {
  22953. cellGroup.left.width -= -changeValue
  22954. })
  22955. }
  22956. }
  22957. }
  22958. function isEditorDisabled() {
  22959. return me.body.contentEditable === 'false'
  22960. }
  22961. function changeRowHeight(td, changeValue) {
  22962. if (Math.abs(changeValue) < 10) return
  22963. var ut = getUETable(td)
  22964. if (ut) {
  22965. var cells = ut.getSameEndPosCells(td, 'y'),
  22966. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  22967. backHeight = cells[0] ? cells[0].offsetHeight : 0
  22968. for (var i = 0, cell; (cell = cells[i++]); ) {
  22969. setCellHeight(cell, changeValue, backHeight)
  22970. }
  22971. }
  22972. }
  22973. /**
  22974. * 获取调整单元格大小的相关单元格
  22975. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  22976. */
  22977. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  22978. if (!table) {
  22979. table = domUtils.findParentByTagName(cell, 'table')
  22980. }
  22981. if (!table) {
  22982. return null
  22983. }
  22984. //获取到该单元格所在行的序列号
  22985. var index = domUtils.getNodeIndex(cell),
  22986. temp = cell,
  22987. rows = table.rows,
  22988. colIndex = 0
  22989. while (temp) {
  22990. //获取到当前单元格在未发生单元格合并时的序列
  22991. if (temp.nodeType === 1) {
  22992. colIndex += temp.colSpan || 1
  22993. }
  22994. temp = temp.previousSibling
  22995. }
  22996. temp = null
  22997. //记录想关的单元格
  22998. var borderCells = []
  22999. utils.each(rows, function(tabRow) {
  23000. var cells = tabRow.cells,
  23001. currIndex = 0
  23002. utils.each(cells, function(tabCell) {
  23003. currIndex += tabCell.colSpan || 1
  23004. if (currIndex === colIndex) {
  23005. borderCells.push({
  23006. left: tabCell,
  23007. right: tabCell.nextSibling || null
  23008. })
  23009. return false
  23010. } else if (currIndex > colIndex) {
  23011. if (isContainMergeCell) {
  23012. borderCells.push({
  23013. left: tabCell
  23014. })
  23015. }
  23016. return false
  23017. }
  23018. })
  23019. })
  23020. return borderCells
  23021. }
  23022. /**
  23023. * 通过给定的单元格集合获取最小的单元格width
  23024. */
  23025. function getMinWidthByTableCells(cells) {
  23026. var minWidth = Number.MAX_VALUE
  23027. for (var i = 0, curCell; (curCell = cells[i]); i++) {
  23028. minWidth = Math.min(
  23029. minWidth,
  23030. curCell.width || getTableCellWidth(curCell)
  23031. )
  23032. }
  23033. return minWidth
  23034. }
  23035. function correctChangeValue(changeValue, relatedCell, cells) {
  23036. //为单元格的paading预留空间
  23037. changeValue -= getTabcellSpace()
  23038. if (changeValue < 0) {
  23039. return 0
  23040. }
  23041. changeValue -= getTableCellWidth(relatedCell)
  23042. //确定方向
  23043. var direction = changeValue < 0 ? 'left' : 'right'
  23044. changeValue = Math.abs(changeValue)
  23045. //只关心非最后一个单元格就可以
  23046. utils.each(cells, function(cellGroup) {
  23047. var curCell = cellGroup[direction]
  23048. //为单元格保留最小空间
  23049. if (curCell) {
  23050. changeValue = Math.min(
  23051. changeValue,
  23052. getTableCellWidth(curCell) - cellMinWidth
  23053. )
  23054. }
  23055. })
  23056. //修正越界
  23057. changeValue = changeValue < 0 ? 0 : changeValue
  23058. return direction === 'left' ? -changeValue : changeValue
  23059. }
  23060. function getTableCellWidth(cell) {
  23061. var width = 0,
  23062. //偏移纠正量
  23063. offset = 0,
  23064. width = cell.offsetWidth - getTabcellSpace()
  23065. //最后一个节点纠正一下
  23066. if (!cell.nextSibling) {
  23067. width -= getTableCellOffset(cell)
  23068. }
  23069. width = width < 0 ? 0 : width
  23070. try {
  23071. cell.width = width
  23072. } catch (e) {}
  23073. return width
  23074. }
  23075. /**
  23076. * 获取单元格所在表格的最末单元格的偏移量
  23077. */
  23078. function getTableCellOffset(cell) {
  23079. tab = domUtils.findParentByTagName(cell, 'table', false)
  23080. if (tab.offsetVal === undefined) {
  23081. var prev = cell.previousSibling
  23082. if (prev) {
  23083. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  23084. tab.offsetVal =
  23085. cell.offsetWidth - prev.offsetWidth === UT.borderWidth
  23086. ? UT.borderWidth
  23087. : 0
  23088. } else {
  23089. tab.offsetVal = 0
  23090. }
  23091. }
  23092. return tab.offsetVal
  23093. }
  23094. function getTabcellSpace() {
  23095. if (UT.tabcellSpace === undefined) {
  23096. var cell = null,
  23097. tab = me.document.createElement('table'),
  23098. tbody = me.document.createElement('tbody'),
  23099. trow = me.document.createElement('tr'),
  23100. tabcell = me.document.createElement('td'),
  23101. mirror = null
  23102. tabcell.style.cssText = 'border: 0;'
  23103. tabcell.width = 1
  23104. trow.appendChild(tabcell)
  23105. trow.appendChild((mirror = tabcell.cloneNode(false)))
  23106. tbody.appendChild(trow)
  23107. tab.appendChild(tbody)
  23108. tab.style.cssText = 'visibility: hidden;'
  23109. me.body.appendChild(tab)
  23110. UT.paddingSpace = tabcell.offsetWidth - 1
  23111. var tmpTabWidth = tab.offsetWidth
  23112. tabcell.style.cssText = ''
  23113. mirror.style.cssText = ''
  23114. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3
  23115. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth
  23116. me.body.removeChild(tab)
  23117. }
  23118. getTabcellSpace = function() {
  23119. return UT.tabcellSpace
  23120. }
  23121. return UT.tabcellSpace
  23122. }
  23123. function getDragLine(editor, doc) {
  23124. if (mousedown) return
  23125. dragLine = editor.document.createElement('div')
  23126. domUtils.setAttributes(dragLine, {
  23127. id: 'ue_tableDragLine',
  23128. unselectable: 'on',
  23129. contenteditable: false,
  23130. onresizestart: 'return false',
  23131. ondragstart: 'return false',
  23132. onselectstart: 'return false',
  23133. style:
  23134. 'background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)'
  23135. })
  23136. editor.body.appendChild(dragLine)
  23137. }
  23138. function hideDragLine(editor) {
  23139. if (mousedown) return
  23140. var line
  23141. while ((line = editor.document.getElementById('ue_tableDragLine'))) {
  23142. domUtils.remove(line)
  23143. }
  23144. }
  23145. /**
  23146. * 依据state(v|h)在cell位置显示横线
  23147. * @param state
  23148. * @param cell
  23149. */
  23150. function showDragLineAt(state, cell) {
  23151. if (!cell) return
  23152. var table = domUtils.findParentByTagName(cell, 'table'),
  23153. caption = table.getElementsByTagName('caption'),
  23154. width = table.offsetWidth,
  23155. height =
  23156. table.offsetHeight -
  23157. (caption.length > 0 ? caption[0].offsetHeight : 0),
  23158. tablePos = domUtils.getXY(table),
  23159. cellPos = domUtils.getXY(cell),
  23160. css
  23161. switch (state) {
  23162. case 'h':
  23163. css =
  23164. 'height:' +
  23165. height +
  23166. 'px;top:' +
  23167. (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) +
  23168. 'px;left:' +
  23169. (cellPos.x + cell.offsetWidth)
  23170. dragLine.style.cssText =
  23171. css +
  23172. 'px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)'
  23173. break
  23174. case 'v':
  23175. css =
  23176. 'width:' +
  23177. width +
  23178. 'px;left:' +
  23179. tablePos.x +
  23180. 'px;top:' +
  23181. (cellPos.y + cell.offsetHeight)
  23182. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  23183. dragLine.style.cssText =
  23184. css +
  23185. 'px;overflow:hidden;position: absolute;display:block;background-color:blue;height:1px;border:0;color:blue;opacity:.2;filter:alpha(opacity=20)'
  23186. break
  23187. default:
  23188. }
  23189. }
  23190. /**
  23191. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  23192. * @param editor
  23193. * @param flag
  23194. */
  23195. function switchBorderColor(editor, flag) {
  23196. var tableArr = domUtils.getElementsByTagName(editor.body, 'table'),
  23197. color
  23198. for (var i = 0, node; (node = tableArr[i++]); ) {
  23199. var td = domUtils.getElementsByTagName(node, 'td')
  23200. if (td[0]) {
  23201. if (flag) {
  23202. color = td[0].style.borderColor.replace(/\s/g, '')
  23203. if (/(#ffffff)|(rgb\(255,255,255\))/gi.test(color))
  23204. domUtils.addClass(node, 'noBorderTable')
  23205. } else {
  23206. domUtils.removeClasses(node, 'noBorderTable')
  23207. }
  23208. }
  23209. }
  23210. }
  23211. function getTableWidth(editor, needIEHack, defaultValue) {
  23212. var body = editor.body
  23213. return (
  23214. body.offsetWidth -
  23215. (needIEHack
  23216. ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2
  23217. : 0) -
  23218. defaultValue.tableBorder * 2 -
  23219. (editor.options.offsetWidth || 0)
  23220. )
  23221. }
  23222. /**
  23223. * 获取当前拖动的单元格
  23224. */
  23225. function getTargetTd(editor, evt) {
  23226. var target = domUtils.findParentByTagName(
  23227. evt.target || evt.srcElement,
  23228. ['td', 'th'],
  23229. true
  23230. ),
  23231. dir = null
  23232. if (!target) {
  23233. return null
  23234. }
  23235. dir = getRelation(target, mouseCoords(evt))
  23236. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  23237. if (!target) {
  23238. return null
  23239. }
  23240. if (dir === 'h1' && target.previousSibling) {
  23241. var position = domUtils.getXY(target),
  23242. cellWidth = target.offsetWidth
  23243. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  23244. target = target.previousSibling
  23245. }
  23246. } else if (dir === 'v1' && target.parentNode.previousSibling) {
  23247. var position = domUtils.getXY(target),
  23248. cellHeight = target.offsetHeight
  23249. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  23250. target = target.parentNode.previousSibling.firstChild
  23251. }
  23252. }
  23253. //排除了非td内部以及用于代码高亮部分的td
  23254. return target && !(editor.fireEvent('excludetable', target) === true)
  23255. ? target
  23256. : null
  23257. }
  23258. }
  23259. // plugins/table.sort.js
  23260. /**
  23261. * Created with JetBrains PhpStorm.
  23262. * User: Jinqn
  23263. * Date: 13-10-12
  23264. * Time: 上午10:20
  23265. * To change this template use File | Settings | File Templates.
  23266. */
  23267. UE.UETable.prototype.sortTable = function(sortByCellIndex, compareFn) {
  23268. var table = this.table,
  23269. rows = table.rows,
  23270. trArray = [],
  23271. flag = rows[0].cells[0].tagName === 'TH',
  23272. lastRowIndex = 0
  23273. if (this.selectedTds.length) {
  23274. var range = this.cellsRange,
  23275. len = range.endRowIndex + 1
  23276. for (var i = range.beginRowIndex; i < len; i++) {
  23277. trArray[i] = rows[i]
  23278. }
  23279. trArray.splice(0, range.beginRowIndex)
  23280. lastRowIndex =
  23281. range.endRowIndex + 1 === this.rowsNum ? 0 : range.endRowIndex + 1
  23282. } else {
  23283. for (var i = 0, len = rows.length; i < len; i++) {
  23284. trArray[i] = rows[i]
  23285. }
  23286. }
  23287. var Fn = {
  23288. reversecurrent: function(td1, td2) {
  23289. return 1
  23290. },
  23291. orderbyasc: function(td1, td2) {
  23292. var value1 = td1.innerText || td1.textContent,
  23293. value2 = td2.innerText || td2.textContent
  23294. return value1.localeCompare(value2)
  23295. },
  23296. reversebyasc: function(td1, td2) {
  23297. var value1 = td1.innerHTML,
  23298. value2 = td2.innerHTML
  23299. return value2.localeCompare(value1)
  23300. },
  23301. orderbynum: function(td1, td2) {
  23302. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  23303. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/)
  23304. if (value1) value1 = +value1[0]
  23305. if (value2) value2 = +value2[0]
  23306. return (value1 || 0) - (value2 || 0)
  23307. },
  23308. reversebynum: function(td1, td2) {
  23309. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  23310. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/)
  23311. if (value1) value1 = +value1[0]
  23312. if (value2) value2 = +value2[0]
  23313. return (value2 || 0) - (value1 || 0)
  23314. }
  23315. }
  23316. //对表格设置排序的标记data-sort-type
  23317. table.setAttribute(
  23318. 'data-sort-type',
  23319. compareFn && typeof compareFn === 'string' && Fn[compareFn]
  23320. ? compareFn
  23321. : ''
  23322. )
  23323. //th不参与排序
  23324. flag && trArray.splice(0, 1)
  23325. trArray = utils.sort(trArray, function(tr1, tr2) {
  23326. var result
  23327. if (compareFn && typeof compareFn === 'function') {
  23328. result = compareFn.call(
  23329. this,
  23330. tr1.cells[sortByCellIndex],
  23331. tr2.cells[sortByCellIndex]
  23332. )
  23333. } else if (compareFn && typeof compareFn === 'number') {
  23334. result = 1
  23335. } else if (compareFn && typeof compareFn === 'string' && Fn[compareFn]) {
  23336. result = Fn[compareFn].call(
  23337. this,
  23338. tr1.cells[sortByCellIndex],
  23339. tr2.cells[sortByCellIndex]
  23340. )
  23341. } else {
  23342. result = Fn['orderbyasc'].call(
  23343. this,
  23344. tr1.cells[sortByCellIndex],
  23345. tr2.cells[sortByCellIndex]
  23346. )
  23347. }
  23348. return result
  23349. })
  23350. var fragment = table.ownerDocument.createDocumentFragment()
  23351. for (var j = 0, len = trArray.length; j < len; j++) {
  23352. fragment.appendChild(trArray[j])
  23353. }
  23354. var tbody = table.getElementsByTagName('tbody')[0]
  23355. if (!lastRowIndex) {
  23356. tbody.appendChild(fragment)
  23357. } else {
  23358. tbody.insertBefore(
  23359. fragment,
  23360. rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1]
  23361. )
  23362. }
  23363. }
  23364. UE.plugins['tablesort'] = function() {
  23365. var me = this,
  23366. UT = UE.UETable,
  23367. getUETable = function(tdOrTable) {
  23368. return UT.getUETable(tdOrTable)
  23369. },
  23370. getTableItemsByRange = function(editor) {
  23371. return UT.getTableItemsByRange(editor)
  23372. }
  23373. me.ready(function() {
  23374. //添加表格可排序的样式
  23375. utils.cssRule(
  23376. 'tablesort',
  23377. 'table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;' +
  23378. ' background-image:url(' +
  23379. me.options.themePath +
  23380. me.options.theme +
  23381. '/images/sortable.png);}',
  23382. me.document
  23383. )
  23384. //做单元格合并操作时,清除可排序标识
  23385. me.addListener('afterexeccommand', function(type, cmd) {
  23386. if (cmd == 'mergeright' || cmd == 'mergedown' || cmd == 'mergecells') {
  23387. this.execCommand('disablesort')
  23388. }
  23389. })
  23390. })
  23391. //表格排序
  23392. UE.commands['sorttable'] = {
  23393. queryCommandState: function() {
  23394. var me = this,
  23395. tableItems = getTableItemsByRange(me)
  23396. if (!tableItems.cell) return -1
  23397. var table = tableItems.table,
  23398. cells = table.getElementsByTagName('td')
  23399. for (var i = 0, cell; (cell = cells[i++]); ) {
  23400. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1
  23401. }
  23402. return 0
  23403. },
  23404. execCommand: function(cmd, fn) {
  23405. var me = this,
  23406. range = me.selection.getRange(),
  23407. bk = range.createBookmark(true),
  23408. tableItems = getTableItemsByRange(me),
  23409. cell = tableItems.cell,
  23410. ut = getUETable(tableItems.table),
  23411. cellInfo = ut.getCellInfo(cell)
  23412. ut.sortTable(cellInfo.cellIndex, fn)
  23413. range.moveToBookmark(bk)
  23414. try {
  23415. range.select()
  23416. } catch (e) {}
  23417. }
  23418. }
  23419. //设置表格可排序,清除表格可排序
  23420. UE.commands['enablesort'] = UE.commands['disablesort'] = {
  23421. queryCommandState: function(cmd) {
  23422. var table = getTableItemsByRange(this).table
  23423. if (table && cmd == 'enablesort') {
  23424. var cells = domUtils.getElementsByTagName(table, 'th td')
  23425. for (var i = 0; i < cells.length; i++) {
  23426. if (
  23427. cells[i].getAttribute('colspan') > 1 ||
  23428. cells[i].getAttribute('rowspan') > 1
  23429. )
  23430. return -1
  23431. }
  23432. }
  23433. return !table
  23434. ? -1
  23435. : (cmd == 'enablesort') ^
  23436. (table.getAttribute('data-sort') != 'sortEnabled')
  23437. ? -1
  23438. : 0
  23439. },
  23440. execCommand: function(cmd) {
  23441. var table = getTableItemsByRange(this).table
  23442. table.setAttribute(
  23443. 'data-sort',
  23444. cmd == 'enablesort' ? 'sortEnabled' : 'sortDisabled'
  23445. )
  23446. cmd == 'enablesort'
  23447. ? domUtils.addClass(table, 'sortEnabled')
  23448. : domUtils.removeClasses(table, 'sortEnabled')
  23449. }
  23450. }
  23451. }
  23452. // plugins/contextmenu.js
  23453. ///import core
  23454. ///commands 右键菜单
  23455. ///commandsName ContextMenu
  23456. ///commandsTitle 右键菜单
  23457. /**
  23458. * 右键菜单
  23459. * @function
  23460. * @name baidu.editor.plugins.contextmenu
  23461. * @author zhanyi
  23462. */
  23463. UE.plugins['contextmenu'] = function() {
  23464. var me = this
  23465. me.setOpt('enableContextMenu', true)
  23466. if (me.getOpt('enableContextMenu') === false) {
  23467. return
  23468. }
  23469. var lang = me.getLang('contextMenu'),
  23470. menu,
  23471. items = me.options.contextMenu || [
  23472. {label: lang['selectall'], cmdName: 'selectall'},
  23473. {
  23474. label: lang.cleardoc,
  23475. cmdName: 'cleardoc',
  23476. exec: function() {
  23477. if (confirm(lang.confirmclear)) {
  23478. this.execCommand('cleardoc')
  23479. }
  23480. }
  23481. },
  23482. '-',
  23483. {
  23484. label: lang.unlink,
  23485. cmdName: 'unlink'
  23486. },
  23487. '-',
  23488. {
  23489. group: lang.paragraph,
  23490. icon: 'justifyjustify',
  23491. subMenu: [
  23492. {
  23493. label: lang.justifyleft,
  23494. cmdName: 'justify',
  23495. value: 'left'
  23496. },
  23497. {
  23498. label: lang.justifyright,
  23499. cmdName: 'justify',
  23500. value: 'right'
  23501. },
  23502. {
  23503. label: lang.justifycenter,
  23504. cmdName: 'justify',
  23505. value: 'center'
  23506. },
  23507. {
  23508. label: lang.justifyjustify,
  23509. cmdName: 'justify',
  23510. value: 'justify'
  23511. }
  23512. ]
  23513. },
  23514. '-',
  23515. {
  23516. group: lang.table,
  23517. icon: 'table',
  23518. subMenu: [
  23519. {
  23520. label: lang.inserttable,
  23521. cmdName: 'inserttable'
  23522. },
  23523. {
  23524. label: lang.deletetable,
  23525. cmdName: 'deletetable'
  23526. },
  23527. '-',
  23528. {
  23529. label: lang.deleterow,
  23530. cmdName: 'deleterow'
  23531. },
  23532. {
  23533. label: lang.deletecol,
  23534. cmdName: 'deletecol'
  23535. },
  23536. {
  23537. label: lang.insertcol,
  23538. cmdName: 'insertcol'
  23539. },
  23540. {
  23541. label: lang.insertcolnext,
  23542. cmdName: 'insertcolnext'
  23543. },
  23544. {
  23545. label: lang.insertrow,
  23546. cmdName: 'insertrow'
  23547. },
  23548. {
  23549. label: lang.insertrownext,
  23550. cmdName: 'insertrownext'
  23551. },
  23552. '-',
  23553. {
  23554. label: lang.insertcaption,
  23555. cmdName: 'insertcaption'
  23556. },
  23557. {
  23558. label: lang.deletecaption,
  23559. cmdName: 'deletecaption'
  23560. },
  23561. {
  23562. label: lang.inserttitle,
  23563. cmdName: 'inserttitle'
  23564. },
  23565. {
  23566. label: lang.deletetitle,
  23567. cmdName: 'deletetitle'
  23568. },
  23569. {
  23570. label: lang.inserttitlecol,
  23571. cmdName: 'inserttitlecol'
  23572. },
  23573. {
  23574. label: lang.deletetitlecol,
  23575. cmdName: 'deletetitlecol'
  23576. },
  23577. '-',
  23578. {
  23579. label: lang.mergecells,
  23580. cmdName: 'mergecells'
  23581. },
  23582. {
  23583. label: lang.mergeright,
  23584. cmdName: 'mergeright'
  23585. },
  23586. {
  23587. label: lang.mergedown,
  23588. cmdName: 'mergedown'
  23589. },
  23590. '-',
  23591. {
  23592. label: lang.splittorows,
  23593. cmdName: 'splittorows'
  23594. },
  23595. {
  23596. label: lang.splittocols,
  23597. cmdName: 'splittocols'
  23598. },
  23599. {
  23600. label: lang.splittocells,
  23601. cmdName: 'splittocells'
  23602. },
  23603. '-',
  23604. {
  23605. label: lang.averageDiseRow,
  23606. cmdName: 'averagedistributerow'
  23607. },
  23608. {
  23609. label: lang.averageDisCol,
  23610. cmdName: 'averagedistributecol'
  23611. },
  23612. '-',
  23613. {
  23614. label: lang.edittd,
  23615. cmdName: 'edittd',
  23616. exec: function() {
  23617. if (UE.ui['edittd']) {
  23618. new UE.ui['edittd'](this)
  23619. }
  23620. this.getDialog('edittd').open()
  23621. }
  23622. },
  23623. {
  23624. label: lang.edittable,
  23625. cmdName: 'edittable',
  23626. exec: function() {
  23627. if (UE.ui['edittable']) {
  23628. new UE.ui['edittable'](this)
  23629. }
  23630. this.getDialog('edittable').open()
  23631. }
  23632. },
  23633. {
  23634. label: lang.setbordervisible,
  23635. cmdName: 'setbordervisible'
  23636. }
  23637. ]
  23638. },
  23639. {
  23640. group: lang.tablesort,
  23641. icon: 'tablesort',
  23642. subMenu: [
  23643. {
  23644. label: lang.enablesort,
  23645. cmdName: 'enablesort'
  23646. },
  23647. {
  23648. label: lang.disablesort,
  23649. cmdName: 'disablesort'
  23650. },
  23651. '-',
  23652. {
  23653. label: lang.reversecurrent,
  23654. cmdName: 'sorttable',
  23655. value: 'reversecurrent'
  23656. },
  23657. {
  23658. label: lang.orderbyasc,
  23659. cmdName: 'sorttable',
  23660. value: 'orderbyasc'
  23661. },
  23662. {
  23663. label: lang.reversebyasc,
  23664. cmdName: 'sorttable',
  23665. value: 'reversebyasc'
  23666. },
  23667. {
  23668. label: lang.orderbynum,
  23669. cmdName: 'sorttable',
  23670. value: 'orderbynum'
  23671. },
  23672. {
  23673. label: lang.reversebynum,
  23674. cmdName: 'sorttable',
  23675. value: 'reversebynum'
  23676. }
  23677. ]
  23678. },
  23679. {
  23680. group: lang.borderbk,
  23681. icon: 'borderBack',
  23682. subMenu: [
  23683. {
  23684. label: lang.setcolor,
  23685. cmdName: 'interlacetable',
  23686. exec: function() {
  23687. this.execCommand('interlacetable')
  23688. }
  23689. },
  23690. {
  23691. label: lang.unsetcolor,
  23692. cmdName: 'uninterlacetable',
  23693. exec: function() {
  23694. this.execCommand('uninterlacetable')
  23695. }
  23696. },
  23697. {
  23698. label: lang.setbackground,
  23699. cmdName: 'settablebackground',
  23700. exec: function() {
  23701. this.execCommand('settablebackground', {
  23702. repeat: true,
  23703. colorList: ['#bbb', '#ccc']
  23704. })
  23705. }
  23706. },
  23707. {
  23708. label: lang.unsetbackground,
  23709. cmdName: 'cleartablebackground',
  23710. exec: function() {
  23711. this.execCommand('cleartablebackground')
  23712. }
  23713. },
  23714. {
  23715. label: lang.redandblue,
  23716. cmdName: 'settablebackground',
  23717. exec: function() {
  23718. this.execCommand('settablebackground', {
  23719. repeat: true,
  23720. colorList: ['red', 'blue']
  23721. })
  23722. }
  23723. },
  23724. {
  23725. label: lang.threecolorgradient,
  23726. cmdName: 'settablebackground',
  23727. exec: function() {
  23728. this.execCommand('settablebackground', {
  23729. repeat: true,
  23730. colorList: ['#aaa', '#bbb', '#ccc']
  23731. })
  23732. }
  23733. }
  23734. ]
  23735. },
  23736. {
  23737. group: lang.aligntd,
  23738. icon: 'aligntd',
  23739. subMenu: [
  23740. {
  23741. cmdName: 'cellalignment',
  23742. value: {align: 'left', vAlign: 'top'}
  23743. },
  23744. {
  23745. cmdName: 'cellalignment',
  23746. value: {align: 'center', vAlign: 'top'}
  23747. },
  23748. {
  23749. cmdName: 'cellalignment',
  23750. value: {align: 'right', vAlign: 'top'}
  23751. },
  23752. {
  23753. cmdName: 'cellalignment',
  23754. value: {align: 'left', vAlign: 'middle'}
  23755. },
  23756. {
  23757. cmdName: 'cellalignment',
  23758. value: {align: 'center', vAlign: 'middle'}
  23759. },
  23760. {
  23761. cmdName: 'cellalignment',
  23762. value: {align: 'right', vAlign: 'middle'}
  23763. },
  23764. {
  23765. cmdName: 'cellalignment',
  23766. value: {align: 'left', vAlign: 'bottom'}
  23767. },
  23768. {
  23769. cmdName: 'cellalignment',
  23770. value: {align: 'center', vAlign: 'bottom'}
  23771. },
  23772. {
  23773. cmdName: 'cellalignment',
  23774. value: {align: 'right', vAlign: 'bottom'}
  23775. }
  23776. ]
  23777. },
  23778. {
  23779. group: lang.aligntable,
  23780. icon: 'aligntable',
  23781. subMenu: [
  23782. {
  23783. cmdName: 'tablealignment',
  23784. className: 'left',
  23785. label: lang.tableleft,
  23786. value: 'left'
  23787. },
  23788. {
  23789. cmdName: 'tablealignment',
  23790. className: 'center',
  23791. label: lang.tablecenter,
  23792. value: 'center'
  23793. },
  23794. {
  23795. cmdName: 'tablealignment',
  23796. className: 'right',
  23797. label: lang.tableright,
  23798. value: 'right'
  23799. }
  23800. ]
  23801. },
  23802. '-',
  23803. {
  23804. label: lang.insertparagraphbefore,
  23805. cmdName: 'insertparagraph',
  23806. value: true
  23807. },
  23808. {
  23809. label: lang.insertparagraphafter,
  23810. cmdName: 'insertparagraph'
  23811. },
  23812. {
  23813. label: lang['copy'],
  23814. cmdName: 'copy'
  23815. },
  23816. {
  23817. label: lang['paste'],
  23818. cmdName: 'paste'
  23819. }
  23820. ]
  23821. if (!items.length) {
  23822. return
  23823. }
  23824. var uiUtils = UE.ui.uiUtils
  23825. me.addListener('contextmenu', function(type, evt) {
  23826. var offset = uiUtils.getViewportOffsetByEvent(evt)
  23827. me.fireEvent('beforeselectionchange')
  23828. if (menu) {
  23829. menu.destroy()
  23830. }
  23831. for (var i = 0, ti, contextItems = []; (ti = items[i]); i++) {
  23832. var last
  23833. ;(function(item) {
  23834. if (item == '-') {
  23835. if (
  23836. (last = contextItems[contextItems.length - 1]) &&
  23837. last !== '-'
  23838. ) {
  23839. contextItems.push('-')
  23840. }
  23841. } else if (item.hasOwnProperty('group')) {
  23842. for (var j = 0, cj, subMenu = []; (cj = item.subMenu[j]); j++) {
  23843. ;(function(subItem) {
  23844. if (subItem == '-') {
  23845. if ((last = subMenu[subMenu.length - 1]) && last !== '-') {
  23846. subMenu.push('-')
  23847. } else {
  23848. subMenu.splice(subMenu.length - 1)
  23849. }
  23850. } else {
  23851. if (
  23852. (me.commands[subItem.cmdName] ||
  23853. UE.commands[subItem.cmdName] ||
  23854. subItem.query) &&
  23855. (subItem.query
  23856. ? subItem.query()
  23857. : me.queryCommandState(subItem.cmdName)) > -1
  23858. ) {
  23859. subMenu.push({
  23860. label:
  23861. subItem.label ||
  23862. me.getLang(
  23863. 'contextMenu.' +
  23864. subItem.cmdName +
  23865. (subItem.value || '')
  23866. ) ||
  23867. '',
  23868. className:
  23869. 'edui-for-' +
  23870. subItem.cmdName +
  23871. (subItem.className
  23872. ? ' edui-for-' +
  23873. subItem.cmdName +
  23874. '-' +
  23875. subItem.className
  23876. : ''),
  23877. onclick: subItem.exec
  23878. ? function() {
  23879. subItem.exec.call(me)
  23880. }
  23881. : function() {
  23882. me.execCommand(subItem.cmdName, subItem.value)
  23883. }
  23884. })
  23885. }
  23886. }
  23887. })(cj)
  23888. }
  23889. if (subMenu.length) {
  23890. function getLabel() {
  23891. switch (item.icon) {
  23892. case 'table':
  23893. return me.getLang('contextMenu.table')
  23894. case 'justifyjustify':
  23895. return me.getLang('contextMenu.paragraph')
  23896. case 'aligntd':
  23897. return me.getLang('contextMenu.aligntd')
  23898. case 'aligntable':
  23899. return me.getLang('contextMenu.aligntable')
  23900. case 'tablesort':
  23901. return lang.tablesort
  23902. case 'borderBack':
  23903. return lang.borderbk
  23904. default:
  23905. return ''
  23906. }
  23907. }
  23908. contextItems.push({
  23909. //todo 修正成自动获取方式
  23910. label: getLabel(),
  23911. className: 'edui-for-' + item.icon,
  23912. subMenu: {
  23913. items: subMenu,
  23914. editor: me
  23915. }
  23916. })
  23917. }
  23918. } else {
  23919. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  23920. if (
  23921. (me.commands[item.cmdName] ||
  23922. UE.commands[item.cmdName] ||
  23923. item.query) &&
  23924. (item.query
  23925. ? item.query.call(me)
  23926. : me.queryCommandState(item.cmdName)) > -1
  23927. ) {
  23928. contextItems.push({
  23929. label: item.label || me.getLang('contextMenu.' + item.cmdName),
  23930. className:
  23931. 'edui-for-' +
  23932. (item.icon ? item.icon : item.cmdName + (item.value || '')),
  23933. onclick: item.exec
  23934. ? function() {
  23935. item.exec.call(me)
  23936. }
  23937. : function() {
  23938. me.execCommand(item.cmdName, item.value)
  23939. }
  23940. })
  23941. }
  23942. }
  23943. })(ti)
  23944. }
  23945. if (contextItems[contextItems.length - 1] == '-') {
  23946. contextItems.pop()
  23947. }
  23948. menu = new UE.ui.Menu({
  23949. items: contextItems,
  23950. className: 'edui-contextmenu',
  23951. editor: me
  23952. })
  23953. menu.render()
  23954. menu.showAt(offset)
  23955. me.fireEvent('aftershowcontextmenu', menu)
  23956. domUtils.preventDefault(evt)
  23957. if (browser.ie) {
  23958. var ieRange
  23959. try {
  23960. ieRange = me.selection.getNative().createRange()
  23961. } catch (e) {
  23962. return
  23963. }
  23964. if (ieRange.item) {
  23965. var range = new dom.Range(me.document)
  23966. range.selectNode(ieRange.item(0)).select(true, true)
  23967. }
  23968. }
  23969. })
  23970. // 添加复制的flash按钮
  23971. me.addListener('aftershowcontextmenu', function(type, menu) {
  23972. if (me.zeroclipboard) {
  23973. var items = menu.items
  23974. for (var key in items) {
  23975. if (items[key].className == 'edui-for-copy') {
  23976. me.zeroclipboard.clip(items[key].getDom())
  23977. }
  23978. }
  23979. }
  23980. })
  23981. }
  23982. // plugins/shortcutmenu.js
  23983. ///import core
  23984. ///commands 弹出菜单
  23985. // commandsName popupmenu
  23986. ///commandsTitle 弹出菜单
  23987. /**
  23988. * 弹出菜单
  23989. * @function
  23990. * @name baidu.editor.plugins.popupmenu
  23991. * @author xuheng
  23992. */
  23993. UE.plugins['shortcutmenu'] = function() {
  23994. var me = this,
  23995. menu,
  23996. items = me.options.shortcutMenu || []
  23997. if (!items.length) {
  23998. return
  23999. }
  24000. me.addListener('contextmenu mouseup', function(type, e) {
  24001. var me = this,
  24002. customEvt = {
  24003. type: type,
  24004. target: e.target || e.srcElement,
  24005. screenX: e.screenX,
  24006. screenY: e.screenY,
  24007. clientX: e.clientX,
  24008. clientY: e.clientY
  24009. }
  24010. setTimeout(function() {
  24011. var rng = me.selection.getRange()
  24012. if (rng.collapsed === false || type == 'contextmenu') {
  24013. if (!menu) {
  24014. menu = new baidu.editor.ui.ShortCutMenu({
  24015. editor: me,
  24016. items: items,
  24017. theme: me.options.theme,
  24018. className: 'edui-shortcutmenu'
  24019. })
  24020. menu.render()
  24021. me.fireEvent('afterrendershortcutmenu', menu)
  24022. }
  24023. menu.show(customEvt, !!UE.plugins['contextmenu'])
  24024. }
  24025. })
  24026. if (type == 'contextmenu') {
  24027. domUtils.preventDefault(e)
  24028. if (browser.ie9below) {
  24029. var ieRange
  24030. try {
  24031. ieRange = me.selection.getNative().createRange()
  24032. } catch (e) {
  24033. return
  24034. }
  24035. if (ieRange.item) {
  24036. var range = new dom.Range(me.document)
  24037. range.selectNode(ieRange.item(0)).select(true, true)
  24038. }
  24039. }
  24040. }
  24041. })
  24042. me.addListener('keydown', function(type) {
  24043. if (type == 'keydown') {
  24044. menu && !menu.isHidden && menu.hide()
  24045. }
  24046. })
  24047. }
  24048. // plugins/basestyle.js
  24049. /**
  24050. * B、I、sub、super命令支持
  24051. * @file
  24052. * @since 1.2.6.1
  24053. */
  24054. UE.plugins['basestyle'] = function() {
  24055. /**
  24056. * 字体加粗
  24057. * @command bold
  24058. * @param { String } cmd 命令字符串
  24059. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  24060. * @method execCommand
  24061. * @example
  24062. * ```javascript
  24063. * //editor是编辑器实例
  24064. * //对当前选中的文本内容执行加粗操作
  24065. * //第一次执行, 文本内容加粗
  24066. * editor.execCommand( 'bold' );
  24067. *
  24068. * //第二次执行, 文本内容取消加粗
  24069. * editor.execCommand( 'bold' );
  24070. * ```
  24071. */
  24072. /**
  24073. * 字体倾斜
  24074. * @command italic
  24075. * @method execCommand
  24076. * @param { String } cmd 命令字符串
  24077. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  24078. * @example
  24079. * ```javascript
  24080. * //editor是编辑器实例
  24081. * //对当前选中的文本内容执行斜体操作
  24082. * //第一次操作, 文本内容将变成斜体
  24083. * editor.execCommand( 'italic' );
  24084. *
  24085. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24086. * editor.execCommand( 'italic' );
  24087. * ```
  24088. */
  24089. /**
  24090. * 下标文本,与“superscript”命令互斥
  24091. * @command subscript
  24092. * @method execCommand
  24093. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  24094. * @param { String } cmd 命令字符串
  24095. * @example
  24096. * ```javascript
  24097. * //editor是编辑器实例
  24098. * //对当前选中的文本内容执行下标操作
  24099. * //第一次操作, 文本内容将变成下标文本
  24100. * editor.execCommand( 'subscript' );
  24101. *
  24102. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24103. * editor.execCommand( 'subscript' );
  24104. * ```
  24105. */
  24106. /**
  24107. * 上标文本,与“subscript”命令互斥
  24108. * @command superscript
  24109. * @method execCommand
  24110. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  24111. * @param { String } cmd 命令字符串
  24112. * @example
  24113. * ```javascript
  24114. * //editor是编辑器实例
  24115. * //对当前选中的文本内容执行上标操作
  24116. * //第一次操作, 文本内容将变成上标文本
  24117. * editor.execCommand( 'superscript' );
  24118. *
  24119. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24120. * editor.execCommand( 'superscript' );
  24121. * ```
  24122. */
  24123. var basestyles = {
  24124. bold: ['strong', 'b'],
  24125. italic: ['em', 'i'],
  24126. subscript: ['sub'],
  24127. superscript: ['sup']
  24128. },
  24129. getObj = function(editor, tagNames) {
  24130. return domUtils.filterNodeList(
  24131. editor.selection.getStartElementPath(),
  24132. tagNames
  24133. )
  24134. },
  24135. me = this
  24136. //添加快捷键
  24137. me.addshortcutkey({
  24138. Bold: 'ctrl+66', //^B
  24139. Italic: 'ctrl+73', //^I
  24140. Underline: 'ctrl+85' //^U
  24141. })
  24142. me.addInputRule(function(root) {
  24143. utils.each(root.getNodesByTagName('b i'), function(node) {
  24144. switch (node.tagName) {
  24145. case 'b':
  24146. node.tagName = 'strong'
  24147. break
  24148. case 'i':
  24149. node.tagName = 'em'
  24150. }
  24151. })
  24152. })
  24153. for (var style in basestyles) {
  24154. ;(function(cmd, tagNames) {
  24155. me.commands[cmd] = {
  24156. execCommand: function(cmdName) {
  24157. var range = me.selection.getRange(),
  24158. obj = getObj(this, tagNames)
  24159. if (range.collapsed) {
  24160. if (obj) {
  24161. var tmpText = me.document.createTextNode('')
  24162. range.insertNode(tmpText).removeInlineStyle(tagNames)
  24163. range.setStartBefore(tmpText)
  24164. domUtils.remove(tmpText)
  24165. } else {
  24166. var tmpNode = range.document.createElement(tagNames[0])
  24167. if (cmdName == 'superscript' || cmdName == 'subscript') {
  24168. tmpText = me.document.createTextNode('')
  24169. range
  24170. .insertNode(tmpText)
  24171. .removeInlineStyle(['sub', 'sup'])
  24172. .setStartBefore(tmpText)
  24173. .collapse(true)
  24174. }
  24175. range.insertNode(tmpNode).setStart(tmpNode, 0)
  24176. }
  24177. range.collapse(true)
  24178. } else {
  24179. if (cmdName == 'superscript' || cmdName == 'subscript') {
  24180. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  24181. range.removeInlineStyle(['sub', 'sup'])
  24182. }
  24183. }
  24184. obj
  24185. ? range.removeInlineStyle(tagNames)
  24186. : range.applyInlineStyle(tagNames[0])
  24187. }
  24188. range.select()
  24189. },
  24190. queryCommandState: function() {
  24191. return getObj(this, tagNames) ? 1 : 0
  24192. }
  24193. }
  24194. })(style, basestyles[style])
  24195. }
  24196. }
  24197. // plugins/elementpath.js
  24198. /**
  24199. * 选取路径命令
  24200. * @file
  24201. */
  24202. UE.plugins['elementpath'] = function() {
  24203. var currentLevel,
  24204. tagNames,
  24205. me = this
  24206. me.setOpt('elementPathEnabled', true)
  24207. if (!me.options.elementPathEnabled) {
  24208. return
  24209. }
  24210. me.commands['elementpath'] = {
  24211. execCommand: function(cmdName, level) {
  24212. var start = tagNames[level],
  24213. range = me.selection.getRange()
  24214. currentLevel = level * 1
  24215. range.selectNode(start).select()
  24216. },
  24217. queryCommandValue: function() {
  24218. //产生一个副本,不能修改原来的startElementPath;
  24219. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  24220. names = []
  24221. tagNames = parents
  24222. for (var i = 0, ci; (ci = parents[i]); i++) {
  24223. if (ci.nodeType == 3) {
  24224. continue
  24225. }
  24226. var name = ci.tagName.toLowerCase()
  24227. if (name == 'img' && ci.getAttribute('anchorname')) {
  24228. name = 'anchor'
  24229. }
  24230. names[i] = name
  24231. if (currentLevel == i) {
  24232. currentLevel = -1
  24233. break
  24234. }
  24235. }
  24236. return names
  24237. }
  24238. }
  24239. }
  24240. // plugins/formatmatch.js
  24241. /**
  24242. * 格式刷,只格式inline的
  24243. * @file
  24244. * @since 1.2.6.1
  24245. */
  24246. /**
  24247. * 格式刷
  24248. * @command formatmatch
  24249. * @method execCommand
  24250. * @remind 该操作不能复制段落格式
  24251. * @param { String } cmd 命令字符串
  24252. * @example
  24253. * ```javascript
  24254. * //editor是编辑器实例
  24255. * //获取格式刷
  24256. * editor.execCommand( 'formatmatch' );
  24257. * ```
  24258. */
  24259. UE.plugins['formatmatch'] = function() {
  24260. var me = this,
  24261. list = [],
  24262. img,
  24263. flag = 0
  24264. me.addListener('reset', function() {
  24265. list = []
  24266. flag = 0
  24267. })
  24268. function addList(type, evt) {
  24269. if (browser.webkit) {
  24270. var target = evt.target.tagName == 'IMG' ? evt.target : null
  24271. }
  24272. function addFormat(range) {
  24273. if (text) {
  24274. range.selectNode(text)
  24275. }
  24276. return range.applyInlineStyle(list[list.length - 1].tagName, null, list)
  24277. }
  24278. me.undoManger && me.undoManger.save()
  24279. var range = me.selection.getRange(),
  24280. imgT = target || range.getClosedNode()
  24281. if (img && imgT && imgT.tagName == 'IMG') {
  24282. //trace:964
  24283. imgT.style.cssText +=
  24284. ';float:' +
  24285. (img.style.cssFloat || img.style.styleFloat || 'none') +
  24286. ';display:' +
  24287. (img.style.display || 'inline')
  24288. img = null
  24289. } else {
  24290. if (!img) {
  24291. var collapsed = range.collapsed
  24292. if (collapsed) {
  24293. var text = me.document.createTextNode('match')
  24294. range.insertNode(text).select()
  24295. }
  24296. me.__hasEnterExecCommand = true
  24297. //不能把block上的属性干掉
  24298. //trace:1553
  24299. var removeFormatAttributes = me.options.removeFormatAttributes
  24300. me.options.removeFormatAttributes = ''
  24301. me.execCommand('removeformat')
  24302. me.options.removeFormatAttributes = removeFormatAttributes
  24303. me.__hasEnterExecCommand = false
  24304. //trace:969
  24305. range = me.selection.getRange()
  24306. if (list.length) {
  24307. addFormat(range)
  24308. }
  24309. if (text) {
  24310. range.setStartBefore(text).collapse(true)
  24311. }
  24312. range.select()
  24313. text && domUtils.remove(text)
  24314. }
  24315. }
  24316. me.undoManger && me.undoManger.save()
  24317. me.removeListener('mouseup', addList)
  24318. flag = 0
  24319. }
  24320. me.commands['formatmatch'] = {
  24321. execCommand: function(cmdName) {
  24322. if (flag) {
  24323. flag = 0
  24324. list = []
  24325. me.removeListener('mouseup', addList)
  24326. return
  24327. }
  24328. var range = me.selection.getRange()
  24329. img = range.getClosedNode()
  24330. if (!img || img.tagName != 'IMG') {
  24331. range.collapse(true).shrinkBoundary()
  24332. var start = range.startContainer
  24333. list = domUtils.findParents(start, true, function(node) {
  24334. return !domUtils.isBlockElm(node) && node.nodeType == 1
  24335. })
  24336. //a不能加入格式刷, 并且克隆节点
  24337. for (var i = 0, ci; (ci = list[i]); i++) {
  24338. if (ci.tagName == 'A') {
  24339. list.splice(i, 1)
  24340. break
  24341. }
  24342. }
  24343. }
  24344. me.addListener('mouseup', addList)
  24345. flag = 1
  24346. },
  24347. queryCommandState: function() {
  24348. return flag
  24349. },
  24350. notNeedUndo: 1
  24351. }
  24352. }
  24353. // plugins/searchreplace.js
  24354. ///import core
  24355. ///commands 查找替换
  24356. ///commandsName SearchReplace
  24357. ///commandsTitle 查询替换
  24358. ///commandsDialog dialogs\searchreplace
  24359. /**
  24360. * @description 查找替换
  24361. * @author zhanyi
  24362. */
  24363. UE.plugin.register('searchreplace', function() {
  24364. var me = this
  24365. var _blockElm = {table: 1, tbody: 1, tr: 1, ol: 1, ul: 1}
  24366. function findTextInString(textContent, opt, currentIndex) {
  24367. var str = opt.searchStr
  24368. if (opt.dir == -1) {
  24369. textContent = textContent
  24370. .split('')
  24371. .reverse()
  24372. .join('')
  24373. str = str
  24374. .split('')
  24375. .reverse()
  24376. .join('')
  24377. currentIndex = textContent.length - currentIndex
  24378. }
  24379. var reg = new RegExp(str, 'g' + (opt.casesensitive ? '' : 'i')),
  24380. match
  24381. while ((match = reg.exec(textContent))) {
  24382. if (match.index >= currentIndex) {
  24383. return opt.dir == -1
  24384. ? textContent.length - match.index - opt.searchStr.length
  24385. : match.index
  24386. }
  24387. }
  24388. return -1
  24389. }
  24390. function findTextBlockElm(node, currentIndex, opt) {
  24391. var textContent,
  24392. index,
  24393. methodName =
  24394. opt.all || opt.dir == 1 ? 'getNextDomNode' : 'getPreDomNode'
  24395. if (domUtils.isBody(node)) {
  24396. node = node.firstChild
  24397. }
  24398. var first = 1
  24399. while (node) {
  24400. textContent =
  24401. node.nodeType == 3
  24402. ? node.nodeValue
  24403. : node[browser.ie ? 'innerText' : 'textContent']
  24404. index = findTextInString(textContent, opt, currentIndex)
  24405. first = 0
  24406. if (index != -1) {
  24407. return {
  24408. node: node,
  24409. index: index
  24410. }
  24411. }
  24412. node = domUtils[methodName](node)
  24413. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  24414. node = domUtils[methodName](node, true)
  24415. }
  24416. if (node) {
  24417. currentIndex =
  24418. opt.dir == -1
  24419. ? (node.nodeType == 3
  24420. ? node.nodeValue
  24421. : node[browser.ie ? 'innerText' : 'textContent']
  24422. ).length
  24423. : 0
  24424. }
  24425. }
  24426. }
  24427. function findNTextInBlockElm(node, index, str) {
  24428. var currentIndex = 0,
  24429. currentNode = node.firstChild,
  24430. currentNodeLength = 0,
  24431. result
  24432. while (currentNode) {
  24433. if (currentNode.nodeType == 3) {
  24434. currentNodeLength = currentNode.nodeValue.replace(
  24435. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24436. ''
  24437. ).length
  24438. currentIndex += currentNodeLength
  24439. if (currentIndex >= index) {
  24440. return {
  24441. node: currentNode,
  24442. index: currentNodeLength - (currentIndex - index)
  24443. }
  24444. }
  24445. } else if (!dtd.$empty[currentNode.tagName]) {
  24446. currentNodeLength = currentNode[
  24447. browser.ie ? 'innerText' : 'textContent'
  24448. ].replace(/(^[\t\r\n]+)|([\t\r\n]+$)/, '').length
  24449. currentIndex += currentNodeLength
  24450. if (currentIndex >= index) {
  24451. result = findNTextInBlockElm(
  24452. currentNode,
  24453. currentNodeLength - (currentIndex - index),
  24454. str
  24455. )
  24456. if (result) {
  24457. return result
  24458. }
  24459. }
  24460. }
  24461. currentNode = domUtils.getNextDomNode(currentNode)
  24462. }
  24463. }
  24464. function searchReplace(me, opt) {
  24465. var rng = me.selection.getRange(),
  24466. startBlockNode,
  24467. searchStr = opt.searchStr,
  24468. span = me.document.createElement('span')
  24469. span.innerHTML = '$$ueditor_searchreplace_key$$'
  24470. rng.shrinkBoundary(true)
  24471. //判断是不是第一次选中
  24472. if (!rng.collapsed) {
  24473. rng.select()
  24474. var rngText = me.selection.getText()
  24475. if (
  24476. new RegExp(
  24477. '^' + opt.searchStr + '$',
  24478. opt.casesensitive ? '' : 'i'
  24479. ).test(rngText)
  24480. ) {
  24481. if (opt.replaceStr != undefined) {
  24482. replaceText(rng, opt.replaceStr)
  24483. rng.select()
  24484. return true
  24485. } else {
  24486. rng.collapse(opt.dir == -1)
  24487. }
  24488. }
  24489. }
  24490. rng.insertNode(span)
  24491. rng.enlargeToBlockElm(true)
  24492. startBlockNode = rng.startContainer
  24493. var currentIndex = startBlockNode[
  24494. browser.ie ? 'innerText' : 'textContent'
  24495. ].indexOf('$$ueditor_searchreplace_key$$')
  24496. rng.setStartBefore(span)
  24497. domUtils.remove(span)
  24498. var result = findTextBlockElm(startBlockNode, currentIndex, opt)
  24499. if (result) {
  24500. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr)
  24501. var rngEnd = findNTextInBlockElm(
  24502. result.node,
  24503. result.index + searchStr.length,
  24504. searchStr
  24505. )
  24506. rng
  24507. .setStart(rngStart.node, rngStart.index)
  24508. .setEnd(rngEnd.node, rngEnd.index)
  24509. if (opt.replaceStr !== undefined) {
  24510. replaceText(rng, opt.replaceStr)
  24511. }
  24512. rng.select()
  24513. return true
  24514. } else {
  24515. rng.setCursor()
  24516. }
  24517. }
  24518. function replaceText(rng, str) {
  24519. str = me.document.createTextNode(str)
  24520. rng.deleteContents().insertNode(str)
  24521. }
  24522. return {
  24523. commands: {
  24524. searchreplace: {
  24525. execCommand: function(cmdName, opt) {
  24526. utils.extend(
  24527. opt,
  24528. {
  24529. all: false,
  24530. casesensitive: false,
  24531. dir: 1
  24532. },
  24533. true
  24534. )
  24535. var num = 0
  24536. if (opt.all) {
  24537. var rng = me.selection.getRange(),
  24538. first = me.body.firstChild
  24539. if (first && first.nodeType == 1) {
  24540. rng.setStart(first, 0)
  24541. rng.shrinkBoundary(true)
  24542. } else if (first.nodeType == 3) {
  24543. rng.setStartBefore(first)
  24544. }
  24545. rng.collapse(true).select(true)
  24546. if (opt.replaceStr !== undefined) {
  24547. me.fireEvent('saveScene')
  24548. }
  24549. while (searchReplace(this, opt)) {
  24550. num++
  24551. }
  24552. if (num) {
  24553. me.fireEvent('saveScene')
  24554. }
  24555. } else {
  24556. if (opt.replaceStr !== undefined) {
  24557. me.fireEvent('saveScene')
  24558. }
  24559. if (searchReplace(this, opt)) {
  24560. num++
  24561. }
  24562. if (num) {
  24563. me.fireEvent('saveScene')
  24564. }
  24565. }
  24566. return num
  24567. },
  24568. notNeedUndo: 1
  24569. }
  24570. }
  24571. }
  24572. })
  24573. // plugins/customstyle.js
  24574. /**
  24575. * 自定义样式
  24576. * @file
  24577. * @since 1.2.6.1
  24578. */
  24579. /**
  24580. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  24581. * @command customstyle
  24582. * @method execCommand
  24583. * @param { String } cmd 命令字符串
  24584. * @example
  24585. * ```javascript
  24586. * editor.execCommand( 'customstyle' );
  24587. * ```
  24588. */
  24589. UE.plugins['customstyle'] = function() {
  24590. var me = this
  24591. me.setOpt({
  24592. customstyle: [
  24593. {
  24594. tag: 'h1',
  24595. name: 'tc',
  24596. style:
  24597. 'font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:center;margin:0 0 20px 0;'
  24598. },
  24599. {
  24600. tag: 'h1',
  24601. name: 'tl',
  24602. style:
  24603. 'font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:left;margin:0 0 10px 0;'
  24604. },
  24605. {
  24606. tag: 'span',
  24607. name: 'im',
  24608. style:
  24609. 'font-size:16px;font-style:italic;font-weight:bold;line-height:18px;'
  24610. },
  24611. {
  24612. tag: 'span',
  24613. name: 'hi',
  24614. style:
  24615. 'font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;'
  24616. }
  24617. ]
  24618. })
  24619. me.commands['customstyle'] = {
  24620. execCommand: function(cmdName, obj) {
  24621. var me = this,
  24622. tagName = obj.tag,
  24623. node = domUtils.findParent(
  24624. me.selection.getStart(),
  24625. function(node) {
  24626. return node.getAttribute('label')
  24627. },
  24628. true
  24629. ),
  24630. range,
  24631. bk,
  24632. tmpObj = {}
  24633. for (var p in obj) {
  24634. if (obj[p] !== undefined) tmpObj[p] = obj[p]
  24635. }
  24636. delete tmpObj.tag
  24637. if (node && node.getAttribute('label') == obj.label) {
  24638. range = this.selection.getRange()
  24639. bk = range.createBookmark()
  24640. if (range.collapsed) {
  24641. //trace:1732 删掉自定义标签,要有p来回填站位
  24642. if (dtd.$block[node.tagName]) {
  24643. var fillNode = me.document.createElement('p')
  24644. domUtils.moveChild(node, fillNode)
  24645. node.parentNode.insertBefore(fillNode, node)
  24646. domUtils.remove(node)
  24647. } else {
  24648. domUtils.remove(node, true)
  24649. }
  24650. } else {
  24651. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  24652. nodes = domUtils.getElementsByTagName(common, tagName)
  24653. if (new RegExp(tagName, 'i').test(common.tagName)) {
  24654. nodes.push(common)
  24655. }
  24656. for (var i = 0, ni; (ni = nodes[i++]); ) {
  24657. if (ni.getAttribute('label') == obj.label) {
  24658. var ps = domUtils.getPosition(ni, bk.start),
  24659. pe = domUtils.getPosition(ni, bk.end)
  24660. if (
  24661. (ps & domUtils.POSITION_FOLLOWING ||
  24662. ps & domUtils.POSITION_CONTAINS) &&
  24663. (pe & domUtils.POSITION_PRECEDING ||
  24664. pe & domUtils.POSITION_CONTAINS)
  24665. )
  24666. if (dtd.$block[tagName]) {
  24667. var fillNode = me.document.createElement('p')
  24668. domUtils.moveChild(ni, fillNode)
  24669. ni.parentNode.insertBefore(fillNode, ni)
  24670. }
  24671. domUtils.remove(ni, true)
  24672. }
  24673. }
  24674. node = domUtils.findParent(
  24675. common,
  24676. function(node) {
  24677. return node.getAttribute('label') == obj.label
  24678. },
  24679. true
  24680. )
  24681. if (node) {
  24682. domUtils.remove(node, true)
  24683. }
  24684. }
  24685. range.moveToBookmark(bk).select()
  24686. } else {
  24687. if (dtd.$block[tagName]) {
  24688. this.execCommand('paragraph', tagName, tmpObj, 'customstyle')
  24689. range = me.selection.getRange()
  24690. if (!range.collapsed) {
  24691. range.collapse()
  24692. node = domUtils.findParent(
  24693. me.selection.getStart(),
  24694. function(node) {
  24695. return node.getAttribute('label') == obj.label
  24696. },
  24697. true
  24698. )
  24699. var pNode = me.document.createElement('p')
  24700. domUtils.insertAfter(node, pNode)
  24701. domUtils.fillNode(me.document, pNode)
  24702. range.setStart(pNode, 0).setCursor()
  24703. }
  24704. } else {
  24705. range = me.selection.getRange()
  24706. if (range.collapsed) {
  24707. node = me.document.createElement(tagName)
  24708. domUtils.setAttributes(node, tmpObj)
  24709. range
  24710. .insertNode(node)
  24711. .setStart(node, 0)
  24712. .setCursor()
  24713. return
  24714. }
  24715. bk = range.createBookmark()
  24716. range
  24717. .applyInlineStyle(tagName, tmpObj)
  24718. .moveToBookmark(bk)
  24719. .select()
  24720. }
  24721. }
  24722. },
  24723. queryCommandValue: function() {
  24724. var parent = domUtils.filterNodeList(
  24725. this.selection.getStartElementPath(),
  24726. function(node) {
  24727. return node.getAttribute('label')
  24728. }
  24729. )
  24730. return parent ? parent.getAttribute('label') : ''
  24731. }
  24732. }
  24733. //当去掉customstyle是,如果是块元素,用p代替
  24734. me.addListener('keyup', function(type, evt) {
  24735. var keyCode = evt.keyCode || evt.which
  24736. if (keyCode == 32 || keyCode == 13) {
  24737. var range = me.selection.getRange()
  24738. if (range.collapsed) {
  24739. var node = domUtils.findParent(
  24740. me.selection.getStart(),
  24741. function(node) {
  24742. return node.getAttribute('label')
  24743. },
  24744. true
  24745. )
  24746. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  24747. var p = me.document.createElement('p')
  24748. domUtils.insertAfter(node, p)
  24749. domUtils.fillNode(me.document, p)
  24750. domUtils.remove(node)
  24751. range.setStart(p, 0).setCursor()
  24752. }
  24753. }
  24754. }
  24755. })
  24756. }
  24757. // plugins/catchremoteimage.js
  24758. ///import core
  24759. ///commands 远程图片抓取
  24760. ///commandsName catchRemoteImage,catchremoteimageenable
  24761. ///commandsTitle 远程图片抓取
  24762. /**
  24763. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  24764. */
  24765. UE.plugins['catchremoteimage'] = function() {
  24766. var me = this,
  24767. ajax = UE.ajax
  24768. /* 设置默认值 */
  24769. if (me.options.catchRemoteImageEnable === undefined) return
  24770. me.setOpt({
  24771. catchRemoteImageEnable: false
  24772. })
  24773. me.addListener('afterpaste', function() {
  24774. me.fireEvent('catchRemoteImage')
  24775. })
  24776. me.addListener('catchRemoteImage', function() {
  24777. var catcherLocalDomain = me.getOpt('catcherLocalDomain'),
  24778. catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),
  24779. catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),
  24780. catcherFieldName = me.getOpt('catcherFieldName')
  24781. var remoteImages = [],
  24782. imgs = domUtils.getElementsByTagName(me.document, 'img'),
  24783. test = function(src, urls) {
  24784. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  24785. return true
  24786. }
  24787. if (urls) {
  24788. for (var j = 0, url; (url = urls[j++]); ) {
  24789. if (src.indexOf(url) !== -1) {
  24790. return true
  24791. }
  24792. }
  24793. }
  24794. return false
  24795. }
  24796. for (var i = 0, ci; (ci = imgs[i++]); ) {
  24797. if (ci.getAttribute('word_img')) {
  24798. continue
  24799. }
  24800. var src = ci.getAttribute('_src') || ci.src || ''
  24801. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  24802. remoteImages.push(src)
  24803. }
  24804. }
  24805. if (remoteImages.length) {
  24806. catchremoteimage(remoteImages, {
  24807. //成功抓取
  24808. success: function(r) {
  24809. try {
  24810. var info =
  24811. r.state !== undefined ? r : eval('(' + r.responseText + ')')
  24812. } catch (e) {
  24813. return
  24814. }
  24815. /* 获取源路径和新路径 */
  24816. var i,
  24817. j,
  24818. ci,
  24819. cj,
  24820. oldSrc,
  24821. newSrc,
  24822. list = info.list
  24823. for (i = 0; (ci = imgs[i++]); ) {
  24824. oldSrc = ci.getAttribute('_src') || ci.src || ''
  24825. for (j = 0; (cj = list[j++]); ) {
  24826. if (oldSrc == cj.source && cj.state == 'SUCCESS') {
  24827. //抓取失败时不做替换处理
  24828. newSrc = catcherUrlPrefix + cj.url
  24829. domUtils.setAttributes(ci, {
  24830. src: newSrc,
  24831. _src: newSrc
  24832. })
  24833. break
  24834. }
  24835. }
  24836. }
  24837. me.fireEvent('catchremotesuccess')
  24838. },
  24839. //回调失败,本次请求超时
  24840. error: function() {
  24841. me.fireEvent('catchremoteerror')
  24842. }
  24843. })
  24844. }
  24845. function catchremoteimage(imgs, callbacks) {
  24846. var params =
  24847. utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  24848. url = utils.formatUrl(
  24849. catcherActionUrl +
  24850. (catcherActionUrl.indexOf('?') == -1 ? '?' : '&') +
  24851. params
  24852. ),
  24853. isJsonp = utils.isCrossDomainUrl(url),
  24854. opt = {
  24855. method: 'POST',
  24856. dataType: isJsonp ? 'jsonp' : '',
  24857. timeout: 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  24858. onsuccess: callbacks['success'],
  24859. onerror: callbacks['error']
  24860. }
  24861. opt[catcherFieldName] = imgs
  24862. ajax.request(url, opt)
  24863. }
  24864. })
  24865. }
  24866. // plugins/snapscreen.js
  24867. /**
  24868. * 截屏插件,为UEditor提供插入支持
  24869. * @file
  24870. * @since 1.4.2
  24871. */
  24872. UE.plugin.register('snapscreen', function() {
  24873. var me = this
  24874. var snapplugin
  24875. function getLocation(url) {
  24876. var search,
  24877. a = document.createElement('a'),
  24878. params = utils.serializeParam(me.queryCommandValue('serverparam')) || ''
  24879. a.href = url
  24880. if (browser.ie) {
  24881. a.href = a.href
  24882. }
  24883. search = a.search
  24884. if (params) {
  24885. search = search + (search.indexOf('?') == -1 ? '?' : '&') + params
  24886. search = search.replace(/[&]+/gi, '&')
  24887. }
  24888. return {
  24889. port: a.port,
  24890. hostname: a.hostname,
  24891. path: a.pathname + search || +a.hash
  24892. }
  24893. }
  24894. return {
  24895. commands: {
  24896. /**
  24897. * 字体背景颜色
  24898. * @command snapscreen
  24899. * @method execCommand
  24900. * @param { String } cmd 命令字符串
  24901. * @example
  24902. * ```javascript
  24903. * editor.execCommand('snapscreen');
  24904. * ```
  24905. */
  24906. snapscreen: {
  24907. execCommand: function(cmd) {
  24908. var url, local, res
  24909. var lang = me.getLang('snapScreen_plugin')
  24910. if (!snapplugin) {
  24911. var container = me.container
  24912. var doc = me.container.ownerDocument || me.container.document
  24913. snapplugin = doc.createElement('object')
  24914. try {
  24915. snapplugin.type = 'application/x-pluginbaidusnap'
  24916. } catch (e) {
  24917. return
  24918. }
  24919. snapplugin.style.cssText =
  24920. 'position:absolute;left:-9999px;width:0;height:0;'
  24921. snapplugin.setAttribute('width', '0')
  24922. snapplugin.setAttribute('height', '0')
  24923. container.appendChild(snapplugin)
  24924. }
  24925. function onSuccess(rs) {
  24926. try {
  24927. rs = eval('(' + rs + ')')
  24928. if (rs.state == 'SUCCESS') {
  24929. var opt = me.options
  24930. me.execCommand('insertimage', {
  24931. src: opt.snapscreenUrlPrefix + rs.url,
  24932. _src: opt.snapscreenUrlPrefix + rs.url,
  24933. alt: rs.title || '',
  24934. floatStyle: opt.snapscreenImgAlign
  24935. })
  24936. } else {
  24937. alert(rs.state)
  24938. }
  24939. } catch (e) {
  24940. alert(lang.callBackErrorMsg)
  24941. }
  24942. }
  24943. url = me.getActionUrl(me.getOpt('snapscreenActionName'))
  24944. local = getLocation(url)
  24945. setTimeout(function() {
  24946. try {
  24947. res = snapplugin.saveSnapshot(
  24948. local.hostname,
  24949. local.path,
  24950. local.port
  24951. )
  24952. } catch (e) {
  24953. me.ui._dialogs['snapscreenDialog'].open()
  24954. return
  24955. }
  24956. onSuccess(res)
  24957. }, 50)
  24958. },
  24959. queryCommandState: function() {
  24960. return navigator.userAgent.indexOf('Windows', 0) != -1 ? 0 : -1
  24961. }
  24962. }
  24963. }
  24964. }
  24965. })
  24966. // plugins/insertparagraph.js
  24967. /**
  24968. * 插入段落
  24969. * @file
  24970. * @since 1.2.6.1
  24971. */
  24972. /**
  24973. * 插入段落
  24974. * @command insertparagraph
  24975. * @method execCommand
  24976. * @param { String } cmd 命令字符串
  24977. * @example
  24978. * ```javascript
  24979. * //editor是编辑器实例
  24980. * editor.execCommand( 'insertparagraph' );
  24981. * ```
  24982. */
  24983. UE.commands['insertparagraph'] = {
  24984. execCommand: function(cmdName, front) {
  24985. var me = this,
  24986. range = me.selection.getRange(),
  24987. start = range.startContainer,
  24988. tmpNode
  24989. while (start) {
  24990. if (domUtils.isBody(start)) {
  24991. break
  24992. }
  24993. tmpNode = start
  24994. start = start.parentNode
  24995. }
  24996. if (tmpNode) {
  24997. var p = me.document.createElement('p')
  24998. if (front) {
  24999. tmpNode.parentNode.insertBefore(p, tmpNode)
  25000. } else {
  25001. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling)
  25002. }
  25003. domUtils.fillNode(me.document, p)
  25004. range.setStart(p, 0).setCursor(false, true)
  25005. }
  25006. }
  25007. }
  25008. // plugins/webapp.js
  25009. /**
  25010. * 百度应用
  25011. * @file
  25012. * @since 1.2.6.1
  25013. */
  25014. /**
  25015. * 插入百度应用
  25016. * @command webapp
  25017. * @method execCommand
  25018. * @remind 需要百度APPKey
  25019. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  25020. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  25021. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  25022. * @example
  25023. * ```javascript
  25024. * //editor是编辑器实例
  25025. * //在编辑器里插入一个“植物大战僵尸”的APP
  25026. * editor.execCommand( 'webapp' , {
  25027. * title: '植物大战僵尸',
  25028. * width: 560,
  25029. * height: 465,
  25030. * logo: '应用展示的图片',
  25031. * url: '百度应用的地址'
  25032. * } );
  25033. * ```
  25034. */
  25035. //UE.plugins['webapp'] = function () {
  25036. // var me = this;
  25037. // function createInsertStr( obj, toIframe, addParagraph ) {
  25038. // return !toIframe ?
  25039. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  25040. // ' 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 + '" />' +
  25041. // (addParagraph ? '</p>' : '')
  25042. // :
  25043. // '<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>';
  25044. // }
  25045. //
  25046. // function switchImgAndIframe( img2frame ) {
  25047. // var tmpdiv,
  25048. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  25049. // for ( var i = 0, node; node = nodes[i++]; ) {
  25050. // if ( node.className != "edui-faked-webapp" ){
  25051. // continue;
  25052. // }
  25053. // tmpdiv = me.document.createElement( "div" );
  25054. // 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 );
  25055. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  25056. // }
  25057. // }
  25058. //
  25059. // me.addListener( "beforegetcontent", function () {
  25060. // switchImgAndIframe( true );
  25061. // } );
  25062. // me.addListener( 'aftersetcontent', function () {
  25063. // switchImgAndIframe( false );
  25064. // } );
  25065. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  25066. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  25067. // return;
  25068. // }
  25069. // switchImgAndIframe( false );
  25070. // } );
  25071. //
  25072. // me.commands['webapp'] = {
  25073. // execCommand:function ( cmd, obj ) {
  25074. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  25075. // }
  25076. // };
  25077. //};
  25078. UE.plugin.register('webapp', function() {
  25079. var me = this
  25080. function createInsertStr(obj, toEmbed) {
  25081. return !toEmbed
  25082. ? '<img title="' +
  25083. obj.title +
  25084. '" width="' +
  25085. obj.width +
  25086. '" height="' +
  25087. obj.height +
  25088. '"' +
  25089. ' src="' +
  25090. me.options.UEDITOR_HOME_URL +
  25091. 'themes/default/images/spacer.gif" _logo_url="' +
  25092. obj.logo +
  25093. '" style="background:url(' +
  25094. obj.logo +
  25095. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' +
  25096. obj.url +
  25097. '" ' +
  25098. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  25099. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  25100. '/>'
  25101. : '<iframe class="edui-faked-webapp" title="' +
  25102. obj.title +
  25103. '" ' +
  25104. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  25105. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  25106. 'width="' +
  25107. obj.width +
  25108. '" height="' +
  25109. obj.height +
  25110. '" scrolling="no" frameborder="0" src="' +
  25111. obj.url +
  25112. '" logo_url = "' +
  25113. obj.logo +
  25114. '"></iframe>'
  25115. }
  25116. return {
  25117. outputRule: function(root) {
  25118. utils.each(root.getNodesByTagName('img'), function(node) {
  25119. var html
  25120. if (node.getAttr('class') == 'edui-faked-webapp') {
  25121. html = createInsertStr(
  25122. {
  25123. title: node.getAttr('title'),
  25124. width: node.getAttr('width'),
  25125. height: node.getAttr('height'),
  25126. align: node.getAttr('align'),
  25127. cssfloat: node.getStyle('float'),
  25128. url: node.getAttr('_url'),
  25129. logo: node.getAttr('_logo_url')
  25130. },
  25131. true
  25132. )
  25133. var embed = UE.uNode.createElement(html)
  25134. node.parentNode.replaceChild(embed, node)
  25135. }
  25136. })
  25137. },
  25138. inputRule: function(root) {
  25139. utils.each(root.getNodesByTagName('iframe'), function(node) {
  25140. if (node.getAttr('class') == 'edui-faked-webapp') {
  25141. var img = UE.uNode.createElement(
  25142. createInsertStr({
  25143. title: node.getAttr('title'),
  25144. width: node.getAttr('width'),
  25145. height: node.getAttr('height'),
  25146. align: node.getAttr('align'),
  25147. cssfloat: node.getStyle('float'),
  25148. url: node.getAttr('src'),
  25149. logo: node.getAttr('logo_url')
  25150. })
  25151. )
  25152. node.parentNode.replaceChild(img, node)
  25153. }
  25154. })
  25155. },
  25156. commands: {
  25157. /**
  25158. * 插入百度应用
  25159. * @command webapp
  25160. * @method execCommand
  25161. * @remind 需要百度APPKey
  25162. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  25163. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  25164. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  25165. * @example
  25166. * ```javascript
  25167. * //editor是编辑器实例
  25168. * //在编辑器里插入一个“植物大战僵尸”的APP
  25169. * editor.execCommand( 'webapp' , {
  25170. * title: '植物大战僵尸',
  25171. * width: 560,
  25172. * height: 465,
  25173. * logo: '应用展示的图片',
  25174. * url: '百度应用的地址'
  25175. * } );
  25176. * ```
  25177. */
  25178. webapp: {
  25179. execCommand: function(cmd, obj) {
  25180. var me = this,
  25181. str = createInsertStr(
  25182. utils.extend(obj, {
  25183. align: 'none'
  25184. }),
  25185. false
  25186. )
  25187. me.execCommand('inserthtml', str)
  25188. },
  25189. queryCommandState: function() {
  25190. var me = this,
  25191. img = me.selection.getRange().getClosedNode(),
  25192. flag = img && img.className == 'edui-faked-webapp'
  25193. return flag ? 1 : 0
  25194. }
  25195. }
  25196. }
  25197. }
  25198. })
  25199. // plugins/template.js
  25200. ///import core
  25201. ///import plugins\inserthtml.js
  25202. ///import plugins\cleardoc.js
  25203. ///commands 模板
  25204. ///commandsName template
  25205. ///commandsTitle 模板
  25206. ///commandsDialog dialogs\template
  25207. UE.plugins['template'] = function() {
  25208. UE.commands['template'] = {
  25209. execCommand: function(cmd, obj) {
  25210. obj.html && this.execCommand('inserthtml', obj.html)
  25211. }
  25212. }
  25213. this.addListener('click', function(type, evt) {
  25214. var el = evt.target || evt.srcElement,
  25215. range = this.selection.getRange()
  25216. var tnode = domUtils.findParent(
  25217. el,
  25218. function(node) {
  25219. if (node.className && domUtils.hasClass(node, 'ue_t')) {
  25220. return node
  25221. }
  25222. },
  25223. true
  25224. )
  25225. tnode &&
  25226. range
  25227. .selectNode(tnode)
  25228. .shrinkBoundary()
  25229. .select()
  25230. })
  25231. this.addListener('keydown', function(type, evt) {
  25232. var range = this.selection.getRange()
  25233. if (!range.collapsed) {
  25234. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  25235. var tnode = domUtils.findParent(
  25236. range.startContainer,
  25237. function(node) {
  25238. if (node.className && domUtils.hasClass(node, 'ue_t')) {
  25239. return node
  25240. }
  25241. },
  25242. true
  25243. )
  25244. if (tnode) {
  25245. domUtils.removeClasses(tnode, ['ue_t'])
  25246. }
  25247. }
  25248. }
  25249. })
  25250. }
  25251. // plugins/music.js
  25252. /**
  25253. * 插入音乐命令
  25254. * @file
  25255. */
  25256. UE.plugin.register('music', function() {
  25257. var me = this
  25258. function creatInsertStr(url, width, height, align, cssfloat, toEmbed) {
  25259. return !toEmbed
  25260. ? '<img ' +
  25261. (align && !cssfloat ? 'align="' + align + '"' : '') +
  25262. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  25263. ' width="' +
  25264. width +
  25265. '" height="' +
  25266. height +
  25267. '" _url="' +
  25268. url +
  25269. '" class="edui-faked-music"' +
  25270. ' src="' +
  25271. me.options.langPath +
  25272. me.options.lang +
  25273. '/images/music.png" />'
  25274. : '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  25275. ' src="' +
  25276. url +
  25277. '" width="' +
  25278. width +
  25279. '" height="' +
  25280. height +
  25281. '" ' +
  25282. (align && !cssfloat ? 'align="' + align + '"' : '') +
  25283. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  25284. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >'
  25285. }
  25286. return {
  25287. outputRule: function(root) {
  25288. utils.each(root.getNodesByTagName('img'), function(node) {
  25289. var html
  25290. if (node.getAttr('class') == 'edui-faked-music') {
  25291. var cssfloat = node.getStyle('float')
  25292. var align = node.getAttr('align')
  25293. html = creatInsertStr(
  25294. node.getAttr('_url'),
  25295. node.getAttr('width'),
  25296. node.getAttr('height'),
  25297. align,
  25298. cssfloat,
  25299. true
  25300. )
  25301. var embed = UE.uNode.createElement(html)
  25302. node.parentNode.replaceChild(embed, node)
  25303. }
  25304. })
  25305. },
  25306. inputRule: function(root) {
  25307. utils.each(root.getNodesByTagName('embed'), function(node) {
  25308. if (node.getAttr('class') == 'edui-faked-music') {
  25309. var cssfloat = node.getStyle('float')
  25310. var align = node.getAttr('align')
  25311. html = creatInsertStr(
  25312. node.getAttr('src'),
  25313. node.getAttr('width'),
  25314. node.getAttr('height'),
  25315. align,
  25316. cssfloat,
  25317. false
  25318. )
  25319. var img = UE.uNode.createElement(html)
  25320. node.parentNode.replaceChild(img, node)
  25321. }
  25322. })
  25323. },
  25324. commands: {
  25325. /**
  25326. * 插入音乐
  25327. * @command music
  25328. * @method execCommand
  25329. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  25330. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  25331. * @example
  25332. * ```javascript
  25333. * //editor是编辑器实例
  25334. * //在编辑器里插入一个“植物大战僵尸”的APP
  25335. * editor.execCommand( 'music' , {
  25336. * width: 400,
  25337. * height: 95,
  25338. * align: "center",
  25339. * url: "音乐地址"
  25340. * } );
  25341. * ```
  25342. */
  25343. music: {
  25344. execCommand: function(cmd, musicObj) {
  25345. var me = this,
  25346. str = creatInsertStr(
  25347. musicObj.url,
  25348. musicObj.width || 400,
  25349. musicObj.height || 95,
  25350. 'none',
  25351. false
  25352. )
  25353. me.execCommand('inserthtml', str)
  25354. },
  25355. queryCommandState: function() {
  25356. var me = this,
  25357. img = me.selection.getRange().getClosedNode(),
  25358. flag = img && img.className == 'edui-faked-music'
  25359. return flag ? 1 : 0
  25360. }
  25361. }
  25362. }
  25363. }
  25364. })
  25365. // plugins/autoupload.js
  25366. /**
  25367. * @description
  25368. * 1.拖放文件到编辑区域,自动上传并插入到选区
  25369. * 2.插入粘贴板的图片,自动上传并插入到选区
  25370. * @author Jinqn
  25371. * @date 2013-10-14
  25372. */
  25373. UE.plugin.register('autoupload', function() {
  25374. function sendAndInsertFile(file, editor) {
  25375. var me = editor
  25376. //模拟数据
  25377. var fieldName,
  25378. urlPrefix,
  25379. maxSize,
  25380. allowFiles,
  25381. actionUrl,
  25382. loadingHtml,
  25383. errorHandler,
  25384. successHandler,
  25385. filetype = /image\/\w+/i.test(file.type) ? 'image' : 'file',
  25386. loadingId = 'loading_' + (+new Date()).toString(36)
  25387. fieldName = me.getOpt(filetype + 'FieldName')
  25388. urlPrefix = me.getOpt(filetype + 'UrlPrefix')
  25389. maxSize = me.getOpt(filetype + 'MaxSize')
  25390. allowFiles = me.getOpt(filetype + 'AllowFiles')
  25391. actionUrl = me.getActionUrl(me.getOpt(filetype + 'ActionName'))
  25392. errorHandler = function(title) {
  25393. var loader = me.document.getElementById(loadingId)
  25394. loader && domUtils.remove(loader)
  25395. me.fireEvent('showmessage', {
  25396. id: loadingId,
  25397. content: title,
  25398. type: 'error',
  25399. timeout: 4000
  25400. })
  25401. }
  25402. if (filetype == 'image') {
  25403. loadingHtml =
  25404. '<img class="loadingclass" id="' +
  25405. loadingId +
  25406. '" src="' +
  25407. me.options.themePath +
  25408. me.options.theme +
  25409. '/images/spacer.gif" title="' +
  25410. (me.getLang('autoupload.loading') || '') +
  25411. '" >'
  25412. successHandler = function(data) {
  25413. var link = urlPrefix + data.url,
  25414. loader = me.document.getElementById(loadingId)
  25415. if (loader) {
  25416. loader.setAttribute('src', link)
  25417. loader.setAttribute('_src', link)
  25418. loader.setAttribute('title', data.title || '')
  25419. loader.setAttribute('alt', data.original || '')
  25420. loader.removeAttribute('id')
  25421. domUtils.removeClasses(loader, 'loadingclass')
  25422. }
  25423. }
  25424. } else {
  25425. loadingHtml =
  25426. '<p>' +
  25427. '<img class="loadingclass" id="' +
  25428. loadingId +
  25429. '" src="' +
  25430. me.options.themePath +
  25431. me.options.theme +
  25432. '/images/spacer.gif" title="' +
  25433. (me.getLang('autoupload.loading') || '') +
  25434. '" >' +
  25435. '</p>'
  25436. successHandler = function(data) {
  25437. var link = urlPrefix + data.url,
  25438. loader = me.document.getElementById(loadingId)
  25439. var rng = me.selection.getRange(),
  25440. bk = rng.createBookmark()
  25441. rng.selectNode(loader).select()
  25442. me.execCommand('insertfile', {url: link})
  25443. rng.moveToBookmark(bk).select()
  25444. }
  25445. }
  25446. /* 插入loading的占位符 */
  25447. me.execCommand('inserthtml', loadingHtml)
  25448. /* 判断后端配置是否没有加载成功 */
  25449. if (!me.getOpt(filetype + 'ActionName')) {
  25450. errorHandler(me.getLang('autoupload.errorLoadConfig'))
  25451. return
  25452. }
  25453. /* 判断文件大小是否超出限制 */
  25454. if (file.size > maxSize) {
  25455. errorHandler(me.getLang('autoupload.exceedSizeError'))
  25456. return
  25457. }
  25458. /* 判断文件格式是否超出允许 */
  25459. var fileext = file.name
  25460. ? file.name.substr(file.name.lastIndexOf('.'))
  25461. : ''
  25462. if (
  25463. (fileext && filetype != 'image') ||
  25464. (allowFiles &&
  25465. (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') ==
  25466. -1)
  25467. ) {
  25468. errorHandler(me.getLang('autoupload.exceedTypeError'))
  25469. return
  25470. }
  25471. /* 创建Ajax并提交 */
  25472. var xhr = new XMLHttpRequest(),
  25473. fd = new FormData(),
  25474. params =
  25475. utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  25476. url = utils.formatUrl(
  25477. actionUrl + (actionUrl.indexOf('?') == -1 ? '?' : '&') + params
  25478. )
  25479. fd.append(
  25480. fieldName,
  25481. file,
  25482. file.name || 'blob.' + file.type.substr('image/'.length)
  25483. )
  25484. fd.append('type', 'ajax')
  25485. xhr.open('post', url, true)
  25486. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
  25487. xhr.addEventListener('load', function(e) {
  25488. try {
  25489. var json = new Function('return ' + utils.trim(e.target.response))()
  25490. if (json.state == 'SUCCESS' && json.url) {
  25491. successHandler(json)
  25492. } else {
  25493. errorHandler(json.state)
  25494. }
  25495. } catch (er) {
  25496. errorHandler(me.getLang('autoupload.loadError'))
  25497. }
  25498. })
  25499. xhr.send(fd)
  25500. }
  25501. function getPasteImage(e) {
  25502. return e.clipboardData &&
  25503. e.clipboardData.items &&
  25504. e.clipboardData.items.length == 1 &&
  25505. /^image\//.test(e.clipboardData.items[0].type)
  25506. ? e.clipboardData.items
  25507. : null
  25508. }
  25509. function getDropImage(e) {
  25510. return e.dataTransfer && e.dataTransfer.files
  25511. ? e.dataTransfer.files
  25512. : null
  25513. }
  25514. return {
  25515. outputRule: function(root) {
  25516. utils.each(root.getNodesByTagName('img'), function(n) {
  25517. if (
  25518. /\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))
  25519. ) {
  25520. n.parentNode.removeChild(n)
  25521. }
  25522. })
  25523. utils.each(root.getNodesByTagName('p'), function(n) {
  25524. if (/\bloadpara\b/.test(n.getAttr('class'))) {
  25525. n.parentNode.removeChild(n)
  25526. }
  25527. })
  25528. },
  25529. bindEvents: {
  25530. //插入粘贴板的图片,拖放插入图片
  25531. ready: function(e) {
  25532. var me = this
  25533. if (window.FormData && window.FileReader) {
  25534. domUtils.on(me.body, 'paste drop', function(e) {
  25535. var hasImg = false,
  25536. items
  25537. //获取粘贴板文件列表或者拖放文件列表
  25538. items = e.type == 'paste' ? getPasteImage(e) : getDropImage(e)
  25539. if (items) {
  25540. var len = items.length,
  25541. file
  25542. while (len--) {
  25543. file = items[len]
  25544. if (file.getAsFile) file = file.getAsFile()
  25545. if (file && file.size > 0) {
  25546. sendAndInsertFile(file, me)
  25547. hasImg = true
  25548. }
  25549. }
  25550. hasImg && e.preventDefault()
  25551. }
  25552. })
  25553. //取消拖放图片时出现的文字光标位置提示
  25554. domUtils.on(me.body, 'dragover', function(e) {
  25555. if (e.dataTransfer.types[0] == 'Files') {
  25556. e.preventDefault()
  25557. }
  25558. })
  25559. //设置loading的样式
  25560. utils.cssRule(
  25561. 'loading',
  25562. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  25563. this.options.themePath +
  25564. this.options.theme +
  25565. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n" +
  25566. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  25567. this.options.themePath +
  25568. this.options.theme +
  25569. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  25570. '}',
  25571. this.document
  25572. )
  25573. }
  25574. }
  25575. }
  25576. }
  25577. })
  25578. // plugins/autosave.js
  25579. UE.plugin.register('autosave', function() {
  25580. var me = this,
  25581. //无限循环保护
  25582. lastSaveTime = new Date(),
  25583. //最小保存间隔时间
  25584. MIN_TIME = 20,
  25585. //auto save key
  25586. saveKey = null
  25587. function save(editor) {
  25588. var saveData
  25589. if (new Date() - lastSaveTime < MIN_TIME) {
  25590. return
  25591. }
  25592. if (!editor.hasContents()) {
  25593. //这里不能调用命令来删除, 会造成事件死循环
  25594. saveKey && me.removePreferences(saveKey)
  25595. return
  25596. }
  25597. lastSaveTime = new Date()
  25598. editor._saveFlag = null
  25599. saveData = me.body.innerHTML
  25600. // if ( editor.fireEvent( "beforeautosave", {
  25601. // content: saveData
  25602. // } ) === false ) {
  25603. // return;
  25604. // }
  25605. // me.setPreferences( saveKey, saveData );
  25606. // editor.fireEvent( "afterautosave", {
  25607. // content: saveData
  25608. // } );
  25609. }
  25610. return {
  25611. defaultOptions: {
  25612. //默认间隔时间
  25613. saveInterval: 500
  25614. },
  25615. bindEvents: {
  25616. ready: function() {
  25617. var _suffix = '-drafts-data',
  25618. key = null
  25619. if (me.key) {
  25620. key = me.key + _suffix
  25621. } else {
  25622. key = (me.container.parentNode.id || 'ue-common') + _suffix
  25623. }
  25624. //页面地址+编辑器ID 保持唯一
  25625. saveKey =
  25626. (location.protocol + location.host + location.pathname).replace(
  25627. /[.:\/]/g,
  25628. '_'
  25629. ) + key
  25630. },
  25631. contentchange: function() {
  25632. if (!saveKey) {
  25633. return
  25634. }
  25635. if (me._saveFlag) {
  25636. window.clearTimeout(me._saveFlag)
  25637. }
  25638. if (me.options.saveInterval > 0) {
  25639. me._saveFlag = window.setTimeout(function() {
  25640. save(me)
  25641. }, me.options.saveInterval)
  25642. } else {
  25643. save(me)
  25644. }
  25645. }
  25646. },
  25647. commands: {
  25648. clearlocaldata: {
  25649. execCommand: function(cmd, name) {
  25650. if (saveKey && me.getPreferences(saveKey)) {
  25651. me.removePreferences(saveKey)
  25652. }
  25653. },
  25654. notNeedUndo: true,
  25655. ignoreContentChange: true
  25656. },
  25657. getlocaldata: {
  25658. execCommand: function(cmd, name) {
  25659. return saveKey ? me.getPreferences(saveKey) || '' : ''
  25660. },
  25661. notNeedUndo: true,
  25662. ignoreContentChange: true
  25663. },
  25664. drafts: {
  25665. execCommand: function(cmd, name) {
  25666. if (saveKey) {
  25667. me.body.innerHTML =
  25668. me.getPreferences(saveKey) || '<p>' + domUtils.fillHtml + '</p>'
  25669. me.focus(true)
  25670. }
  25671. },
  25672. queryCommandState: function() {
  25673. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1
  25674. },
  25675. notNeedUndo: true,
  25676. ignoreContentChange: true
  25677. }
  25678. }
  25679. }
  25680. })
  25681. // plugins/charts.js
  25682. UE.plugin.register('charts', function() {
  25683. var me = this
  25684. return {
  25685. bindEvents: {
  25686. chartserror: function() {}
  25687. },
  25688. commands: {
  25689. charts: {
  25690. execCommand: function(cmd, data) {
  25691. var tableNode = domUtils.findParentByTagName(
  25692. this.selection.getRange().startContainer,
  25693. 'table',
  25694. true
  25695. ),
  25696. flagText = [],
  25697. config = {}
  25698. if (!tableNode) {
  25699. return false
  25700. }
  25701. if (!validData(tableNode)) {
  25702. me.fireEvent('chartserror')
  25703. return false
  25704. }
  25705. config.title = data.title || ''
  25706. config.subTitle = data.subTitle || ''
  25707. config.xTitle = data.xTitle || ''
  25708. config.yTitle = data.yTitle || ''
  25709. config.suffix = data.suffix || ''
  25710. config.tip = data.tip || ''
  25711. //数据对齐方式
  25712. config.dataFormat = data.tableDataFormat || ''
  25713. //图表类型
  25714. config.chartType = data.chartType || 0
  25715. for (var key in config) {
  25716. if (!config.hasOwnProperty(key)) {
  25717. continue
  25718. }
  25719. flagText.push(key + ':' + config[key])
  25720. }
  25721. tableNode.setAttribute('data-chart', flagText.join(';'))
  25722. domUtils.addClass(tableNode, 'edui-charts-table')
  25723. },
  25724. queryCommandState: function(cmd, name) {
  25725. var tableNode = domUtils.findParentByTagName(
  25726. this.selection.getRange().startContainer,
  25727. 'table',
  25728. true
  25729. )
  25730. return tableNode && validData(tableNode) ? 0 : -1
  25731. }
  25732. }
  25733. },
  25734. inputRule: function(root) {
  25735. utils.each(root.getNodesByTagName('table'), function(tableNode) {
  25736. if (tableNode.getAttr('data-chart') !== undefined) {
  25737. tableNode.setAttr('style')
  25738. }
  25739. })
  25740. },
  25741. outputRule: function(root) {
  25742. utils.each(root.getNodesByTagName('table'), function(tableNode) {
  25743. if (tableNode.getAttr('data-chart') !== undefined) {
  25744. tableNode.setAttr('style', 'display: none;')
  25745. }
  25746. })
  25747. }
  25748. }
  25749. function validData(table) {
  25750. var firstRows = null,
  25751. cellCount = 0
  25752. //行数不够
  25753. if (table.rows.length < 2) {
  25754. return false
  25755. }
  25756. //列数不够
  25757. if (table.rows[0].cells.length < 2) {
  25758. return false
  25759. }
  25760. //第一行所有cell必须是th
  25761. firstRows = table.rows[0].cells
  25762. cellCount = firstRows.length
  25763. for (var i = 0, cell; (cell = firstRows[i]); i++) {
  25764. if (cell.tagName.toLowerCase() !== 'th') {
  25765. return false
  25766. }
  25767. }
  25768. for (var i = 1, row; (row = table.rows[i]); i++) {
  25769. //每行单元格数不匹配, 返回false
  25770. if (row.cells.length != cellCount) {
  25771. return false
  25772. }
  25773. //第一列不是th也返回false
  25774. if (row.cells[0].tagName.toLowerCase() !== 'th') {
  25775. return false
  25776. }
  25777. for (var j = 1, cell; (cell = row.cells[j]); j++) {
  25778. var value = utils.trim(cell.innerText || cell.textContent || '')
  25779. value = value
  25780. .replace(new RegExp(UE.dom.domUtils.fillChar, 'g'), '')
  25781. .replace(/^\s+|\s+$/g, '')
  25782. //必须是数字
  25783. if (!/^\d*\.?\d+$/.test(value)) {
  25784. return false
  25785. }
  25786. }
  25787. }
  25788. return true
  25789. }
  25790. })
  25791. // plugins/section.js
  25792. /**
  25793. * 目录大纲支持插件
  25794. * @file
  25795. * @since 1.3.0
  25796. */
  25797. UE.plugin.register('section', function() {
  25798. /* 目录节点对象 */
  25799. function Section(option) {
  25800. this.tag = ''
  25801. ;(this.level = -1), (this.dom = null)
  25802. this.nextSection = null
  25803. this.previousSection = null
  25804. this.parentSection = null
  25805. this.startAddress = []
  25806. this.endAddress = []
  25807. this.children = []
  25808. }
  25809. function getSection(option) {
  25810. var section = new Section()
  25811. return utils.extend(section, option)
  25812. }
  25813. function getNodeFromAddress(startAddress, root) {
  25814. var current = root
  25815. for (var i = 0; i < startAddress.length; i++) {
  25816. if (!current.childNodes) return null
  25817. current = current.childNodes[startAddress[i]]
  25818. }
  25819. return current
  25820. }
  25821. var me = this
  25822. return {
  25823. bindMultiEvents: {
  25824. type: 'aftersetcontent afterscencerestore',
  25825. handler: function() {
  25826. me.fireEvent('updateSections')
  25827. }
  25828. },
  25829. bindEvents: {
  25830. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  25831. ready: function() {
  25832. me.fireEvent('updateSections')
  25833. domUtils.on(me.body, 'drop paste', function() {
  25834. me.fireEvent('updateSections')
  25835. })
  25836. },
  25837. /* 执行paragraph命令之后 */
  25838. afterexeccommand: function(type, cmd) {
  25839. if (cmd == 'paragraph') {
  25840. me.fireEvent('updateSections')
  25841. }
  25842. },
  25843. /* 部分键盘操作,触发updateSections事件 */
  25844. keyup: function(type, e) {
  25845. var me = this,
  25846. range = me.selection.getRange()
  25847. if (range.collapsed != true) {
  25848. me.fireEvent('updateSections')
  25849. } else {
  25850. var keyCode = e.keyCode || e.which
  25851. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  25852. me.fireEvent('updateSections')
  25853. }
  25854. }
  25855. }
  25856. },
  25857. commands: {
  25858. getsections: {
  25859. execCommand: function(cmd, levels) {
  25860. var levelFn = levels || ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
  25861. for (var i = 0; i < levelFn.length; i++) {
  25862. if (typeof levelFn[i] == 'string') {
  25863. levelFn[i] = (function(fn) {
  25864. return function(node) {
  25865. return node.tagName == fn.toUpperCase()
  25866. }
  25867. })(levelFn[i])
  25868. } else if (typeof levelFn[i] != 'function') {
  25869. levelFn[i] = function(node) {
  25870. return null
  25871. }
  25872. }
  25873. }
  25874. function getSectionLevel(node) {
  25875. for (var i = 0; i < levelFn.length; i++) {
  25876. if (levelFn[i](node)) return i
  25877. }
  25878. return -1
  25879. }
  25880. var me = this,
  25881. Directory = getSection({level: -1, title: 'root'}),
  25882. previous = Directory
  25883. function traversal(node, Directory) {
  25884. var level,
  25885. tmpSection = null,
  25886. parent,
  25887. child,
  25888. children = node.childNodes
  25889. for (var i = 0, len = children.length; i < len; i++) {
  25890. child = children[i]
  25891. level = getSectionLevel(child)
  25892. if (level >= 0) {
  25893. var address = me.selection
  25894. .getRange()
  25895. .selectNode(child)
  25896. .createAddress(true).startAddress,
  25897. current = getSection({
  25898. tag: child.tagName,
  25899. title: child.innerText || child.textContent || '',
  25900. level: level,
  25901. dom: child,
  25902. startAddress: utils.clone(address, []),
  25903. endAddress: utils.clone(address, []),
  25904. children: []
  25905. })
  25906. previous.nextSection = current
  25907. current.previousSection = previous
  25908. parent = previous
  25909. while (level <= parent.level) {
  25910. parent = parent.parentSection
  25911. }
  25912. current.parentSection = parent
  25913. parent.children.push(current)
  25914. tmpSection = previous = current
  25915. } else {
  25916. child.nodeType === 1 && traversal(child, Directory)
  25917. tmpSection &&
  25918. tmpSection.endAddress[tmpSection.endAddress.length - 1]++
  25919. }
  25920. }
  25921. }
  25922. traversal(me.body, Directory)
  25923. return Directory
  25924. },
  25925. notNeedUndo: true
  25926. },
  25927. movesection: {
  25928. execCommand: function(cmd, sourceSection, targetSection, isAfter) {
  25929. var me = this,
  25930. targetAddress,
  25931. target
  25932. if (!sourceSection || !targetSection || targetSection.level == -1)
  25933. return
  25934. targetAddress = isAfter
  25935. ? targetSection.endAddress
  25936. : targetSection.startAddress
  25937. target = getNodeFromAddress(targetAddress, me.body)
  25938. /* 判断目标地址是否被源章节包含 */
  25939. if (
  25940. !targetAddress ||
  25941. !target ||
  25942. isContainsAddress(
  25943. sourceSection.startAddress,
  25944. sourceSection.endAddress,
  25945. targetAddress
  25946. )
  25947. )
  25948. return
  25949. var startNode = getNodeFromAddress(
  25950. sourceSection.startAddress,
  25951. me.body
  25952. ),
  25953. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  25954. current,
  25955. nextNode
  25956. if (isAfter) {
  25957. current = endNode
  25958. while (
  25959. current &&
  25960. !(
  25961. domUtils.getPosition(startNode, current) &
  25962. domUtils.POSITION_FOLLOWING
  25963. )
  25964. ) {
  25965. nextNode = current.previousSibling
  25966. domUtils.insertAfter(target, current)
  25967. if (current == startNode) break
  25968. current = nextNode
  25969. }
  25970. } else {
  25971. current = startNode
  25972. while (
  25973. current &&
  25974. !(
  25975. domUtils.getPosition(current, endNode) &
  25976. domUtils.POSITION_FOLLOWING
  25977. )
  25978. ) {
  25979. nextNode = current.nextSibling
  25980. target.parentNode.insertBefore(current, target)
  25981. if (current == endNode) break
  25982. current = nextNode
  25983. }
  25984. }
  25985. me.fireEvent('updateSections')
  25986. /* 获取地址的包含关系 */
  25987. function isContainsAddress(
  25988. startAddress,
  25989. endAddress,
  25990. addressTarget
  25991. ) {
  25992. var isAfterStartAddress = false,
  25993. isBeforeEndAddress = false
  25994. for (var i = 0; i < startAddress.length; i++) {
  25995. if (i >= addressTarget.length) break
  25996. if (addressTarget[i] > startAddress[i]) {
  25997. isAfterStartAddress = true
  25998. break
  25999. } else if (addressTarget[i] < startAddress[i]) {
  26000. break
  26001. }
  26002. }
  26003. for (var i = 0; i < endAddress.length; i++) {
  26004. if (i >= addressTarget.length) break
  26005. if (addressTarget[i] < startAddress[i]) {
  26006. isBeforeEndAddress = true
  26007. break
  26008. } else if (addressTarget[i] > startAddress[i]) {
  26009. break
  26010. }
  26011. }
  26012. return isAfterStartAddress && isBeforeEndAddress
  26013. }
  26014. }
  26015. },
  26016. deletesection: {
  26017. execCommand: function(cmd, section, keepChildren) {
  26018. var me = this
  26019. if (!section) return
  26020. function getNodeFromAddress(startAddress) {
  26021. var current = me.body
  26022. for (var i = 0; i < startAddress.length; i++) {
  26023. if (!current.childNodes) return null
  26024. current = current.childNodes[startAddress[i]]
  26025. }
  26026. return current
  26027. }
  26028. var startNode = getNodeFromAddress(section.startAddress),
  26029. endNode = getNodeFromAddress(section.endAddress),
  26030. current = startNode,
  26031. nextNode
  26032. if (!keepChildren) {
  26033. while (
  26034. current &&
  26035. domUtils.inDoc(endNode, me.document) &&
  26036. !(
  26037. domUtils.getPosition(current, endNode) &
  26038. domUtils.POSITION_FOLLOWING
  26039. )
  26040. ) {
  26041. nextNode = current.nextSibling
  26042. domUtils.remove(current)
  26043. current = nextNode
  26044. }
  26045. } else {
  26046. domUtils.remove(current)
  26047. }
  26048. me.fireEvent('updateSections')
  26049. }
  26050. },
  26051. selectsection: {
  26052. execCommand: function(cmd, section) {
  26053. if (!section && !section.dom) return false
  26054. var me = this,
  26055. range = me.selection.getRange(),
  26056. address = {
  26057. startAddress: utils.clone(section.startAddress, []),
  26058. endAddress: utils.clone(section.endAddress, [])
  26059. }
  26060. address.endAddress[address.endAddress.length - 1]++
  26061. range
  26062. .moveToAddress(address)
  26063. .select()
  26064. .scrollToView()
  26065. return true
  26066. },
  26067. notNeedUndo: true
  26068. },
  26069. scrolltosection: {
  26070. execCommand: function(cmd, section) {
  26071. if (!section && !section.dom) return false
  26072. var me = this,
  26073. range = me.selection.getRange(),
  26074. address = {
  26075. startAddress: section.startAddress,
  26076. endAddress: section.endAddress
  26077. }
  26078. address.endAddress[address.endAddress.length - 1]++
  26079. range.moveToAddress(address).scrollToView()
  26080. return true
  26081. },
  26082. notNeedUndo: true
  26083. }
  26084. }
  26085. }
  26086. })
  26087. // plugins/simpleupload.js
  26088. /**
  26089. * @description
  26090. * 简单上传:点击按钮,直接选择文件上传
  26091. * @author Jinqn
  26092. * @date 2014-03-31
  26093. */
  26094. UE.plugin.register('simpleupload', function() {
  26095. var me = this,
  26096. isLoaded = false,
  26097. containerBtn
  26098. function initUploadBtn() {
  26099. var w = containerBtn.offsetWidth || 20,
  26100. h = containerBtn.offsetHeight || 20,
  26101. btnIframe = document.createElement('iframe'),
  26102. btnStyle =
  26103. 'display:block;width:' +
  26104. w +
  26105. 'px;height:' +
  26106. h +
  26107. '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;'
  26108. domUtils.on(btnIframe, 'load', function() {
  26109. var timestrap = (+new Date()).toString(36),
  26110. wrapper,
  26111. btnIframeDoc,
  26112. btnIframeBody
  26113. btnIframeDoc =
  26114. btnIframe.contentDocument || btnIframe.contentWindow.document
  26115. btnIframeBody = btnIframeDoc.body
  26116. wrapper = btnIframeDoc.createElement('div')
  26117. wrapper.innerHTML =
  26118. '<form id="edui_form_' +
  26119. timestrap +
  26120. '" target="edui_iframe_' +
  26121. timestrap +
  26122. '" method="POST" enctype="multipart/form-data" action="' +
  26123. me.getOpt('serverUrl') +
  26124. '" ' +
  26125. 'style="' +
  26126. btnStyle +
  26127. '">' +
  26128. '<input id="edui_input_' +
  26129. timestrap +
  26130. '" type="file" accept="image/*" name="' +
  26131. me.options.imageFieldName +
  26132. '" ' +
  26133. 'style="' +
  26134. btnStyle +
  26135. '">' +
  26136. '</form>' +
  26137. '<iframe id="edui_iframe_' +
  26138. timestrap +
  26139. '" name="edui_iframe_' +
  26140. timestrap +
  26141. '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>'
  26142. wrapper.className = 'edui-' + me.options.theme
  26143. wrapper.id = me.ui.id + '_iframeupload'
  26144. btnIframeBody.style.cssText = btnStyle
  26145. btnIframeBody.style.width = w + 'px'
  26146. btnIframeBody.style.height = h + 'px'
  26147. btnIframeBody.appendChild(wrapper)
  26148. if (btnIframeBody.parentNode) {
  26149. btnIframeBody.parentNode.style.width = w + 'px'
  26150. btnIframeBody.parentNode.style.height = w + 'px'
  26151. }
  26152. var form = btnIframeDoc.getElementById('edui_form_' + timestrap)
  26153. var input = btnIframeDoc.getElementById('edui_input_' + timestrap)
  26154. var iframe = btnIframeDoc.getElementById('edui_iframe_' + timestrap)
  26155. /**
  26156. * 2017-09-07 改掉了ueditor源码,将本身的单文件上传的方法改为ajax上传,主要目的是为了解决跨域的问题
  26157. * @author Guoqing
  26158. */
  26159. domUtils.on(input, 'change', function() {
  26160. if (!input.value) return
  26161. var loadingId = 'loading_' + (+new Date()).toString(36)
  26162. var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'))
  26163. var allowFiles = me.getOpt('imageAllowFiles')
  26164. me.focus()
  26165. me.execCommand(
  26166. 'inserthtml',
  26167. '<img class="loadingclass" id="' +
  26168. loadingId +
  26169. '" src="' +
  26170. me.options.themePath +
  26171. me.options.theme +
  26172. '/images/spacer.gif" title="' +
  26173. (me.getLang('simpleupload.loading') || '') +
  26174. '" >'
  26175. )
  26176. ;/!* 判断后端配置是否没有加载成功 *!/
  26177. if (!me.getOpt('imageActionName')) {
  26178. errorHandler(me.getLang('autoupload.errorLoadConfig'))
  26179. return
  26180. }
  26181. // 判断文件格式是否错误
  26182. var filename = input.value,
  26183. fileext = filename ? filename.substr(filename.lastIndexOf('.')) : ''
  26184. if (
  26185. !fileext ||
  26186. (allowFiles &&
  26187. (allowFiles.join('') + '.').indexOf(
  26188. fileext.toLowerCase() + '.'
  26189. ) == -1)
  26190. ) {
  26191. showErrorLoader(me.getLang('simpleupload.exceedTypeError'))
  26192. return
  26193. }
  26194. var params =
  26195. utils.serializeParam(me.queryCommandValue('serverparam')) || ''
  26196. var action = utils.formatUrl(
  26197. imageActionUrl +
  26198. (imageActionUrl.indexOf('?') == -1 ? '?' : '&') +
  26199. params
  26200. )
  26201. var formData = new FormData()
  26202. formData.append('upfile', form[0].files[0])
  26203. $.ajax({
  26204. url: action,
  26205. type: 'POST',
  26206. cache: false,
  26207. data: formData,
  26208. processData: false,
  26209. contentType: false,
  26210. success: function(data) {
  26211. data = JSON.parse(data)
  26212. var link,
  26213. loader,
  26214. body = (iframe.contentDocument || iframe.contentWindow.document)
  26215. .body,
  26216. result = body.innerText || body.textContent || ''
  26217. link = me.options.imageUrlPrefix + data.url
  26218. if (data.state == 'SUCCESS' && data.url) {
  26219. loader = me.document.getElementById(loadingId)
  26220. loader.setAttribute('src', link)
  26221. loader.setAttribute('_src', link)
  26222. loader.setAttribute('title', data.title || '')
  26223. loader.setAttribute('alt', data.original || '')
  26224. loader.removeAttribute('id')
  26225. domUtils.removeClasses(loader, 'loadingclass')
  26226. } else {
  26227. showErrorLoader && showErrorLoader(data.state)
  26228. }
  26229. form.reset()
  26230. }
  26231. })
  26232. function showErrorLoader(title) {
  26233. if (loadingId) {
  26234. var loader = me.document.getElementById(loadingId)
  26235. loader && domUtils.remove(loader)
  26236. me.fireEvent('showmessage', {
  26237. id: loadingId,
  26238. content: title,
  26239. type: 'error',
  26240. timeout: 4000
  26241. })
  26242. }
  26243. }
  26244. })
  26245. /*domUtils.on(input, 'change', function(){
  26246. if(!input.value) return;
  26247. var loadingId = 'loading_' + (+new Date()).toString(36);
  26248. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  26249. var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'));
  26250. var allowFiles = me.getOpt('imageAllowFiles');
  26251. me.focus();
  26252. me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme +'/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');
  26253. function callback(){
  26254. try{
  26255. var link, json, loader,
  26256. body = (iframe.contentDocument || iframe.contentWindow.document).body,
  26257. result = body.innerText || body.textContent || '';
  26258. json = (new Function("return " + result))();
  26259. link = me.options.imageUrlPrefix + json.url;
  26260. if(json.state == 'SUCCESS' && json.url) {
  26261. loader = me.document.getElementById(loadingId);
  26262. loader.setAttribute('src', link);
  26263. loader.setAttribute('_src', link);
  26264. loader.setAttribute('title', json.title || '');
  26265. loader.setAttribute('alt', json.original || '');
  26266. loader.removeAttribute('id');
  26267. domUtils.removeClasses(loader, 'loadingclass');
  26268. } else {
  26269. showErrorLoader && showErrorLoader(json.state);
  26270. }
  26271. }catch(er){
  26272. showErrorLoader && showErrorLoader(me.getLang('simpleupload.loadError'));
  26273. }
  26274. form.reset();
  26275. domUtils.un(iframe, 'load', callback);
  26276. }
  26277. function showErrorLoader(title){
  26278. if(loadingId) {
  26279. var loader = me.document.getElementById(loadingId);
  26280. loader && domUtils.remove(loader);
  26281. me.fireEvent('showmessage', {
  26282. 'id': loadingId,
  26283. 'content': title,
  26284. 'type': 'error',
  26285. 'timeout': 4000
  26286. });
  26287. }
  26288. }
  26289. /!* 判断后端配置是否没有加载成功 *!/
  26290. if (!me.getOpt('imageActionName')) {
  26291. errorHandler(me.getLang('autoupload.errorLoadConfig'));
  26292. return;
  26293. }
  26294. // 判断文件格式是否错误
  26295. var filename = input.value,
  26296. fileext = filename ? filename.substr(filename.lastIndexOf('.')):'';
  26297. if (!fileext || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  26298. showErrorLoader(me.getLang('simpleupload.exceedTypeError'));
  26299. return;
  26300. }
  26301. domUtils.on(iframe, 'load', callback);
  26302. form.action = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf('?') == -1 ? '?':'&') + params);
  26303. form.submit();
  26304. });*/
  26305. var stateTimer
  26306. me.addListener('selectionchange', function() {
  26307. clearTimeout(stateTimer)
  26308. stateTimer = setTimeout(function() {
  26309. var state = me.queryCommandState('simpleupload')
  26310. if (state == -1) {
  26311. input.disabled = 'disabled'
  26312. } else {
  26313. input.disabled = false
  26314. }
  26315. }, 400)
  26316. })
  26317. isLoaded = true
  26318. })
  26319. btnIframe.style.cssText = btnStyle
  26320. containerBtn.appendChild(btnIframe)
  26321. }
  26322. return {
  26323. bindEvents: {
  26324. ready: function() {
  26325. //设置loading的样式
  26326. utils.cssRule(
  26327. 'loading',
  26328. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  26329. this.options.themePath +
  26330. this.options.theme +
  26331. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n" +
  26332. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  26333. this.options.themePath +
  26334. this.options.theme +
  26335. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  26336. '}',
  26337. this.document
  26338. )
  26339. },
  26340. /* 初始化简单上传按钮 */
  26341. simpleuploadbtnready: function(type, container) {
  26342. containerBtn = container
  26343. me.afterConfigReady(initUploadBtn)
  26344. }
  26345. },
  26346. outputRule: function(root) {
  26347. utils.each(root.getNodesByTagName('img'), function(n) {
  26348. if (
  26349. /\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))
  26350. ) {
  26351. n.parentNode.removeChild(n)
  26352. }
  26353. })
  26354. },
  26355. commands: {
  26356. simpleupload: {
  26357. queryCommandState: function() {
  26358. return isLoaded ? 0 : -1
  26359. }
  26360. }
  26361. }
  26362. }
  26363. })
  26364. // plugins/serverparam.js
  26365. /**
  26366. * 服务器提交的额外参数列表设置插件
  26367. * @file
  26368. * @since 1.2.6.1
  26369. */
  26370. UE.plugin.register('serverparam', function() {
  26371. var me = this,
  26372. serverParam = {}
  26373. return {
  26374. commands: {
  26375. /**
  26376. * 修改服务器提交的额外参数列表,清除所有项
  26377. * @command serverparam
  26378. * @method execCommand
  26379. * @param { String } cmd 命令字符串
  26380. * @example
  26381. * ```javascript
  26382. * editor.execCommand('serverparam');
  26383. * editor.queryCommandValue('serverparam'); //返回空
  26384. * ```
  26385. */
  26386. /**
  26387. * 修改服务器提交的额外参数列表,删除指定项
  26388. * @command serverparam
  26389. * @method execCommand
  26390. * @param { String } cmd 命令字符串
  26391. * @param { String } key 要清除的属性
  26392. * @example
  26393. * ```javascript
  26394. * editor.execCommand('serverparam', 'name'); //删除属性name
  26395. * ```
  26396. */
  26397. /**
  26398. * 修改服务器提交的额外参数列表,使用键值添加项
  26399. * @command serverparam
  26400. * @method execCommand
  26401. * @param { String } cmd 命令字符串
  26402. * @param { String } key 要添加的属性
  26403. * @param { String } value 要添加属性的值
  26404. * @example
  26405. * ```javascript
  26406. * editor.execCommand('serverparam', 'name', 'hello');
  26407. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26408. * ```
  26409. */
  26410. /**
  26411. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  26412. * @command serverparam
  26413. * @method execCommand
  26414. * @param { String } cmd 命令字符串
  26415. * @param { Object } key 传入的键值对对象
  26416. * @example
  26417. * ```javascript
  26418. * editor.execCommand('serverparam', {'name': 'hello'});
  26419. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26420. * ```
  26421. */
  26422. /**
  26423. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  26424. * @command serverparam
  26425. * @method execCommand
  26426. * @param { String } cmd 命令字符串
  26427. * @param { Function } key 自定义获取参数的函数
  26428. * @example
  26429. * ```javascript
  26430. * editor.execCommand('serverparam', function(editor){
  26431. * return {'key': 'value'};
  26432. * });
  26433. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  26434. * ```
  26435. */
  26436. /**
  26437. * 获取服务器提交的额外参数列表
  26438. * @command serverparam
  26439. * @method queryCommandValue
  26440. * @param { String } cmd 命令字符串
  26441. * @example
  26442. * ```javascript
  26443. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  26444. * ```
  26445. */
  26446. serverparam: {
  26447. execCommand: function(cmd, key, value) {
  26448. if (key === undefined || key === null) {
  26449. //不传参数,清空列表
  26450. serverParam = {}
  26451. } else if (utils.isString(key)) {
  26452. //传入键值
  26453. if (value === undefined || value === null) {
  26454. delete serverParam[key]
  26455. } else {
  26456. serverParam[key] = value
  26457. }
  26458. } else if (utils.isObject(key)) {
  26459. //传入对象,覆盖列表项
  26460. utils.extend(serverParam, key, true)
  26461. } else if (utils.isFunction(key)) {
  26462. //传入函数,添加列表项
  26463. utils.extend(serverParam, key(), true)
  26464. }
  26465. },
  26466. queryCommandValue: function() {
  26467. return serverParam || {}
  26468. }
  26469. }
  26470. }
  26471. }
  26472. })
  26473. // plugins/insertfile.js
  26474. /**
  26475. * 插入附件
  26476. */
  26477. UE.plugin.register('insertfile', function() {
  26478. var me = this
  26479. function getFileIcon(url) {
  26480. var ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase(),
  26481. maps = {
  26482. rar: 'icon_rar.gif',
  26483. zip: 'icon_rar.gif',
  26484. tar: 'icon_rar.gif',
  26485. gz: 'icon_rar.gif',
  26486. bz2: 'icon_rar.gif',
  26487. doc: 'icon_doc.gif',
  26488. docx: 'icon_doc.gif',
  26489. pdf: 'icon_pdf.gif',
  26490. mp3: 'icon_mp3.gif',
  26491. xls: 'icon_xls.gif',
  26492. chm: 'icon_chm.gif',
  26493. ppt: 'icon_ppt.gif',
  26494. pptx: 'icon_ppt.gif',
  26495. avi: 'icon_mv.gif',
  26496. rmvb: 'icon_mv.gif',
  26497. wmv: 'icon_mv.gif',
  26498. flv: 'icon_mv.gif',
  26499. swf: 'icon_mv.gif',
  26500. rm: 'icon_mv.gif',
  26501. exe: 'icon_exe.gif',
  26502. psd: 'icon_psd.gif',
  26503. txt: 'icon_txt.gif',
  26504. jpg: 'icon_jpg.gif',
  26505. png: 'icon_jpg.gif',
  26506. jpeg: 'icon_jpg.gif',
  26507. gif: 'icon_jpg.gif',
  26508. ico: 'icon_jpg.gif',
  26509. bmp: 'icon_jpg.gif'
  26510. }
  26511. return maps[ext] ? maps[ext] : maps['txt']
  26512. }
  26513. return {
  26514. commands: {
  26515. insertfile: {
  26516. execCommand: function(command, filelist) {
  26517. filelist = utils.isArray(filelist) ? filelist : [filelist]
  26518. var i,
  26519. item,
  26520. title,
  26521. html = ''
  26522. for (i = 0; i < filelist.length; i++) {
  26523. item = filelist[i]
  26524. title =
  26525. item.title || item.url.substr(item.url.lastIndexOf('/') + 1)
  26526. html +=
  26527. '<p style="line-height: 16px;">' +
  26528. '<a style="font-size:12px; color:#0066cc;" href="' +
  26529. item.url +
  26530. '" title="' +
  26531. title +
  26532. '">' +
  26533. title +
  26534. '</a>' +
  26535. '</p>'
  26536. }
  26537. me.execCommand('insertHtml', html)
  26538. }
  26539. }
  26540. }
  26541. }
  26542. })
  26543. // plugins/xssFilter.js
  26544. /**
  26545. * @file xssFilter.js
  26546. * @desc xss过滤器
  26547. * @author robbenmu
  26548. */
  26549. UE.plugins.xssFilter = function() {
  26550. var config = UEDITOR_CONFIG
  26551. var whitList = config.whitList
  26552. function filter(node) {
  26553. var tagName = node.tagName
  26554. var attrs = node.attrs
  26555. if (!whitList.hasOwnProperty(tagName)) {
  26556. node.parentNode.removeChild(node)
  26557. return false
  26558. }
  26559. UE.utils.each(attrs, function(val, key) {
  26560. if (whitList[tagName].indexOf(key) === -1) {
  26561. node.setAttr(key)
  26562. }
  26563. })
  26564. }
  26565. // 添加inserthtml\paste等操作用的过滤规则
  26566. if (whitList && config.xssFilterRules) {
  26567. this.options.filterRules = (function() {
  26568. var result = {}
  26569. UE.utils.each(whitList, function(val, key) {
  26570. result[key] = function(node) {
  26571. return filter(node)
  26572. }
  26573. })
  26574. return result
  26575. })()
  26576. }
  26577. var tagList = []
  26578. UE.utils.each(whitList, function(val, key) {
  26579. tagList.push(key)
  26580. })
  26581. // 添加input过滤规则
  26582. //
  26583. if (whitList && config.inputXssFilter) {
  26584. this.addInputRule(function(root) {
  26585. root.traversal(function(node) {
  26586. if (node.type !== 'element') {
  26587. return false
  26588. }
  26589. filter(node)
  26590. })
  26591. })
  26592. }
  26593. // 添加output过滤规则
  26594. //
  26595. if (whitList && config.outputXssFilter) {
  26596. this.addOutputRule(function(root) {
  26597. root.traversal(function(node) {
  26598. if (node.type !== 'element') {
  26599. return false
  26600. }
  26601. filter(node)
  26602. })
  26603. })
  26604. }
  26605. }
  26606. // ui/ui.js
  26607. var baidu = baidu || {}
  26608. baidu.editor = baidu.editor || {}
  26609. UE.ui = baidu.editor.ui = {}
  26610. // ui/uiutils.js
  26611. ;(function() {
  26612. var browser = baidu.editor.browser,
  26613. domUtils = baidu.editor.dom.domUtils
  26614. var magic = '$EDITORUI'
  26615. var root = (window[magic] = {})
  26616. var uidMagic = 'ID' + magic
  26617. var uidCount = 0
  26618. var uiUtils = (baidu.editor.ui.uiUtils = {
  26619. uid: function(obj) {
  26620. return obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount
  26621. },
  26622. hook: function(fn, callback) {
  26623. var dg
  26624. if (fn && fn._callbacks) {
  26625. dg = fn
  26626. } else {
  26627. dg = function() {
  26628. var q
  26629. if (fn) {
  26630. q = fn.apply(this, arguments)
  26631. }
  26632. var callbacks = dg._callbacks
  26633. var k = callbacks.length
  26634. while (k--) {
  26635. var r = callbacks[k].apply(this, arguments)
  26636. if (q === undefined) {
  26637. q = r
  26638. }
  26639. }
  26640. return q
  26641. }
  26642. dg._callbacks = []
  26643. }
  26644. dg._callbacks.push(callback)
  26645. return dg
  26646. },
  26647. createElementByHtml: function(html) {
  26648. var el = document.createElement('div')
  26649. el.innerHTML = html
  26650. el = el.firstChild
  26651. el.parentNode.removeChild(el)
  26652. return el
  26653. },
  26654. getViewportElement: function() {
  26655. return browser.ie && browser.quirks
  26656. ? document.body
  26657. : document.documentElement
  26658. },
  26659. getClientRect: function(element) {
  26660. var bcr
  26661. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  26662. try {
  26663. bcr = element.getBoundingClientRect()
  26664. } catch (e) {
  26665. bcr = {left: 0, top: 0, height: 0, width: 0}
  26666. }
  26667. var rect = {
  26668. left: Math.round(bcr.left),
  26669. top: Math.round(bcr.top),
  26670. height: Math.round(bcr.bottom - bcr.top),
  26671. width: Math.round(bcr.right - bcr.left)
  26672. }
  26673. var doc
  26674. while (
  26675. (doc = element.ownerDocument) !== document &&
  26676. (element = domUtils.getWindow(doc).frameElement)
  26677. ) {
  26678. bcr = element.getBoundingClientRect()
  26679. rect.left += bcr.left
  26680. rect.top += bcr.top
  26681. }
  26682. rect.bottom = rect.top + rect.height
  26683. rect.right = rect.left + rect.width
  26684. return rect
  26685. },
  26686. getViewportRect: function() {
  26687. var viewportEl = uiUtils.getViewportElement()
  26688. var width = (window.innerWidth || viewportEl.clientWidth) | 0
  26689. var height = (window.innerHeight || viewportEl.clientHeight) | 0
  26690. return {
  26691. left: 0,
  26692. top: 0,
  26693. height: height,
  26694. width: width,
  26695. bottom: height,
  26696. right: width
  26697. }
  26698. },
  26699. setViewportOffset: function(element, offset) {
  26700. var rect
  26701. var fixedLayer = uiUtils.getFixedLayer()
  26702. if (element.parentNode === fixedLayer) {
  26703. element.style.left = offset.left + 'px'
  26704. element.style.top = offset.top + 'px'
  26705. } else {
  26706. domUtils.setViewportOffset(element, offset)
  26707. }
  26708. },
  26709. getEventOffset: function(evt) {
  26710. var el = evt.target || evt.srcElement
  26711. var rect = uiUtils.getClientRect(el)
  26712. var offset = uiUtils.getViewportOffsetByEvent(evt)
  26713. return {
  26714. left: offset.left - rect.left,
  26715. top: offset.top - rect.top
  26716. }
  26717. },
  26718. getViewportOffsetByEvent: function(evt) {
  26719. var el = evt.target || evt.srcElement
  26720. var frameEl = domUtils.getWindow(el).frameElement
  26721. var offset = {
  26722. left: evt.clientX,
  26723. top: evt.clientY
  26724. }
  26725. if (frameEl && el.ownerDocument !== document) {
  26726. var rect = uiUtils.getClientRect(frameEl)
  26727. offset.left += rect.left
  26728. offset.top += rect.top
  26729. }
  26730. return offset
  26731. },
  26732. setGlobal: function(id, obj) {
  26733. root[id] = obj
  26734. return magic + '["' + id + '"]'
  26735. },
  26736. unsetGlobal: function(id) {
  26737. delete root[id]
  26738. },
  26739. copyAttributes: function(tgt, src) {
  26740. var attributes = src.attributes
  26741. var k = attributes.length
  26742. while (k--) {
  26743. var attrNode = attributes[k]
  26744. if (
  26745. attrNode.nodeName != 'style' &&
  26746. attrNode.nodeName != 'class' &&
  26747. (!browser.ie || attrNode.specified)
  26748. ) {
  26749. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue)
  26750. }
  26751. }
  26752. if (src.className) {
  26753. domUtils.addClass(tgt, src.className)
  26754. }
  26755. if (src.style.cssText) {
  26756. tgt.style.cssText += ';' + src.style.cssText
  26757. }
  26758. },
  26759. removeStyle: function(el, styleName) {
  26760. if (el.style.removeProperty) {
  26761. el.style.removeProperty(styleName)
  26762. } else if (el.style.removeAttribute) {
  26763. el.style.removeAttribute(styleName)
  26764. } else throw ''
  26765. },
  26766. contains: function(elA, elB) {
  26767. return (
  26768. elA &&
  26769. elB &&
  26770. (elA === elB
  26771. ? false
  26772. : elA.contains
  26773. ? elA.contains(elB)
  26774. : elA.compareDocumentPosition(elB) & 16)
  26775. )
  26776. },
  26777. startDrag: function(evt, callbacks, doc) {
  26778. var doc = doc || document
  26779. var startX = evt.clientX
  26780. var startY = evt.clientY
  26781. function handleMouseMove(evt) {
  26782. var x = evt.clientX - startX
  26783. var y = evt.clientY - startY
  26784. callbacks.ondragmove(x, y, evt)
  26785. if (evt.stopPropagation) {
  26786. evt.stopPropagation()
  26787. } else {
  26788. evt.cancelBubble = true
  26789. }
  26790. }
  26791. if (doc.addEventListener) {
  26792. function handleMouseUp(evt) {
  26793. doc.removeEventListener('mousemove', handleMouseMove, true)
  26794. doc.removeEventListener('mouseup', handleMouseUp, true)
  26795. window.removeEventListener('mouseup', handleMouseUp, true)
  26796. callbacks.ondragstop()
  26797. }
  26798. doc.addEventListener('mousemove', handleMouseMove, true)
  26799. doc.addEventListener('mouseup', handleMouseUp, true)
  26800. window.addEventListener('mouseup', handleMouseUp, true)
  26801. evt.preventDefault()
  26802. } else {
  26803. var elm = evt.srcElement
  26804. elm.setCapture()
  26805. function releaseCaptrue() {
  26806. elm.releaseCapture()
  26807. elm.detachEvent('onmousemove', handleMouseMove)
  26808. elm.detachEvent('onmouseup', releaseCaptrue)
  26809. elm.detachEvent('onlosecaptrue', releaseCaptrue)
  26810. callbacks.ondragstop()
  26811. }
  26812. elm.attachEvent('onmousemove', handleMouseMove)
  26813. elm.attachEvent('onmouseup', releaseCaptrue)
  26814. elm.attachEvent('onlosecaptrue', releaseCaptrue)
  26815. evt.returnValue = false
  26816. }
  26817. callbacks.ondragstart()
  26818. },
  26819. getFixedLayer: function() {
  26820. var layer = document.getElementById('edui_fixedlayer')
  26821. if (layer == null) {
  26822. layer = document.createElement('div')
  26823. layer.id = 'edui_fixedlayer'
  26824. document.body.appendChild(layer)
  26825. if (browser.ie && browser.version <= 8) {
  26826. layer.style.position = 'absolute'
  26827. bindFixedLayer()
  26828. setTimeout(updateFixedOffset)
  26829. } else {
  26830. layer.style.position = 'fixed'
  26831. }
  26832. layer.style.left = '0'
  26833. layer.style.top = '0'
  26834. layer.style.width = '0'
  26835. layer.style.height = '0'
  26836. }
  26837. return layer
  26838. },
  26839. makeUnselectable: function(element) {
  26840. if (browser.opera || (browser.ie && browser.version < 9)) {
  26841. element.unselectable = 'on'
  26842. if (element.hasChildNodes()) {
  26843. for (var i = 0; i < element.childNodes.length; i++) {
  26844. if (element.childNodes[i].nodeType == 1) {
  26845. uiUtils.makeUnselectable(element.childNodes[i])
  26846. }
  26847. }
  26848. }
  26849. } else {
  26850. if (element.style.MozUserSelect !== undefined) {
  26851. element.style.MozUserSelect = 'none'
  26852. } else if (element.style.WebkitUserSelect !== undefined) {
  26853. element.style.WebkitUserSelect = 'none'
  26854. } else if (element.style.KhtmlUserSelect !== undefined) {
  26855. element.style.KhtmlUserSelect = 'none'
  26856. }
  26857. }
  26858. }
  26859. })
  26860. function updateFixedOffset() {
  26861. var layer = document.getElementById('edui_fixedlayer')
  26862. uiUtils.setViewportOffset(layer, {
  26863. left: 0,
  26864. top: 0
  26865. })
  26866. // layer.style.display = 'none';
  26867. // layer.style.display = 'block';
  26868. //#trace: 1354
  26869. // setTimeout(updateFixedOffset);
  26870. }
  26871. function bindFixedLayer(adjOffset) {
  26872. domUtils.on(window, 'scroll', updateFixedOffset)
  26873. domUtils.on(
  26874. window,
  26875. 'resize',
  26876. baidu.editor.utils.defer(updateFixedOffset, 0, true)
  26877. )
  26878. }
  26879. })()
  26880. // ui/uibase.js
  26881. ;(function() {
  26882. var utils = baidu.editor.utils,
  26883. uiUtils = baidu.editor.ui.uiUtils,
  26884. EventBase = baidu.editor.EventBase,
  26885. UIBase = (baidu.editor.ui.UIBase = function() {})
  26886. UIBase.prototype = {
  26887. className: '',
  26888. uiName: '',
  26889. initOptions: function(options) {
  26890. var me = this
  26891. for (var k in options) {
  26892. me[k] = options[k]
  26893. }
  26894. this.id = this.id || 'edui' + uiUtils.uid()
  26895. },
  26896. initUIBase: function() {
  26897. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this))
  26898. },
  26899. render: function(holder) {
  26900. var html = this.renderHtml()
  26901. var el = uiUtils.createElementByHtml(html)
  26902. //by xuheng 给每个node添加class
  26903. var list = domUtils.getElementsByTagName(el, '*')
  26904. var theme = 'edui-' + (this.theme || this.editor.options.theme)
  26905. var layer = document.getElementById('edui_fixedlayer')
  26906. for (var i = 0, node; (node = list[i++]); ) {
  26907. domUtils.addClass(node, theme)
  26908. }
  26909. domUtils.addClass(el, theme)
  26910. if (layer) {
  26911. layer.className = ''
  26912. domUtils.addClass(layer, theme)
  26913. }
  26914. var seatEl = this.getDom()
  26915. if (seatEl != null) {
  26916. seatEl.parentNode.replaceChild(el, seatEl)
  26917. uiUtils.copyAttributes(el, seatEl)
  26918. } else {
  26919. if (typeof holder == 'string') {
  26920. holder = document.getElementById(holder)
  26921. }
  26922. holder = holder || uiUtils.getFixedLayer()
  26923. domUtils.addClass(holder, theme)
  26924. holder.appendChild(el)
  26925. }
  26926. this.postRender()
  26927. },
  26928. getDom: function(name) {
  26929. if (!name) {
  26930. return document.getElementById(this.id)
  26931. } else {
  26932. return document.getElementById(this.id + '_' + name)
  26933. }
  26934. },
  26935. postRender: function() {
  26936. this.fireEvent('postrender')
  26937. },
  26938. getHtmlTpl: function() {
  26939. return ''
  26940. },
  26941. formatHtml: function(tpl) {
  26942. var prefix = 'edui-' + this.uiName
  26943. return tpl
  26944. .replace(/##/g, this.id)
  26945. .replace(/%%-/g, this.uiName ? prefix + '-' : '')
  26946. .replace(/%%/g, (this.uiName ? prefix : '') + ' ' + this.className)
  26947. .replace(/\$\$/g, this._globalKey)
  26948. },
  26949. renderHtml: function() {
  26950. return this.formatHtml(this.getHtmlTpl())
  26951. },
  26952. dispose: function() {
  26953. var box = this.getDom()
  26954. if (box) baidu.editor.dom.domUtils.remove(box)
  26955. uiUtils.unsetGlobal(this.id)
  26956. }
  26957. }
  26958. utils.inherits(UIBase, EventBase)
  26959. })()
  26960. // ui/separator.js
  26961. ;(function() {
  26962. var utils = baidu.editor.utils,
  26963. UIBase = baidu.editor.ui.UIBase,
  26964. Separator = (baidu.editor.ui.Separator = function(options) {
  26965. this.initOptions(options)
  26966. this.initSeparator()
  26967. })
  26968. Separator.prototype = {
  26969. uiName: 'separator',
  26970. initSeparator: function() {
  26971. this.initUIBase()
  26972. },
  26973. getHtmlTpl: function() {
  26974. return '<div id="##" class="edui-box %%"></div>'
  26975. }
  26976. }
  26977. utils.inherits(Separator, UIBase)
  26978. })()
  26979. // ui/mask.js
  26980. ///import core
  26981. ///import uicore
  26982. ;(function() {
  26983. var utils = baidu.editor.utils,
  26984. domUtils = baidu.editor.dom.domUtils,
  26985. UIBase = baidu.editor.ui.UIBase,
  26986. uiUtils = baidu.editor.ui.uiUtils
  26987. var Mask = (baidu.editor.ui.Mask = function(options) {
  26988. this.initOptions(options)
  26989. this.initUIBase()
  26990. })
  26991. Mask.prototype = {
  26992. getHtmlTpl: function() {
  26993. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>'
  26994. },
  26995. postRender: function() {
  26996. var me = this
  26997. domUtils.on(window, 'resize', function() {
  26998. setTimeout(function() {
  26999. if (!me.isHidden()) {
  27000. me._fill()
  27001. }
  27002. })
  27003. })
  27004. },
  27005. show: function(zIndex) {
  27006. this._fill()
  27007. this.getDom().style.display = ''
  27008. this.getDom().style.zIndex = zIndex
  27009. },
  27010. hide: function() {
  27011. this.getDom().style.display = 'none'
  27012. this.getDom().style.zIndex = ''
  27013. },
  27014. isHidden: function() {
  27015. return this.getDom().style.display == 'none'
  27016. },
  27017. _onMouseDown: function() {
  27018. return false
  27019. },
  27020. _onClick: function(e, target) {
  27021. this.fireEvent('click', e, target)
  27022. },
  27023. _fill: function() {
  27024. var el = this.getDom()
  27025. var vpRect = uiUtils.getViewportRect()
  27026. el.style.width = vpRect.width + 'px'
  27027. el.style.height = vpRect.height + 'px'
  27028. }
  27029. }
  27030. utils.inherits(Mask, UIBase)
  27031. })()
  27032. // ui/popup.js
  27033. ///import core
  27034. ///import uicore
  27035. ;(function() {
  27036. var utils = baidu.editor.utils,
  27037. uiUtils = baidu.editor.ui.uiUtils,
  27038. domUtils = baidu.editor.dom.domUtils,
  27039. UIBase = baidu.editor.ui.UIBase,
  27040. Popup = (baidu.editor.ui.Popup = function(options) {
  27041. this.initOptions(options)
  27042. this.initPopup()
  27043. })
  27044. var allPopups = []
  27045. function closeAllPopup(evt, el) {
  27046. for (var i = 0; i < allPopups.length; i++) {
  27047. var pop = allPopups[i]
  27048. if (!pop.isHidden()) {
  27049. if (pop.queryAutoHide(el) !== false) {
  27050. if (
  27051. evt &&
  27052. /scroll/gi.test(evt.type) &&
  27053. pop.className == 'edui-wordpastepop'
  27054. )
  27055. return
  27056. pop.hide()
  27057. }
  27058. }
  27059. }
  27060. if (allPopups.length) pop.editor.fireEvent('afterhidepop')
  27061. }
  27062. Popup.postHide = closeAllPopup
  27063. var ANCHOR_CLASSES = [
  27064. 'edui-anchor-topleft',
  27065. 'edui-anchor-topright',
  27066. 'edui-anchor-bottomleft',
  27067. 'edui-anchor-bottomright'
  27068. ]
  27069. Popup.prototype = {
  27070. SHADOW_RADIUS: 5,
  27071. content: null,
  27072. _hidden: false,
  27073. autoRender: true,
  27074. canSideLeft: true,
  27075. canSideUp: true,
  27076. initPopup: function() {
  27077. this.initUIBase()
  27078. allPopups.push(this)
  27079. },
  27080. getHtmlTpl: function() {
  27081. return (
  27082. '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  27083. ' <div id="##_body" class="edui-popup-body">' +
  27084. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  27085. ' <div class="edui-shadow"></div>' +
  27086. ' <div id="##_content" class="edui-popup-content">' +
  27087. this.getContentHtmlTpl() +
  27088. ' </div>' +
  27089. ' </div>' +
  27090. '</div>'
  27091. )
  27092. },
  27093. getContentHtmlTpl: function() {
  27094. if (this.content) {
  27095. if (typeof this.content == 'string') {
  27096. return this.content
  27097. }
  27098. return this.content.renderHtml()
  27099. } else {
  27100. return ''
  27101. }
  27102. },
  27103. _UIBase_postRender: UIBase.prototype.postRender,
  27104. postRender: function() {
  27105. if (this.content instanceof UIBase) {
  27106. this.content.postRender()
  27107. }
  27108. //捕获鼠标滚轮
  27109. if (this.captureWheel && !this.captured) {
  27110. this.captured = true
  27111. var winHeight =
  27112. (document.documentElement.clientHeight ||
  27113. document.body.clientHeight) - 80,
  27114. _height = this.getDom().offsetHeight,
  27115. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  27116. content = this.getDom('content'),
  27117. ifr = this.getDom('body').getElementsByTagName('iframe'),
  27118. me = this
  27119. ifr.length && (ifr = ifr[0])
  27120. while (_top + _height > winHeight) {
  27121. _height -= 30
  27122. }
  27123. content.style.height = _height + 'px'
  27124. //同步更改iframe高度
  27125. ifr && (ifr.style.height = _height + 'px')
  27126. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  27127. if (window.XMLHttpRequest) {
  27128. domUtils.on(
  27129. content,
  27130. 'onmousewheel' in document.body ? 'mousewheel' : 'DOMMouseScroll',
  27131. function(e) {
  27132. if (e.preventDefault) {
  27133. e.preventDefault()
  27134. } else {
  27135. e.returnValue = false
  27136. }
  27137. if (e.wheelDelta) {
  27138. content.scrollTop -= (e.wheelDelta / 120) * 60
  27139. } else {
  27140. content.scrollTop -= (e.detail / -3) * 60
  27141. }
  27142. }
  27143. )
  27144. } else {
  27145. //ie6
  27146. domUtils.on(this.getDom(), 'mousewheel', function(e) {
  27147. e.returnValue = false
  27148. me.getDom('content').scrollTop -= (e.wheelDelta / 120) * 60
  27149. })
  27150. }
  27151. }
  27152. this.fireEvent('postRenderAfter')
  27153. this.hide(true)
  27154. this._UIBase_postRender()
  27155. },
  27156. _doAutoRender: function() {
  27157. if (!this.getDom() && this.autoRender) {
  27158. this.render()
  27159. }
  27160. },
  27161. mesureSize: function() {
  27162. var box = this.getDom('content')
  27163. return uiUtils.getClientRect(box)
  27164. },
  27165. fitSize: function() {
  27166. if (this.captureWheel && this.sized) {
  27167. return this.__size
  27168. }
  27169. this.sized = true
  27170. var popBodyEl = this.getDom('body')
  27171. popBodyEl.style.width = ''
  27172. popBodyEl.style.height = ''
  27173. var size = this.mesureSize()
  27174. if (this.captureWheel) {
  27175. popBodyEl.style.width = -(-20 - size.width) + 'px'
  27176. var height = parseInt(this.getDom('content').style.height, 10)
  27177. !window.isNaN(height) && (size.height = height)
  27178. } else {
  27179. popBodyEl.style.width = size.width + 'px'
  27180. }
  27181. popBodyEl.style.height = size.height + 'px'
  27182. this.__size = size
  27183. this.captureWheel && (this.getDom('content').style.overflow = 'auto')
  27184. return size
  27185. },
  27186. showAnchor: function(element, hoz) {
  27187. this.showAnchorRect(uiUtils.getClientRect(element), hoz)
  27188. },
  27189. showAnchorRect: function(rect, hoz, adj) {
  27190. this._doAutoRender()
  27191. var vpRect = uiUtils.getViewportRect()
  27192. this.getDom().style.visibility = 'hidden'
  27193. this._show()
  27194. var popSize = this.fitSize()
  27195. var sideLeft, sideUp, left, top
  27196. if (hoz) {
  27197. sideLeft =
  27198. this.canSideLeft &&
  27199. rect.right + popSize.width > vpRect.right &&
  27200. rect.left > popSize.width
  27201. sideUp =
  27202. this.canSideUp &&
  27203. rect.top + popSize.height > vpRect.bottom &&
  27204. rect.bottom > popSize.height
  27205. left = sideLeft ? rect.left - popSize.width : rect.right
  27206. top = sideUp ? rect.bottom - popSize.height : rect.top
  27207. } else {
  27208. sideLeft =
  27209. this.canSideLeft &&
  27210. rect.right + popSize.width > vpRect.right &&
  27211. rect.left > popSize.width
  27212. sideUp =
  27213. this.canSideUp &&
  27214. rect.top + popSize.height > vpRect.bottom &&
  27215. rect.bottom > popSize.height
  27216. left = sideLeft ? rect.right - popSize.width : rect.left
  27217. top = sideUp ? rect.top - popSize.height : rect.bottom
  27218. }
  27219. var popEl = this.getDom()
  27220. uiUtils.setViewportOffset(popEl, {
  27221. left: left,
  27222. top: top
  27223. })
  27224. domUtils.removeClasses(popEl, ANCHOR_CLASSES)
  27225. popEl.className +=
  27226. ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)]
  27227. if (this.editor) {
  27228. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10
  27229. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  27230. popEl.style.zIndex - 1
  27231. }
  27232. this.getDom().style.visibility = 'visible'
  27233. },
  27234. showAt: function(offset) {
  27235. var left = offset.left
  27236. var top = offset.top
  27237. var rect = {
  27238. left: left,
  27239. top: top,
  27240. right: left,
  27241. bottom: top,
  27242. height: 0,
  27243. width: 0
  27244. }
  27245. this.showAnchorRect(rect, false, true)
  27246. },
  27247. _show: function() {
  27248. if (this._hidden) {
  27249. var box = this.getDom()
  27250. box.style.display = ''
  27251. this._hidden = false
  27252. // if (box.setActive) {
  27253. // box.setActive();
  27254. // }
  27255. this.fireEvent('show')
  27256. }
  27257. },
  27258. isHidden: function() {
  27259. return this._hidden
  27260. },
  27261. show: function() {
  27262. this._doAutoRender()
  27263. this._show()
  27264. },
  27265. hide: function(notNofity) {
  27266. if (!this._hidden && this.getDom()) {
  27267. this.getDom().style.display = 'none'
  27268. this._hidden = true
  27269. if (!notNofity) {
  27270. this.fireEvent('hide')
  27271. }
  27272. }
  27273. },
  27274. queryAutoHide: function(el) {
  27275. return !el || !uiUtils.contains(this.getDom(), el)
  27276. }
  27277. }
  27278. utils.inherits(Popup, UIBase)
  27279. domUtils.on(document, 'mousedown', function(evt) {
  27280. var el = evt.target || evt.srcElement
  27281. closeAllPopup(evt, el)
  27282. })
  27283. domUtils.on(window, 'scroll', function(evt, el) {
  27284. closeAllPopup(evt, el)
  27285. })
  27286. })()
  27287. // ui/colorpicker.js
  27288. ///import core
  27289. ///import uicore
  27290. ;(function() {
  27291. var utils = baidu.editor.utils,
  27292. UIBase = baidu.editor.ui.UIBase,
  27293. ColorPicker = (baidu.editor.ui.ColorPicker = function(options) {
  27294. this.initOptions(options)
  27295. this.noColorText = this.noColorText || this.editor.getLang('clearColor')
  27296. this.initUIBase()
  27297. })
  27298. ColorPicker.prototype = {
  27299. getHtmlTpl: function() {
  27300. return genColorPicker(this.noColorText, this.editor)
  27301. },
  27302. _onTableClick: function(evt) {
  27303. var tgt = evt.target || evt.srcElement
  27304. var color = tgt.getAttribute('data-color')
  27305. if (color) {
  27306. this.fireEvent('pickcolor', color)
  27307. }
  27308. },
  27309. _onTableOver: function(evt) {
  27310. var tgt = evt.target || evt.srcElement
  27311. var color = tgt.getAttribute('data-color')
  27312. if (color) {
  27313. this.getDom('preview').style.backgroundColor = color
  27314. }
  27315. },
  27316. _onTableOut: function() {
  27317. this.getDom('preview').style.backgroundColor = ''
  27318. },
  27319. _onPickNoColor: function() {
  27320. this.fireEvent('picknocolor')
  27321. }
  27322. }
  27323. utils.inherits(ColorPicker, UIBase)
  27324. var COLORS = (
  27325. 'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
  27326. 'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
  27327. 'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
  27328. 'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
  27329. 'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
  27330. '7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
  27331. 'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,'
  27332. ).split(',')
  27333. function genColorPicker(noColorText, editor) {
  27334. var html =
  27335. '<div id="##" class="edui-colorpicker %%">' +
  27336. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  27337. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  27338. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' +
  27339. noColorText +
  27340. '</div>' +
  27341. '</div>' +
  27342. '<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">' +
  27343. '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;padding-top: 2px"><td colspan="10">' +
  27344. editor.getLang('themeColor') +
  27345. '</td> </tr>' +
  27346. '<tr class="edui-colorpicker-tablefirstrow" >'
  27347. for (var i = 0; i < COLORS.length; i++) {
  27348. if (i && i % 10 === 0) {
  27349. html +=
  27350. '</tr>' +
  27351. (i == 60
  27352. ? '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;"><td colspan="10">' +
  27353. editor.getLang('standardColor') +
  27354. '</td></tr>'
  27355. : '') +
  27356. '<tr' +
  27357. (i == 60 ? ' class="edui-colorpicker-tablefirstrow"' : '') +
  27358. '>'
  27359. }
  27360. html +=
  27361. i < 70
  27362. ? '<td style="padding: 0 2px;"><a hidefocus title="' +
  27363. COLORS[i] +
  27364. '" onclick="return false;" href="javascript:" unselectable="on" class="edui-box edui-colorpicker-colorcell"' +
  27365. ' data-color="#' +
  27366. COLORS[i] +
  27367. '"' +
  27368. ' style="background-color:#' +
  27369. COLORS[i] +
  27370. ';border:solid #ccc;' +
  27371. (i < 10 || i >= 60
  27372. ? 'border-width:1px;'
  27373. : i >= 10 && i < 20
  27374. ? 'border-width:1px 1px 0 1px;'
  27375. : 'border-width:0 1px 0 1px;') +
  27376. '"' +
  27377. '></a></td>'
  27378. : ''
  27379. }
  27380. html += '</tr></table></div>'
  27381. return html
  27382. }
  27383. })()
  27384. // ui/tablepicker.js
  27385. ///import core
  27386. ///import uicore
  27387. ;(function() {
  27388. var utils = baidu.editor.utils,
  27389. uiUtils = baidu.editor.ui.uiUtils,
  27390. UIBase = baidu.editor.ui.UIBase
  27391. var TablePicker = (baidu.editor.ui.TablePicker = function(options) {
  27392. this.initOptions(options)
  27393. this.initTablePicker()
  27394. })
  27395. TablePicker.prototype = {
  27396. defaultNumRows: 10,
  27397. defaultNumCols: 10,
  27398. maxNumRows: 20,
  27399. maxNumCols: 20,
  27400. numRows: 10,
  27401. numCols: 10,
  27402. lengthOfCellSide: 22,
  27403. initTablePicker: function() {
  27404. this.initUIBase()
  27405. },
  27406. getHtmlTpl: function() {
  27407. var me = this
  27408. return (
  27409. '<div id="##" class="edui-tablepicker %%">' +
  27410. '<div class="edui-tablepicker-body">' +
  27411. '<div class="edui-infoarea">' +
  27412. '<span id="##_label" class="edui-label"></span>' +
  27413. '</div>' +
  27414. '<div class="edui-pickarea"' +
  27415. ' onmousemove="$$._onMouseMove(event, this);"' +
  27416. ' onmouseover="$$._onMouseOver(event, this);"' +
  27417. ' onmouseout="$$._onMouseOut(event, this);"' +
  27418. ' onclick="$$._onClick(event, this);"' +
  27419. '>' +
  27420. '<div id="##_overlay" class="edui-overlay"></div>' +
  27421. '</div>' +
  27422. '</div>' +
  27423. '</div>'
  27424. )
  27425. },
  27426. _UIBase_render: UIBase.prototype.render,
  27427. render: function(holder) {
  27428. this._UIBase_render(holder)
  27429. this.getDom('label').innerHTML =
  27430. '0' +
  27431. this.editor.getLang('t_row') +
  27432. ' x 0' +
  27433. this.editor.getLang('t_col')
  27434. },
  27435. _track: function(numCols, numRows) {
  27436. var style = this.getDom('overlay').style
  27437. var sideLen = this.lengthOfCellSide
  27438. style.width = numCols * sideLen + 'px'
  27439. style.height = numRows * sideLen + 'px'
  27440. var label = this.getDom('label')
  27441. label.innerHTML =
  27442. numCols +
  27443. this.editor.getLang('t_col') +
  27444. ' x ' +
  27445. numRows +
  27446. this.editor.getLang('t_row')
  27447. this.numCols = numCols
  27448. this.numRows = numRows
  27449. },
  27450. _onMouseOver: function(evt, el) {
  27451. var rel = evt.relatedTarget || evt.fromElement
  27452. if (!uiUtils.contains(el, rel) && el !== rel) {
  27453. this.getDom('label').innerHTML =
  27454. '0' +
  27455. this.editor.getLang('t_col') +
  27456. ' x 0' +
  27457. this.editor.getLang('t_row')
  27458. this.getDom('overlay').style.visibility = ''
  27459. }
  27460. },
  27461. _onMouseOut: function(evt, el) {
  27462. var rel = evt.relatedTarget || evt.toElement
  27463. if (!uiUtils.contains(el, rel) && el !== rel) {
  27464. this.getDom('label').innerHTML =
  27465. '0' +
  27466. this.editor.getLang('t_col') +
  27467. ' x 0' +
  27468. this.editor.getLang('t_row')
  27469. this.getDom('overlay').style.visibility = 'hidden'
  27470. }
  27471. },
  27472. _onMouseMove: function(evt, el) {
  27473. var style = this.getDom('overlay').style
  27474. var offset = uiUtils.getEventOffset(evt)
  27475. var sideLen = this.lengthOfCellSide
  27476. var numCols = Math.ceil(offset.left / sideLen)
  27477. var numRows = Math.ceil(offset.top / sideLen)
  27478. this._track(numCols, numRows)
  27479. },
  27480. _onClick: function() {
  27481. this.fireEvent('picktable', this.numCols, this.numRows)
  27482. }
  27483. }
  27484. utils.inherits(TablePicker, UIBase)
  27485. })()
  27486. // ui/stateful.js
  27487. ;(function() {
  27488. var browser = baidu.editor.browser,
  27489. domUtils = baidu.editor.dom.domUtils,
  27490. uiUtils = baidu.editor.ui.uiUtils
  27491. var TPL_STATEFUL =
  27492. 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  27493. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  27494. (browser.ie
  27495. ? ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  27496. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"'
  27497. : ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  27498. ' onmouseout="$$.Stateful_onMouseOut(event, this);"')
  27499. baidu.editor.ui.Stateful = {
  27500. alwalysHoverable: false,
  27501. target: null, //目标元素和this指向dom不一样
  27502. Stateful_init: function() {
  27503. this._Stateful_dGetHtmlTpl = this.getHtmlTpl
  27504. this.getHtmlTpl = this.Stateful_getHtmlTpl
  27505. },
  27506. Stateful_getHtmlTpl: function() {
  27507. var tpl = this._Stateful_dGetHtmlTpl()
  27508. // 使用function避免$转义
  27509. return tpl.replace(/stateful/g, function() {
  27510. return TPL_STATEFUL
  27511. })
  27512. },
  27513. Stateful_onMouseEnter: function(evt, el) {
  27514. this.target = el
  27515. if (!this.isDisabled() || this.alwalysHoverable) {
  27516. this.addState('hover')
  27517. this.fireEvent('over')
  27518. }
  27519. },
  27520. Stateful_onMouseLeave: function(evt, el) {
  27521. if (!this.isDisabled() || this.alwalysHoverable) {
  27522. this.removeState('hover')
  27523. this.removeState('active')
  27524. this.fireEvent('out')
  27525. }
  27526. },
  27527. Stateful_onMouseOver: function(evt, el) {
  27528. var rel = evt.relatedTarget
  27529. if (!uiUtils.contains(el, rel) && el !== rel) {
  27530. this.Stateful_onMouseEnter(evt, el)
  27531. }
  27532. },
  27533. Stateful_onMouseOut: function(evt, el) {
  27534. var rel = evt.relatedTarget
  27535. if (!uiUtils.contains(el, rel) && el !== rel) {
  27536. this.Stateful_onMouseLeave(evt, el)
  27537. }
  27538. },
  27539. Stateful_onMouseDown: function(evt, el) {
  27540. if (!this.isDisabled()) {
  27541. this.addState('active')
  27542. }
  27543. },
  27544. Stateful_onMouseUp: function(evt, el) {
  27545. if (!this.isDisabled()) {
  27546. this.removeState('active')
  27547. }
  27548. },
  27549. Stateful_postRender: function() {
  27550. if (this.disabled && !this.hasState('disabled')) {
  27551. this.addState('disabled')
  27552. }
  27553. },
  27554. hasState: function(state) {
  27555. return domUtils.hasClass(this.getStateDom(), 'edui-state-' + state)
  27556. },
  27557. addState: function(state) {
  27558. if (!this.hasState(state)) {
  27559. this.getStateDom().className += ' edui-state-' + state
  27560. }
  27561. },
  27562. removeState: function(state) {
  27563. if (this.hasState(state)) {
  27564. domUtils.removeClasses(this.getStateDom(), ['edui-state-' + state])
  27565. }
  27566. },
  27567. getStateDom: function() {
  27568. return this.getDom('state')
  27569. },
  27570. isChecked: function() {
  27571. return this.hasState('checked')
  27572. },
  27573. setChecked: function(checked) {
  27574. if (!this.isDisabled() && checked) {
  27575. this.addState('checked')
  27576. } else {
  27577. this.removeState('checked')
  27578. }
  27579. },
  27580. isDisabled: function() {
  27581. return this.hasState('disabled')
  27582. },
  27583. setDisabled: function(disabled) {
  27584. if (disabled) {
  27585. this.removeState('hover')
  27586. this.removeState('checked')
  27587. this.removeState('active')
  27588. this.addState('disabled')
  27589. } else {
  27590. this.removeState('disabled')
  27591. }
  27592. }
  27593. }
  27594. })()
  27595. // ui/button.js
  27596. ///import core
  27597. ///import uicore
  27598. ///import ui/stateful.js
  27599. ;(function() {
  27600. var utils = baidu.editor.utils,
  27601. UIBase = baidu.editor.ui.UIBase,
  27602. Stateful = baidu.editor.ui.Stateful,
  27603. Button = (baidu.editor.ui.Button = function(options) {
  27604. if (options.name) {
  27605. var btnName = options.name
  27606. var cssRules = options.cssRules
  27607. if (!options.className) {
  27608. options.className = 'edui-for-' + btnName
  27609. }
  27610. options.cssRules =
  27611. '.edui-default .edui-for-' +
  27612. btnName +
  27613. ' .edui-icon {' +
  27614. cssRules +
  27615. '}'
  27616. }
  27617. this.initOptions(options)
  27618. this.initButton()
  27619. })
  27620. Button.prototype = {
  27621. uiName: 'button',
  27622. label: '',
  27623. title: '',
  27624. showIcon: true,
  27625. showText: true,
  27626. cssRules: '',
  27627. initButton: function() {
  27628. this.initUIBase()
  27629. this.Stateful_init()
  27630. if (this.cssRules) {
  27631. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules)
  27632. }
  27633. },
  27634. getHtmlTpl: function() {
  27635. return (
  27636. '<div id="##" class="edui-box %%">' +
  27637. '<div id="##_state" stateful>' +
  27638. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' +
  27639. (this.title ? 'title="' + this.title + '"' : '') +
  27640. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  27641. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : '') +
  27642. (this.showText
  27643. ? '<div class="edui-box edui-label">' + this.label + '</div>'
  27644. : '') +
  27645. '</div>' +
  27646. '</div>' +
  27647. '</div></div>'
  27648. )
  27649. },
  27650. postRender: function() {
  27651. this.Stateful_postRender()
  27652. this.setDisabled(this.disabled)
  27653. },
  27654. _onMouseDown: function(e) {
  27655. var target = e.target || e.srcElement,
  27656. tagName = target && target.tagName && target.tagName.toLowerCase()
  27657. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  27658. return false
  27659. }
  27660. },
  27661. _onClick: function() {
  27662. if (!this.isDisabled()) {
  27663. this.fireEvent('click')
  27664. }
  27665. },
  27666. setTitle: function(text) {
  27667. var label = this.getDom('label')
  27668. label.innerHTML = text
  27669. }
  27670. }
  27671. utils.inherits(Button, UIBase)
  27672. utils.extend(Button.prototype, Stateful)
  27673. })()
  27674. // ui/splitbutton.js
  27675. ///import core
  27676. ///import uicore
  27677. ///import ui/stateful.js
  27678. ;(function() {
  27679. var utils = baidu.editor.utils,
  27680. uiUtils = baidu.editor.ui.uiUtils,
  27681. domUtils = baidu.editor.dom.domUtils,
  27682. UIBase = baidu.editor.ui.UIBase,
  27683. Stateful = baidu.editor.ui.Stateful,
  27684. SplitButton = (baidu.editor.ui.SplitButton = function(options) {
  27685. this.initOptions(options)
  27686. this.initSplitButton()
  27687. })
  27688. SplitButton.prototype = {
  27689. popup: null,
  27690. uiName: 'splitbutton',
  27691. title: '',
  27692. initSplitButton: function() {
  27693. this.initUIBase()
  27694. this.Stateful_init()
  27695. var me = this
  27696. if (this.popup != null) {
  27697. var popup = this.popup
  27698. this.popup = null
  27699. this.setPopup(popup)
  27700. }
  27701. },
  27702. _UIBase_postRender: UIBase.prototype.postRender,
  27703. postRender: function() {
  27704. this.Stateful_postRender()
  27705. this._UIBase_postRender()
  27706. },
  27707. setPopup: function(popup) {
  27708. if (this.popup === popup) return
  27709. if (this.popup != null) {
  27710. this.popup.dispose()
  27711. }
  27712. popup.addListener('show', utils.bind(this._onPopupShow, this))
  27713. popup.addListener('hide', utils.bind(this._onPopupHide, this))
  27714. popup.addListener(
  27715. 'postrender',
  27716. utils.bind(function() {
  27717. popup
  27718. .getDom('body')
  27719. .appendChild(
  27720. uiUtils.createElementByHtml(
  27721. '<div id="' +
  27722. this.popup.id +
  27723. '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  27724. (uiUtils.getClientRect(this.getDom()).width + 20) +
  27725. 'px"></div>'
  27726. )
  27727. )
  27728. popup.getDom().className += ' ' + this.className
  27729. }, this)
  27730. )
  27731. this.popup = popup
  27732. },
  27733. _onPopupShow: function() {
  27734. this.addState('opened')
  27735. },
  27736. _onPopupHide: function() {
  27737. this.removeState('opened')
  27738. },
  27739. getHtmlTpl: function() {
  27740. return (
  27741. '<div id="##" class="edui-box %%">' +
  27742. '<div ' +
  27743. (this.title ? 'title="' + this.title + '"' : '') +
  27744. ' id="##_state" stateful><div class="%%-body">' +
  27745. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  27746. '<div class="edui-box edui-icon"></div>' +
  27747. '</div>' +
  27748. '<div class="edui-box edui-splitborder"></div>' +
  27749. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  27750. '</div></div></div>'
  27751. )
  27752. },
  27753. showPopup: function() {
  27754. // 当popup往上弹出的时候,做特殊处理
  27755. var rect = uiUtils.getClientRect(this.getDom())
  27756. rect.top -= this.popup.SHADOW_RADIUS
  27757. rect.height += this.popup.SHADOW_RADIUS
  27758. this.popup.showAnchorRect(rect)
  27759. },
  27760. _onArrowClick: function(event, el) {
  27761. if (!this.isDisabled()) {
  27762. this.showPopup()
  27763. }
  27764. },
  27765. _onButtonClick: function() {
  27766. if (!this.isDisabled()) {
  27767. this.fireEvent('buttonclick')
  27768. }
  27769. }
  27770. }
  27771. utils.inherits(SplitButton, UIBase)
  27772. utils.extend(SplitButton.prototype, Stateful, true)
  27773. })()
  27774. // ui/colorbutton.js
  27775. ///import core
  27776. ///import uicore
  27777. ///import ui/colorpicker.js
  27778. ///import ui/popup.js
  27779. ///import ui/splitbutton.js
  27780. ;(function() {
  27781. var utils = baidu.editor.utils,
  27782. uiUtils = baidu.editor.ui.uiUtils,
  27783. ColorPicker = baidu.editor.ui.ColorPicker,
  27784. Popup = baidu.editor.ui.Popup,
  27785. SplitButton = baidu.editor.ui.SplitButton,
  27786. ColorButton = (baidu.editor.ui.ColorButton = function(options) {
  27787. this.initOptions(options)
  27788. this.initColorButton()
  27789. })
  27790. ColorButton.prototype = {
  27791. initColorButton: function() {
  27792. var me = this
  27793. this.popup = new Popup({
  27794. content: new ColorPicker({
  27795. noColorText: me.editor.getLang('clearColor'),
  27796. editor: me.editor,
  27797. onpickcolor: function(t, color) {
  27798. me._onPickColor(color)
  27799. },
  27800. onpicknocolor: function(t, color) {
  27801. me._onPickNoColor(color)
  27802. }
  27803. }),
  27804. editor: me.editor
  27805. })
  27806. this.initSplitButton()
  27807. },
  27808. _SplitButton_postRender: SplitButton.prototype.postRender,
  27809. postRender: function() {
  27810. this._SplitButton_postRender()
  27811. this.getDom('button_body').appendChild(
  27812. uiUtils.createElementByHtml(
  27813. '<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>'
  27814. )
  27815. )
  27816. this.getDom().className += ' edui-colorbutton'
  27817. },
  27818. setColor: function(color) {
  27819. this.getDom('colorlump').style.backgroundColor = color
  27820. this.color = color
  27821. },
  27822. _onPickColor: function(color) {
  27823. if (this.fireEvent('pickcolor', color) !== false) {
  27824. this.setColor(color)
  27825. this.popup.hide()
  27826. }
  27827. },
  27828. _onPickNoColor: function(color) {
  27829. if (this.fireEvent('picknocolor') !== false) {
  27830. this.popup.hide()
  27831. }
  27832. }
  27833. }
  27834. utils.inherits(ColorButton, SplitButton)
  27835. })()
  27836. // ui/tablebutton.js
  27837. ///import core
  27838. ///import uicore
  27839. ///import ui/popup.js
  27840. ///import ui/tablepicker.js
  27841. ///import ui/splitbutton.js
  27842. ;(function() {
  27843. var utils = baidu.editor.utils,
  27844. Popup = baidu.editor.ui.Popup,
  27845. TablePicker = baidu.editor.ui.TablePicker,
  27846. SplitButton = baidu.editor.ui.SplitButton,
  27847. TableButton = (baidu.editor.ui.TableButton = function(options) {
  27848. this.initOptions(options)
  27849. this.initTableButton()
  27850. })
  27851. TableButton.prototype = {
  27852. initTableButton: function() {
  27853. var me = this
  27854. this.popup = new Popup({
  27855. content: new TablePicker({
  27856. editor: me.editor,
  27857. onpicktable: function(t, numCols, numRows) {
  27858. me._onPickTable(numCols, numRows)
  27859. }
  27860. }),
  27861. editor: me.editor
  27862. })
  27863. this.initSplitButton()
  27864. },
  27865. _onPickTable: function(numCols, numRows) {
  27866. if (this.fireEvent('picktable', numCols, numRows) !== false) {
  27867. this.popup.hide()
  27868. }
  27869. }
  27870. }
  27871. utils.inherits(TableButton, SplitButton)
  27872. })()
  27873. // ui/autotypesetpicker.js
  27874. ///import core
  27875. ///import uicore
  27876. ;(function() {
  27877. var utils = baidu.editor.utils,
  27878. UIBase = baidu.editor.ui.UIBase
  27879. var AutoTypeSetPicker = (baidu.editor.ui.AutoTypeSetPicker = function(
  27880. options
  27881. ) {
  27882. this.initOptions(options)
  27883. this.initAutoTypeSetPicker()
  27884. })
  27885. AutoTypeSetPicker.prototype = {
  27886. initAutoTypeSetPicker: function() {
  27887. this.initUIBase()
  27888. },
  27889. getHtmlTpl: function() {
  27890. var me = this.editor,
  27891. opt = me.options.autotypeset,
  27892. lang = me.getLang('autoTypeSet')
  27893. var textAlignInputName = 'textAlignValue' + me.uid,
  27894. imageBlockInputName = 'imageBlockLineValue' + me.uid,
  27895. symbolConverInputName = 'symbolConverValue' + me.uid
  27896. return (
  27897. '<div id="##" class="edui-autotypesetpicker %%">' +
  27898. '<div class="edui-autotypesetpicker-body">' +
  27899. '<table >' +
  27900. '<tr><td nowrap><input type="checkbox" name="mergeEmptyline" ' +
  27901. (opt['mergeEmptyline'] ? 'checked' : '') +
  27902. '>' +
  27903. lang.mergeLine +
  27904. '</td><td colspan="2"><input type="checkbox" name="removeEmptyline" ' +
  27905. (opt['removeEmptyline'] ? 'checked' : '') +
  27906. '>' +
  27907. lang.delLine +
  27908. '</td></tr>' +
  27909. '<tr><td nowrap><input type="checkbox" name="removeClass" ' +
  27910. (opt['removeClass'] ? 'checked' : '') +
  27911. '>' +
  27912. lang.removeFormat +
  27913. '</td><td colspan="2"><input type="checkbox" name="indent" ' +
  27914. (opt['indent'] ? 'checked' : '') +
  27915. '>' +
  27916. lang.indent +
  27917. '</td></tr>' +
  27918. '<tr>' +
  27919. '<td nowrap><input type="checkbox" name="textAlign" ' +
  27920. (opt['textAlign'] ? 'checked' : '') +
  27921. '>' +
  27922. lang.alignment +
  27923. '</td>' +
  27924. '<td colspan="2" id="' +
  27925. textAlignInputName +
  27926. '">' +
  27927. '<input type="radio" name="' +
  27928. textAlignInputName +
  27929. '" value="left" ' +
  27930. (opt['textAlign'] && opt['textAlign'] == 'left' ? 'checked' : '') +
  27931. '>' +
  27932. me.getLang('justifyleft') +
  27933. '<input type="radio" name="' +
  27934. textAlignInputName +
  27935. '" value="center" ' +
  27936. (opt['textAlign'] && opt['textAlign'] == 'center' ? 'checked' : '') +
  27937. '>' +
  27938. me.getLang('justifycenter') +
  27939. '<input type="radio" name="' +
  27940. textAlignInputName +
  27941. '" value="right" ' +
  27942. (opt['textAlign'] && opt['textAlign'] == 'right' ? 'checked' : '') +
  27943. '>' +
  27944. me.getLang('justifyright') +
  27945. '</td>' +
  27946. '</tr>' +
  27947. '<tr>' +
  27948. '<td nowrap><input type="checkbox" name="imageBlockLine" ' +
  27949. (opt['imageBlockLine'] ? 'checked' : '') +
  27950. '>' +
  27951. lang.imageFloat +
  27952. '</td>' +
  27953. '<td nowrap id="' +
  27954. imageBlockInputName +
  27955. '">' +
  27956. '<input type="radio" name="' +
  27957. imageBlockInputName +
  27958. '" value="none" ' +
  27959. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'none'
  27960. ? 'checked'
  27961. : '') +
  27962. '>' +
  27963. me.getLang('default') +
  27964. '<input type="radio" name="' +
  27965. imageBlockInputName +
  27966. '" value="left" ' +
  27967. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'left'
  27968. ? 'checked'
  27969. : '') +
  27970. '>' +
  27971. me.getLang('justifyleft') +
  27972. '<input type="radio" name="' +
  27973. imageBlockInputName +
  27974. '" value="center" ' +
  27975. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'center'
  27976. ? 'checked'
  27977. : '') +
  27978. '>' +
  27979. me.getLang('justifycenter') +
  27980. '<input type="radio" name="' +
  27981. imageBlockInputName +
  27982. '" value="right" ' +
  27983. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'right'
  27984. ? 'checked'
  27985. : '') +
  27986. '>' +
  27987. me.getLang('justifyright') +
  27988. '</td>' +
  27989. '</tr>' +
  27990. '<tr><td nowrap><input type="checkbox" name="clearFontSize" ' +
  27991. (opt['clearFontSize'] ? 'checked' : '') +
  27992. '>' +
  27993. lang.removeFontsize +
  27994. '</td><td colspan="2"><input type="checkbox" name="clearFontFamily" ' +
  27995. (opt['clearFontFamily'] ? 'checked' : '') +
  27996. '>' +
  27997. lang.removeFontFamily +
  27998. '</td></tr>' +
  27999. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' +
  28000. (opt['removeEmptyNode'] ? 'checked' : '') +
  28001. '>' +
  28002. lang.removeHtml +
  28003. '</td></tr>' +
  28004. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' +
  28005. (opt['pasteFilter'] ? 'checked' : '') +
  28006. '>' +
  28007. lang.pasteFilter +
  28008. '</td></tr>' +
  28009. '<tr>' +
  28010. '<td nowrap><input type="checkbox" name="symbolConver" ' +
  28011. (opt['bdc2sb'] || opt['tobdc'] ? 'checked' : '') +
  28012. '>' +
  28013. lang.symbol +
  28014. '</td>' +
  28015. '<td id="' +
  28016. symbolConverInputName +
  28017. '">' +
  28018. '<input type="radio" name="bdc" value="bdc2sb" ' +
  28019. (opt['bdc2sb'] ? 'checked' : '') +
  28020. '>' +
  28021. lang.bdc2sb +
  28022. '<input type="radio" name="bdc" value="tobdc" ' +
  28023. (opt['tobdc'] ? 'checked' : '') +
  28024. '>' +
  28025. lang.tobdc +
  28026. '' +
  28027. '</td>' +
  28028. '<td nowrap align="right"><button >' +
  28029. lang.run +
  28030. '</button></td>' +
  28031. '</tr>' +
  28032. '</table>' +
  28033. '</div>' +
  28034. '</div>'
  28035. )
  28036. },
  28037. _UIBase_render: UIBase.prototype.render
  28038. }
  28039. utils.inherits(AutoTypeSetPicker, UIBase)
  28040. })()
  28041. // ui/autotypesetbutton.js
  28042. ///import core
  28043. ///import uicore
  28044. ///import ui/popup.js
  28045. ///import ui/autotypesetpicker.js
  28046. ///import ui/splitbutton.js
  28047. ;(function() {
  28048. var utils = baidu.editor.utils,
  28049. Popup = baidu.editor.ui.Popup,
  28050. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  28051. SplitButton = baidu.editor.ui.SplitButton,
  28052. AutoTypeSetButton = (baidu.editor.ui.AutoTypeSetButton = function(
  28053. options
  28054. ) {
  28055. this.initOptions(options)
  28056. this.initAutoTypeSetButton()
  28057. })
  28058. function getPara(me) {
  28059. var opt = {},
  28060. cont = me.getDom(),
  28061. editorId = me.editor.uid,
  28062. inputType = null,
  28063. attrName = null,
  28064. ipts = domUtils.getElementsByTagName(cont, 'input')
  28065. for (var i = ipts.length - 1, ipt; (ipt = ipts[i--]); ) {
  28066. inputType = ipt.getAttribute('type')
  28067. if (inputType == 'checkbox') {
  28068. attrName = ipt.getAttribute('name')
  28069. opt[attrName] && delete opt[attrName]
  28070. if (ipt.checked) {
  28071. var attrValue = document.getElementById(
  28072. attrName + 'Value' + editorId
  28073. )
  28074. if (attrValue) {
  28075. if (/input/gi.test(attrValue.tagName)) {
  28076. opt[attrName] = attrValue.value
  28077. } else {
  28078. var iptChilds = attrValue.getElementsByTagName('input')
  28079. for (
  28080. var j = iptChilds.length - 1, iptchild;
  28081. (iptchild = iptChilds[j--]);
  28082. ) {
  28083. if (iptchild.checked) {
  28084. opt[attrName] = iptchild.value
  28085. break
  28086. }
  28087. }
  28088. }
  28089. } else {
  28090. opt[attrName] = true
  28091. }
  28092. } else {
  28093. opt[attrName] = false
  28094. }
  28095. } else {
  28096. opt[ipt.getAttribute('value')] = ipt.checked
  28097. }
  28098. }
  28099. var selects = domUtils.getElementsByTagName(cont, 'select')
  28100. for (var i = 0, si; (si = selects[i++]); ) {
  28101. var attr = si.getAttribute('name')
  28102. opt[attr] = opt[attr] ? si.value : ''
  28103. }
  28104. utils.extend(me.editor.options.autotypeset, opt)
  28105. me.editor.setPreferences('autotypeset', opt)
  28106. }
  28107. AutoTypeSetButton.prototype = {
  28108. initAutoTypeSetButton: function() {
  28109. var me = this
  28110. this.popup = new Popup({
  28111. //传入配置参数
  28112. content: new AutoTypeSetPicker({editor: me.editor}),
  28113. editor: me.editor,
  28114. hide: function() {
  28115. if (!this._hidden && this.getDom()) {
  28116. getPara(this)
  28117. this.getDom().style.display = 'none'
  28118. this._hidden = true
  28119. this.fireEvent('hide')
  28120. }
  28121. }
  28122. })
  28123. var flag = 0
  28124. this.popup.addListener('postRenderAfter', function() {
  28125. var popupUI = this
  28126. if (flag) return
  28127. var cont = this.getDom(),
  28128. btn = cont.getElementsByTagName('button')[0]
  28129. btn.onclick = function() {
  28130. getPara(popupUI)
  28131. me.editor.execCommand('autotypeset')
  28132. popupUI.hide()
  28133. }
  28134. domUtils.on(cont, 'click', function(e) {
  28135. var target = e.target || e.srcElement,
  28136. editorId = me.editor.uid
  28137. if (target && target.tagName == 'INPUT') {
  28138. // 点击图片浮动的checkbox,去除对应的radio
  28139. if (
  28140. target.name == 'imageBlockLine' ||
  28141. target.name == 'textAlign' ||
  28142. target.name == 'symbolConver'
  28143. ) {
  28144. var checked = target.checked,
  28145. radioTd = document.getElementById(
  28146. target.name + 'Value' + editorId
  28147. ),
  28148. radios = radioTd.getElementsByTagName('input'),
  28149. defalutSelect = {
  28150. imageBlockLine: 'none',
  28151. textAlign: 'left',
  28152. symbolConver: 'tobdc'
  28153. }
  28154. for (var i = 0; i < radios.length; i++) {
  28155. if (checked) {
  28156. if (radios[i].value == defalutSelect[target.name]) {
  28157. radios[i].checked = 'checked'
  28158. }
  28159. } else {
  28160. radios[i].checked = false
  28161. }
  28162. }
  28163. }
  28164. // 点击radio,选中对应的checkbox
  28165. if (
  28166. target.name == 'imageBlockLineValue' + editorId ||
  28167. target.name == 'textAlignValue' + editorId ||
  28168. target.name == 'bdc'
  28169. ) {
  28170. var checkboxs = target.parentNode.previousSibling.getElementsByTagName(
  28171. 'input'
  28172. )
  28173. checkboxs && (checkboxs[0].checked = true)
  28174. }
  28175. getPara(popupUI)
  28176. }
  28177. })
  28178. flag = 1
  28179. })
  28180. this.initSplitButton()
  28181. }
  28182. }
  28183. utils.inherits(AutoTypeSetButton, SplitButton)
  28184. })()
  28185. // ui/cellalignpicker.js
  28186. ///import core
  28187. ///import uicore
  28188. ;(function() {
  28189. var utils = baidu.editor.utils,
  28190. Popup = baidu.editor.ui.Popup,
  28191. Stateful = baidu.editor.ui.Stateful,
  28192. UIBase = baidu.editor.ui.UIBase
  28193. /**
  28194. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  28195. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  28196. * @update 2013/4/2 hancong03@baidu.com
  28197. */
  28198. var CellAlignPicker = (baidu.editor.ui.CellAlignPicker = function(options) {
  28199. this.initOptions(options)
  28200. this.initSelected()
  28201. this.initCellAlignPicker()
  28202. })
  28203. CellAlignPicker.prototype = {
  28204. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  28205. initSelected: function() {
  28206. var status = {
  28207. valign: {
  28208. top: 0,
  28209. middle: 1,
  28210. bottom: 2
  28211. },
  28212. align: {
  28213. left: 0,
  28214. center: 1,
  28215. right: 2
  28216. },
  28217. count: 3
  28218. },
  28219. result = -1
  28220. if (this.selected) {
  28221. this.selectedIndex =
  28222. status.valign[this.selected.valign] * status.count +
  28223. status.align[this.selected.align]
  28224. }
  28225. },
  28226. initCellAlignPicker: function() {
  28227. this.initUIBase()
  28228. this.Stateful_init()
  28229. },
  28230. getHtmlTpl: function() {
  28231. var alignType = ['left', 'center', 'right'],
  28232. COUNT = 9,
  28233. tempClassName = null,
  28234. tempIndex = -1,
  28235. tmpl = []
  28236. for (var i = 0; i < COUNT; i++) {
  28237. tempClassName =
  28238. this.selectedIndex === i ? ' class="edui-cellalign-selected" ' : ''
  28239. tempIndex = i % 3
  28240. tempIndex === 0 && tmpl.push('<tr>')
  28241. tmpl.push(
  28242. '<td index="' +
  28243. i +
  28244. '" ' +
  28245. tempClassName +
  28246. ' stateful><div class="edui-icon edui-' +
  28247. alignType[tempIndex] +
  28248. '"></div></td>'
  28249. )
  28250. tempIndex === 2 && tmpl.push('</tr>')
  28251. }
  28252. return (
  28253. '<div id="##" class="edui-cellalignpicker %%">' +
  28254. '<div class="edui-cellalignpicker-body">' +
  28255. '<table onclick="$$._onClick(event);">' +
  28256. tmpl.join('') +
  28257. '</table>' +
  28258. '</div>' +
  28259. '</div>'
  28260. )
  28261. },
  28262. getStateDom: function() {
  28263. return this.target
  28264. },
  28265. _onClick: function(evt) {
  28266. var target = evt.target || evt.srcElement
  28267. if (/icon/.test(target.className)) {
  28268. this.items[target.parentNode.getAttribute('index')].onclick()
  28269. Popup.postHide(evt)
  28270. }
  28271. },
  28272. _UIBase_render: UIBase.prototype.render
  28273. }
  28274. utils.inherits(CellAlignPicker, UIBase)
  28275. utils.extend(CellAlignPicker.prototype, Stateful, true)
  28276. })()
  28277. // ui/pastepicker.js
  28278. ///import core
  28279. ///import uicore
  28280. ;(function() {
  28281. var utils = baidu.editor.utils,
  28282. Stateful = baidu.editor.ui.Stateful,
  28283. uiUtils = baidu.editor.ui.uiUtils,
  28284. UIBase = baidu.editor.ui.UIBase
  28285. var PastePicker = (baidu.editor.ui.PastePicker = function(options) {
  28286. this.initOptions(options)
  28287. this.initPastePicker()
  28288. })
  28289. PastePicker.prototype = {
  28290. initPastePicker: function() {
  28291. this.initUIBase()
  28292. this.Stateful_init()
  28293. },
  28294. getHtmlTpl: function() {
  28295. return (
  28296. '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  28297. '<div class="edui-pastecontainer">' +
  28298. '<div class="edui-title">' +
  28299. this.editor.getLang('pasteOpt') +
  28300. '</div>' +
  28301. '<div class="edui-button">' +
  28302. '<div title="' +
  28303. this.editor.getLang('pasteSourceFormat') +
  28304. '" onclick="$$.format(false)" stateful>' +
  28305. '<div class="edui-richtxticon"></div></div>' +
  28306. '<div title="' +
  28307. this.editor.getLang('tagFormat') +
  28308. '" onclick="$$.format(2)" stateful>' +
  28309. '<div class="edui-tagicon"></div></div>' +
  28310. '<div title="' +
  28311. this.editor.getLang('pasteTextFormat') +
  28312. '" onclick="$$.format(true)" stateful>' +
  28313. '<div class="edui-plaintxticon"></div></div>' +
  28314. '</div>' +
  28315. '</div>' +
  28316. '</div>'
  28317. )
  28318. },
  28319. getStateDom: function() {
  28320. return this.target
  28321. },
  28322. format: function(param) {
  28323. this.editor.ui._isTransfer = true
  28324. this.editor.fireEvent('pasteTransfer', param)
  28325. },
  28326. _onClick: function(cur) {
  28327. var node = domUtils.getNextDomNode(cur),
  28328. screenHt = uiUtils.getViewportRect().height,
  28329. subPop = uiUtils.getClientRect(node)
  28330. if (subPop.top + subPop.height > screenHt)
  28331. node.style.top = -subPop.height - cur.offsetHeight + 'px'
  28332. else node.style.top = ''
  28333. if (/hidden/gi.test(domUtils.getComputedStyle(node, 'visibility'))) {
  28334. node.style.visibility = 'visible'
  28335. domUtils.addClass(cur, 'edui-state-opened')
  28336. } else {
  28337. node.style.visibility = 'hidden'
  28338. domUtils.removeClasses(cur, 'edui-state-opened')
  28339. }
  28340. },
  28341. _UIBase_render: UIBase.prototype.render
  28342. }
  28343. utils.inherits(PastePicker, UIBase)
  28344. utils.extend(PastePicker.prototype, Stateful, true)
  28345. })()
  28346. // ui/toolbar.js
  28347. ;(function() {
  28348. var utils = baidu.editor.utils,
  28349. uiUtils = baidu.editor.ui.uiUtils,
  28350. UIBase = baidu.editor.ui.UIBase,
  28351. Toolbar = (baidu.editor.ui.Toolbar = function(options) {
  28352. this.initOptions(options)
  28353. this.initToolbar()
  28354. })
  28355. Toolbar.prototype = {
  28356. items: null,
  28357. initToolbar: function() {
  28358. this.items = this.items || []
  28359. this.initUIBase()
  28360. },
  28361. add: function(item, index) {
  28362. if (index === undefined) {
  28363. this.items.push(item)
  28364. } else {
  28365. this.items.splice(index, 0, item)
  28366. }
  28367. },
  28368. getHtmlTpl: function() {
  28369. var buff = []
  28370. for (var i = 0; i < this.items.length; i++) {
  28371. buff[i] = this.items[i].renderHtml()
  28372. }
  28373. return (
  28374. '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  28375. buff.join('') +
  28376. '</div>'
  28377. )
  28378. },
  28379. postRender: function() {
  28380. var box = this.getDom()
  28381. for (var i = 0; i < this.items.length; i++) {
  28382. this.items[i].postRender()
  28383. }
  28384. uiUtils.makeUnselectable(box)
  28385. },
  28386. _onMouseDown: function(e) {
  28387. var target = e.target || e.srcElement,
  28388. tagName = target && target.tagName && target.tagName.toLowerCase()
  28389. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  28390. return false
  28391. }
  28392. }
  28393. }
  28394. utils.inherits(Toolbar, UIBase)
  28395. })()
  28396. // ui/menu.js
  28397. ///import core
  28398. ///import uicore
  28399. ///import ui\popup.js
  28400. ///import ui\stateful.js
  28401. ;(function() {
  28402. var utils = baidu.editor.utils,
  28403. domUtils = baidu.editor.dom.domUtils,
  28404. uiUtils = baidu.editor.ui.uiUtils,
  28405. UIBase = baidu.editor.ui.UIBase,
  28406. Popup = baidu.editor.ui.Popup,
  28407. Stateful = baidu.editor.ui.Stateful,
  28408. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  28409. Menu = (baidu.editor.ui.Menu = function(options) {
  28410. this.initOptions(options)
  28411. this.initMenu()
  28412. })
  28413. var menuSeparator = {
  28414. renderHtml: function() {
  28415. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>'
  28416. },
  28417. postRender: function() {},
  28418. queryAutoHide: function() {
  28419. return true
  28420. }
  28421. }
  28422. Menu.prototype = {
  28423. items: null,
  28424. uiName: 'menu',
  28425. initMenu: function() {
  28426. this.items = this.items || []
  28427. this.initPopup()
  28428. this.initItems()
  28429. },
  28430. initItems: function() {
  28431. for (var i = 0; i < this.items.length; i++) {
  28432. var item = this.items[i]
  28433. if (item == '-') {
  28434. this.items[i] = this.getSeparator()
  28435. } else if (!(item instanceof MenuItem)) {
  28436. item.editor = this.editor
  28437. item.theme = this.editor.options.theme
  28438. this.items[i] = this.createItem(item)
  28439. }
  28440. }
  28441. },
  28442. getSeparator: function() {
  28443. return menuSeparator
  28444. },
  28445. createItem: function(item) {
  28446. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28447. item.menu = this
  28448. return new MenuItem(item)
  28449. },
  28450. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  28451. getContentHtmlTpl: function() {
  28452. if (this.items.length == 0) {
  28453. return this._Popup_getContentHtmlTpl()
  28454. }
  28455. var buff = []
  28456. for (var i = 0; i < this.items.length; i++) {
  28457. var item = this.items[i]
  28458. buff[i] = item.renderHtml()
  28459. }
  28460. return '<div class="%%-body">' + buff.join('') + '</div>'
  28461. },
  28462. _Popup_postRender: Popup.prototype.postRender,
  28463. postRender: function() {
  28464. var me = this
  28465. for (var i = 0; i < this.items.length; i++) {
  28466. var item = this.items[i]
  28467. item.ownerMenu = this
  28468. item.postRender()
  28469. }
  28470. domUtils.on(this.getDom(), 'mouseover', function(evt) {
  28471. evt = evt || event
  28472. var rel = evt.relatedTarget || evt.fromElement
  28473. var el = me.getDom()
  28474. if (!uiUtils.contains(el, rel) && el !== rel) {
  28475. me.fireEvent('over')
  28476. }
  28477. })
  28478. this._Popup_postRender()
  28479. },
  28480. queryAutoHide: function(el) {
  28481. if (el) {
  28482. if (uiUtils.contains(this.getDom(), el)) {
  28483. return false
  28484. }
  28485. for (var i = 0; i < this.items.length; i++) {
  28486. var item = this.items[i]
  28487. if (item.queryAutoHide(el) === false) {
  28488. return false
  28489. }
  28490. }
  28491. }
  28492. },
  28493. clearItems: function() {
  28494. for (var i = 0; i < this.items.length; i++) {
  28495. var item = this.items[i]
  28496. clearTimeout(item._showingTimer)
  28497. clearTimeout(item._closingTimer)
  28498. if (item.subMenu) {
  28499. item.subMenu.destroy()
  28500. }
  28501. }
  28502. this.items = []
  28503. },
  28504. destroy: function() {
  28505. if (this.getDom()) {
  28506. domUtils.remove(this.getDom())
  28507. }
  28508. this.clearItems()
  28509. },
  28510. dispose: function() {
  28511. this.destroy()
  28512. }
  28513. }
  28514. utils.inherits(Menu, Popup)
  28515. /**
  28516. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28517. * @type {Function}
  28518. */
  28519. var MenuItem = (baidu.editor.ui.MenuItem = function(options) {
  28520. this.initOptions(options)
  28521. this.initUIBase()
  28522. this.Stateful_init()
  28523. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  28524. if (options.className && options.className.indexOf('aligntd') != -1) {
  28525. var me = this
  28526. //获取单元格对齐初始状态
  28527. this.subMenu.selected = this.editor.queryCommandValue('cellalignment')
  28528. this.subMenu = new Popup({
  28529. content: new CellAlignPicker(this.subMenu),
  28530. parentMenu: me,
  28531. editor: me.editor,
  28532. destroy: function() {
  28533. if (this.getDom()) {
  28534. domUtils.remove(this.getDom())
  28535. }
  28536. }
  28537. })
  28538. this.subMenu.addListener('postRenderAfter', function() {
  28539. domUtils.on(this.getDom(), 'mouseover', function() {
  28540. me.addState('opened')
  28541. })
  28542. })
  28543. } else {
  28544. this.subMenu = new Menu(this.subMenu)
  28545. }
  28546. }
  28547. })
  28548. MenuItem.prototype = {
  28549. label: '',
  28550. subMenu: null,
  28551. ownerMenu: null,
  28552. uiName: 'menuitem',
  28553. alwalysHoverable: true,
  28554. getHtmlTpl: function() {
  28555. return (
  28556. '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  28557. '<div class="%%-body">' +
  28558. this.renderLabelHtml() +
  28559. '</div>' +
  28560. '</div>'
  28561. )
  28562. },
  28563. postRender: function() {
  28564. var me = this
  28565. this.addListener('over', function() {
  28566. me.ownerMenu.fireEvent('submenuover', me)
  28567. if (me.subMenu) {
  28568. me.delayShowSubMenu()
  28569. }
  28570. })
  28571. if (this.subMenu) {
  28572. this.getDom().className += ' edui-hassubmenu'
  28573. this.subMenu.render()
  28574. this.addListener('out', function() {
  28575. me.delayHideSubMenu()
  28576. })
  28577. this.subMenu.addListener('over', function() {
  28578. clearTimeout(me._closingTimer)
  28579. me._closingTimer = null
  28580. me.addState('opened')
  28581. })
  28582. this.ownerMenu.addListener('hide', function() {
  28583. me.hideSubMenu()
  28584. })
  28585. this.ownerMenu.addListener('submenuover', function(t, subMenu) {
  28586. if (subMenu !== me) {
  28587. me.delayHideSubMenu()
  28588. }
  28589. })
  28590. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide
  28591. this.subMenu.queryAutoHide = function(el) {
  28592. if (el && uiUtils.contains(me.getDom(), el)) {
  28593. return false
  28594. }
  28595. return this._bakQueryAutoHide(el)
  28596. }
  28597. }
  28598. this.getDom().style.tabIndex = '-1'
  28599. uiUtils.makeUnselectable(this.getDom())
  28600. this.Stateful_postRender()
  28601. },
  28602. delayShowSubMenu: function() {
  28603. var me = this
  28604. if (!me.isDisabled()) {
  28605. me.addState('opened')
  28606. clearTimeout(me._showingTimer)
  28607. clearTimeout(me._closingTimer)
  28608. me._closingTimer = null
  28609. me._showingTimer = setTimeout(function() {
  28610. me.showSubMenu()
  28611. }, 250)
  28612. }
  28613. },
  28614. delayHideSubMenu: function() {
  28615. var me = this
  28616. if (!me.isDisabled()) {
  28617. me.removeState('opened')
  28618. clearTimeout(me._showingTimer)
  28619. if (!me._closingTimer) {
  28620. me._closingTimer = setTimeout(function() {
  28621. if (!me.hasState('opened')) {
  28622. me.hideSubMenu()
  28623. }
  28624. me._closingTimer = null
  28625. }, 400)
  28626. }
  28627. }
  28628. },
  28629. renderLabelHtml: function() {
  28630. return (
  28631. '<div class="edui-arrow"></div>' +
  28632. '<div class="edui-box edui-icon"></div>' +
  28633. '<div class="edui-box edui-label %%-label">' +
  28634. (this.label || '') +
  28635. '</div>'
  28636. )
  28637. },
  28638. getStateDom: function() {
  28639. return this.getDom()
  28640. },
  28641. queryAutoHide: function(el) {
  28642. if (this.subMenu && this.hasState('opened')) {
  28643. return this.subMenu.queryAutoHide(el)
  28644. }
  28645. },
  28646. _onClick: function(event, this_) {
  28647. if (this.hasState('disabled')) return
  28648. if (this.fireEvent('click', event, this_) !== false) {
  28649. if (this.subMenu) {
  28650. this.showSubMenu()
  28651. } else {
  28652. Popup.postHide(event)
  28653. }
  28654. }
  28655. },
  28656. showSubMenu: function() {
  28657. var rect = uiUtils.getClientRect(this.getDom())
  28658. rect.right -= 5
  28659. rect.left += 2
  28660. rect.width -= 7
  28661. rect.top -= 4
  28662. rect.bottom += 4
  28663. rect.height += 8
  28664. this.subMenu.showAnchorRect(rect, true, true)
  28665. },
  28666. hideSubMenu: function() {
  28667. this.subMenu.hide()
  28668. }
  28669. }
  28670. utils.inherits(MenuItem, UIBase)
  28671. utils.extend(MenuItem.prototype, Stateful, true)
  28672. })()
  28673. // ui/combox.js
  28674. ///import core
  28675. ///import uicore
  28676. ///import ui/menu.js
  28677. ///import ui/splitbutton.js
  28678. ;(function() {
  28679. // todo: menu和item提成通用list
  28680. var utils = baidu.editor.utils,
  28681. uiUtils = baidu.editor.ui.uiUtils,
  28682. Menu = baidu.editor.ui.Menu,
  28683. SplitButton = baidu.editor.ui.SplitButton,
  28684. Combox = (baidu.editor.ui.Combox = function(options) {
  28685. this.initOptions(options)
  28686. this.initCombox()
  28687. })
  28688. Combox.prototype = {
  28689. uiName: 'combox',
  28690. onbuttonclick: function() {
  28691. this.showPopup()
  28692. },
  28693. initCombox: function() {
  28694. var me = this
  28695. this.items = this.items || []
  28696. for (var i = 0; i < this.items.length; i++) {
  28697. var item = this.items[i]
  28698. item.uiName = 'listitem'
  28699. item.index = i
  28700. item.onclick = function() {
  28701. me.selectByIndex(this.index)
  28702. }
  28703. }
  28704. this.popup = new Menu({
  28705. items: this.items,
  28706. uiName: 'list',
  28707. editor: this.editor,
  28708. captureWheel: true,
  28709. combox: this
  28710. })
  28711. this.initSplitButton()
  28712. },
  28713. _SplitButton_postRender: SplitButton.prototype.postRender,
  28714. postRender: function() {
  28715. this._SplitButton_postRender()
  28716. this.setLabel(this.label || '')
  28717. this.setValue(this.initValue || '')
  28718. },
  28719. showPopup: function() {
  28720. var rect = uiUtils.getClientRect(this.getDom())
  28721. rect.top += 1
  28722. rect.bottom -= 1
  28723. rect.height -= 2
  28724. this.popup.showAnchorRect(rect)
  28725. },
  28726. getValue: function() {
  28727. return this.value
  28728. },
  28729. setValue: function(value) {
  28730. var index = this.indexByValue(value)
  28731. if (index != -1) {
  28732. this.selectedIndex = index
  28733. this.setLabel(this.items[index].label)
  28734. this.value = this.items[index].value
  28735. } else {
  28736. this.selectedIndex = -1
  28737. this.setLabel(this.getLabelForUnknowValue(value))
  28738. this.value = value
  28739. }
  28740. },
  28741. setLabel: function(label) {
  28742. this.getDom('button_body').innerHTML = label
  28743. this.label = label
  28744. },
  28745. getLabelForUnknowValue: function(value) {
  28746. return value
  28747. },
  28748. indexByValue: function(value) {
  28749. for (var i = 0; i < this.items.length; i++) {
  28750. if (value == this.items[i].value) {
  28751. return i
  28752. }
  28753. }
  28754. return -1
  28755. },
  28756. getItem: function(index) {
  28757. return this.items[index]
  28758. },
  28759. selectByIndex: function(index) {
  28760. if (
  28761. index < this.items.length &&
  28762. this.fireEvent('select', index) !== false
  28763. ) {
  28764. this.selectedIndex = index
  28765. this.value = this.items[index].value
  28766. this.setLabel(this.items[index].label)
  28767. }
  28768. }
  28769. }
  28770. utils.inherits(Combox, SplitButton)
  28771. })()
  28772. // ui/dialog.js
  28773. ///import core
  28774. ///import uicore
  28775. ///import ui/mask.js
  28776. ///import ui/button.js
  28777. ;(function() {
  28778. var utils = baidu.editor.utils,
  28779. domUtils = baidu.editor.dom.domUtils,
  28780. uiUtils = baidu.editor.ui.uiUtils,
  28781. Mask = baidu.editor.ui.Mask,
  28782. UIBase = baidu.editor.ui.UIBase,
  28783. Button = baidu.editor.ui.Button,
  28784. Dialog = (baidu.editor.ui.Dialog = function(options) {
  28785. if (options.name) {
  28786. var name = options.name
  28787. var cssRules = options.cssRules
  28788. if (!options.className) {
  28789. options.className = 'edui-for-' + name
  28790. }
  28791. if (cssRules) {
  28792. options.cssRules =
  28793. '.edui-default .edui-for-' +
  28794. name +
  28795. ' .edui-dialog-content {' +
  28796. cssRules +
  28797. '}'
  28798. }
  28799. }
  28800. this.initOptions(
  28801. utils.extend(
  28802. {
  28803. autoReset: true,
  28804. draggable: true,
  28805. onok: function() {},
  28806. oncancel: function() {},
  28807. onclose: function(t, ok) {
  28808. return ok ? this.onok() : this.oncancel()
  28809. },
  28810. //是否控制dialog中的scroll事件, 默认为不阻止
  28811. holdScroll: false
  28812. },
  28813. options
  28814. )
  28815. )
  28816. this.initDialog()
  28817. })
  28818. var modalMask
  28819. var dragMask
  28820. var activeDialog
  28821. Dialog.prototype = {
  28822. draggable: false,
  28823. uiName: 'dialog',
  28824. initDialog: function() {
  28825. var me = this,
  28826. theme = this.editor.options.theme
  28827. if (this.cssRules) {
  28828. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules)
  28829. }
  28830. this.initUIBase()
  28831. this.modalMask =
  28832. modalMask ||
  28833. (modalMask = new Mask({
  28834. className: 'edui-dialog-modalmask',
  28835. theme: theme,
  28836. onclick: function() {
  28837. activeDialog && activeDialog.close(false)
  28838. }
  28839. }))
  28840. this.dragMask =
  28841. dragMask ||
  28842. (dragMask = new Mask({
  28843. className: 'edui-dialog-dragmask',
  28844. theme: theme
  28845. }))
  28846. this.closeButton = new Button({
  28847. className: 'edui-dialog-closebutton',
  28848. title: me.closeDialog,
  28849. theme: theme,
  28850. onclick: function() {
  28851. me.close(false)
  28852. }
  28853. })
  28854. this.fullscreen && this.initResizeEvent()
  28855. if (this.buttons) {
  28856. for (var i = 0; i < this.buttons.length; i++) {
  28857. if (!(this.buttons[i] instanceof Button)) {
  28858. this.buttons[i] = new Button(
  28859. utils.extend(
  28860. this.buttons[i],
  28861. {
  28862. editor: this.editor
  28863. },
  28864. true
  28865. )
  28866. )
  28867. }
  28868. }
  28869. }
  28870. },
  28871. initResizeEvent: function() {
  28872. var me = this
  28873. domUtils.on(window, 'resize', function() {
  28874. if (me._hidden || me._hidden === undefined) {
  28875. return
  28876. }
  28877. if (me.__resizeTimer) {
  28878. window.clearTimeout(me.__resizeTimer)
  28879. }
  28880. me.__resizeTimer = window.setTimeout(function() {
  28881. me.__resizeTimer = null
  28882. var dialogWrapNode = me.getDom(),
  28883. contentNode = me.getDom('content'),
  28884. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28885. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  28886. vpRect = uiUtils.getViewportRect()
  28887. contentNode.style.width =
  28888. vpRect.width - wrapRect.width + contentRect.width + 'px'
  28889. contentNode.style.height =
  28890. vpRect.height - wrapRect.height + contentRect.height + 'px'
  28891. dialogWrapNode.style.width = vpRect.width + 'px'
  28892. dialogWrapNode.style.height = vpRect.height + 'px'
  28893. me.fireEvent('resize')
  28894. }, 100)
  28895. })
  28896. },
  28897. fitSize: function() {
  28898. var popBodyEl = this.getDom('body')
  28899. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  28900. // uiUtils.removeStyle(popBodyEl, 'width');
  28901. // uiUtils.removeStyle(popBodyEl, 'height');
  28902. // }
  28903. var size = this.mesureSize()
  28904. popBodyEl.style.width = size.width + 'px'
  28905. popBodyEl.style.height = size.height + 'px'
  28906. return size
  28907. },
  28908. safeSetOffset: function(offset) {
  28909. var me = this
  28910. var el = me.getDom()
  28911. var vpRect = uiUtils.getViewportRect()
  28912. var rect = uiUtils.getClientRect(el)
  28913. var left = offset.left
  28914. if (left + rect.width > vpRect.right) {
  28915. left = vpRect.right - rect.width
  28916. }
  28917. var top = offset.top
  28918. if (top + rect.height > vpRect.bottom) {
  28919. top = vpRect.bottom - rect.height
  28920. }
  28921. el.style.left = Math.max(left, 0) + 'px'
  28922. el.style.top = Math.max(top, 0) + 'px'
  28923. },
  28924. showAtCenter: function() {
  28925. var vpRect = uiUtils.getViewportRect()
  28926. if (!this.fullscreen) {
  28927. this.getDom().style.display = ''
  28928. var popSize = this.fitSize()
  28929. var titleHeight = this.getDom('titlebar').offsetHeight | 0
  28930. var left = vpRect.width / 2 - popSize.width / 2
  28931. var top =
  28932. vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight
  28933. var popEl = this.getDom()
  28934. this.safeSetOffset({
  28935. left: Math.max(left | 0, 0),
  28936. top: Math.max(top | 0, 0)
  28937. })
  28938. if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
  28939. popEl.className += ' edui-state-centered'
  28940. }
  28941. } else {
  28942. var dialogWrapNode = this.getDom(),
  28943. contentNode = this.getDom('content')
  28944. dialogWrapNode.style.display = 'block'
  28945. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28946. contentRect = UE.ui.uiUtils.getClientRect(contentNode)
  28947. dialogWrapNode.style.left = '-100000px'
  28948. contentNode.style.width =
  28949. vpRect.width - wrapRect.width + contentRect.width + 'px'
  28950. contentNode.style.height =
  28951. vpRect.height - wrapRect.height + contentRect.height + 'px'
  28952. dialogWrapNode.style.width = vpRect.width + 'px'
  28953. dialogWrapNode.style.height = vpRect.height + 'px'
  28954. dialogWrapNode.style.left = 0
  28955. //保存环境的overflow值
  28956. this._originalContext = {
  28957. html: {
  28958. overflowX: document.documentElement.style.overflowX,
  28959. overflowY: document.documentElement.style.overflowY
  28960. },
  28961. body: {
  28962. overflowX: document.body.style.overflowX,
  28963. overflowY: document.body.style.overflowY
  28964. }
  28965. }
  28966. document.documentElement.style.overflowX = 'hidden'
  28967. document.documentElement.style.overflowY = 'hidden'
  28968. document.body.style.overflowX = 'hidden'
  28969. document.body.style.overflowY = 'hidden'
  28970. }
  28971. this._show()
  28972. },
  28973. getContentHtml: function() {
  28974. var contentHtml = ''
  28975. if (typeof this.content == 'string') {
  28976. contentHtml = this.content
  28977. } else if (this.iframeUrl) {
  28978. contentHtml =
  28979. '<span id="' +
  28980. this.id +
  28981. '_contmask" class="dialogcontmask"></span><iframe id="' +
  28982. this.id +
  28983. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' +
  28984. this.iframeUrl +
  28985. '"></iframe>'
  28986. }
  28987. return contentHtml
  28988. },
  28989. getHtmlTpl: function() {
  28990. var footHtml = ''
  28991. if (this.buttons) {
  28992. var buff = []
  28993. for (var i = 0; i < this.buttons.length; i++) {
  28994. buff[i] = this.buttons[i].renderHtml()
  28995. }
  28996. footHtml =
  28997. '<div class="%%-foot">' +
  28998. '<div id="##_buttons" class="%%-buttons">' +
  28999. buff.join('') +
  29000. '</div>' +
  29001. '</div>'
  29002. }
  29003. return (
  29004. '<div id="##" class="%%"><div ' +
  29005. (!this.fullscreen
  29006. ? 'class="%%"'
  29007. : 'class="%%-wrap edui-dialog-fullscreen-flag"') +
  29008. '><div id="##_body" class="%%-body">' +
  29009. '<div class="%%-shadow"></div>' +
  29010. '<div id="##_titlebar" class="%%-titlebar">' +
  29011. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  29012. '<span class="%%-caption">' +
  29013. (this.title || '') +
  29014. '</span>' +
  29015. '</div>' +
  29016. this.closeButton.renderHtml() +
  29017. '</div>' +
  29018. '<div id="##_content" class="%%-content">' +
  29019. (this.autoReset ? '' : this.getContentHtml()) +
  29020. '</div>' +
  29021. footHtml +
  29022. '</div></div></div>'
  29023. )
  29024. },
  29025. postRender: function() {
  29026. // todo: 保持居中/记住上次关闭位置选项
  29027. if (!this.modalMask.getDom()) {
  29028. this.modalMask.render()
  29029. this.modalMask.hide()
  29030. }
  29031. if (!this.dragMask.getDom()) {
  29032. this.dragMask.render()
  29033. this.dragMask.hide()
  29034. }
  29035. var me = this
  29036. this.addListener('show', function() {
  29037. me.modalMask.show(this.getDom().style.zIndex - 2)
  29038. })
  29039. this.addListener('hide', function() {
  29040. me.modalMask.hide()
  29041. })
  29042. if (this.buttons) {
  29043. for (var i = 0; i < this.buttons.length; i++) {
  29044. this.buttons[i].postRender()
  29045. }
  29046. }
  29047. domUtils.on(window, 'resize', function() {
  29048. setTimeout(function() {
  29049. if (!me.isHidden()) {
  29050. me.safeSetOffset(uiUtils.getClientRect(me.getDom()))
  29051. }
  29052. })
  29053. })
  29054. //hold住scroll事件,防止dialog的滚动影响页面
  29055. // if( this.holdScroll ) {
  29056. //
  29057. // if( !me.iframeUrl ) {
  29058. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  29059. // domUtils.preventDefault(e);
  29060. // } );
  29061. // } else {
  29062. // me.addListener('dialogafterreset', function(){
  29063. // window.setTimeout(function(){
  29064. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  29065. //
  29066. // if( browser.ie ) {
  29067. //
  29068. // var timer = window.setInterval(function(){
  29069. //
  29070. // if( iframeWindow.document && iframeWindow.document.body ) {
  29071. // window.clearInterval( timer );
  29072. // timer = null;
  29073. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  29074. // domUtils.preventDefault(e);
  29075. // } );
  29076. // }
  29077. //
  29078. // }, 100);
  29079. //
  29080. // } else {
  29081. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  29082. // domUtils.preventDefault(e);
  29083. // } );
  29084. // }
  29085. //
  29086. // }, 1);
  29087. // });
  29088. // }
  29089. //
  29090. // }
  29091. this._hide()
  29092. },
  29093. mesureSize: function() {
  29094. var body = this.getDom('body')
  29095. var width = uiUtils.getClientRect(this.getDom('content')).width
  29096. var dialogBodyStyle = body.style
  29097. dialogBodyStyle.width = width
  29098. return uiUtils.getClientRect(body)
  29099. },
  29100. _onTitlebarMouseDown: function(evt, el) {
  29101. if (this.draggable) {
  29102. var rect
  29103. var vpRect = uiUtils.getViewportRect()
  29104. var me = this
  29105. uiUtils.startDrag(evt, {
  29106. ondragstart: function() {
  29107. rect = uiUtils.getClientRect(me.getDom())
  29108. me.getDom('contmask').style.visibility = 'visible'
  29109. me.dragMask.show(me.getDom().style.zIndex - 1)
  29110. },
  29111. ondragmove: function(x, y) {
  29112. var left = rect.left + x
  29113. var top = rect.top + y
  29114. me.safeSetOffset({
  29115. left: left,
  29116. top: top
  29117. })
  29118. },
  29119. ondragstop: function() {
  29120. me.getDom('contmask').style.visibility = 'hidden'
  29121. domUtils.removeClasses(me.getDom(), ['edui-state-centered'])
  29122. me.dragMask.hide()
  29123. }
  29124. })
  29125. }
  29126. },
  29127. reset: function() {
  29128. this.getDom('content').innerHTML = this.getContentHtml()
  29129. this.fireEvent('dialogafterreset')
  29130. },
  29131. _show: function() {
  29132. if (this._hidden) {
  29133. this.getDom().style.display = ''
  29134. //要高过编辑器的zindxe
  29135. this.editor.container.style.zIndex &&
  29136. (this.getDom().style.zIndex =
  29137. this.editor.container.style.zIndex * 1 + 10)
  29138. this._hidden = false
  29139. this.fireEvent('show')
  29140. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  29141. this.getDom().style.zIndex - 4
  29142. }
  29143. },
  29144. isHidden: function() {
  29145. return this._hidden
  29146. },
  29147. _hide: function() {
  29148. if (!this._hidden) {
  29149. var wrapNode = this.getDom()
  29150. wrapNode.style.display = 'none'
  29151. wrapNode.style.zIndex = ''
  29152. wrapNode.style.width = ''
  29153. wrapNode.style.height = ''
  29154. this._hidden = true
  29155. this.fireEvent('hide')
  29156. }
  29157. },
  29158. open: function() {
  29159. if (this.autoReset) {
  29160. //有可能还没有渲染
  29161. try {
  29162. this.reset()
  29163. } catch (e) {
  29164. this.render()
  29165. this.open()
  29166. }
  29167. }
  29168. this.showAtCenter()
  29169. if (this.iframeUrl) {
  29170. try {
  29171. this.getDom('iframe').focus()
  29172. } catch (ex) {}
  29173. }
  29174. activeDialog = this
  29175. },
  29176. _onCloseButtonClick: function(evt, el) {
  29177. this.close(false)
  29178. },
  29179. close: function(ok) {
  29180. if (this.fireEvent('close', ok) !== false) {
  29181. //还原环境
  29182. if (this.fullscreen) {
  29183. document.documentElement.style.overflowX = this._originalContext.html.overflowX
  29184. document.documentElement.style.overflowY = this._originalContext.html.overflowY
  29185. document.body.style.overflowX = this._originalContext.body.overflowX
  29186. document.body.style.overflowY = this._originalContext.body.overflowY
  29187. delete this._originalContext
  29188. }
  29189. this._hide()
  29190. //销毁content
  29191. var content = this.getDom('content')
  29192. var iframe = this.getDom('iframe')
  29193. if (content && iframe) {
  29194. var doc = iframe.contentDocument || iframe.contentWindow.document
  29195. doc && (doc.body.innerHTML = '')
  29196. domUtils.remove(content)
  29197. }
  29198. }
  29199. }
  29200. }
  29201. utils.inherits(Dialog, UIBase)
  29202. })()
  29203. // ui/menubutton.js
  29204. ///import core
  29205. ///import uicore
  29206. ///import ui/menu.js
  29207. ///import ui/splitbutton.js
  29208. ;(function() {
  29209. var utils = baidu.editor.utils,
  29210. Menu = baidu.editor.ui.Menu,
  29211. SplitButton = baidu.editor.ui.SplitButton,
  29212. MenuButton = (baidu.editor.ui.MenuButton = function(options) {
  29213. this.initOptions(options)
  29214. this.initMenuButton()
  29215. })
  29216. MenuButton.prototype = {
  29217. initMenuButton: function() {
  29218. var me = this
  29219. this.uiName = 'menubutton'
  29220. this.popup = new Menu({
  29221. items: me.items,
  29222. className: me.className,
  29223. editor: me.editor
  29224. })
  29225. this.popup.addListener('show', function() {
  29226. var list = this
  29227. for (var i = 0; i < list.items.length; i++) {
  29228. list.items[i].removeState('checked')
  29229. if (list.items[i].value == me._value) {
  29230. list.items[i].addState('checked')
  29231. this.value = me._value
  29232. }
  29233. }
  29234. })
  29235. this.initSplitButton()
  29236. },
  29237. setValue: function(value) {
  29238. this._value = value
  29239. }
  29240. }
  29241. utils.inherits(MenuButton, SplitButton)
  29242. })()
  29243. // ui/multiMenu.js
  29244. ///import core
  29245. ///import uicore
  29246. ///commands 表情
  29247. ;(function() {
  29248. var utils = baidu.editor.utils,
  29249. Popup = baidu.editor.ui.Popup,
  29250. SplitButton = baidu.editor.ui.SplitButton,
  29251. MultiMenuPop = (baidu.editor.ui.MultiMenuPop = function(options) {
  29252. this.initOptions(options)
  29253. this.initMultiMenu()
  29254. })
  29255. MultiMenuPop.prototype = {
  29256. initMultiMenu: function() {
  29257. var me = this
  29258. this.popup = new Popup({
  29259. content: '',
  29260. editor: me.editor,
  29261. iframe_rendered: false,
  29262. onshow: function() {
  29263. if (!this.iframe_rendered) {
  29264. this.iframe_rendered = true
  29265. this.getDom('content').innerHTML =
  29266. '<iframe id="' +
  29267. me.id +
  29268. '_iframe" src="' +
  29269. me.iframeUrl +
  29270. '" frameborder="0"></iframe>'
  29271. me.editor.container.style.zIndex &&
  29272. (this.getDom().style.zIndex =
  29273. me.editor.container.style.zIndex * 1 + 1)
  29274. }
  29275. }
  29276. // canSideUp:false,
  29277. // canSideLeft:false
  29278. })
  29279. this.onbuttonclick = function() {
  29280. this.showPopup()
  29281. }
  29282. this.initSplitButton()
  29283. }
  29284. }
  29285. utils.inherits(MultiMenuPop, SplitButton)
  29286. })()
  29287. // ui/shortcutmenu.js
  29288. ;(function() {
  29289. var UI = baidu.editor.ui,
  29290. UIBase = UI.UIBase,
  29291. uiUtils = UI.uiUtils,
  29292. utils = baidu.editor.utils,
  29293. domUtils = baidu.editor.dom.domUtils
  29294. var allMenus = [], //存储所有快捷菜单
  29295. timeID,
  29296. isSubMenuShow = false //是否有子pop显示
  29297. var ShortCutMenu = (UI.ShortCutMenu = function(options) {
  29298. this.initOptions(options)
  29299. this.initShortCutMenu()
  29300. })
  29301. ShortCutMenu.postHide = hideAllMenu
  29302. ShortCutMenu.prototype = {
  29303. isHidden: true,
  29304. SPACE: 5,
  29305. initShortCutMenu: function() {
  29306. this.items = this.items || []
  29307. this.initUIBase()
  29308. this.initItems()
  29309. this.initEvent()
  29310. allMenus.push(this)
  29311. },
  29312. initEvent: function() {
  29313. var me = this,
  29314. doc = me.editor.document
  29315. domUtils.on(doc, 'mousemove', function(e) {
  29316. if (me.isHidden === false) {
  29317. //有pop显示就不隐藏快捷菜单
  29318. if (me.getSubMenuMark() || me.eventType == 'contextmenu') return
  29319. var flag = true,
  29320. el = me.getDom(),
  29321. wt = el.offsetWidth,
  29322. ht = el.offsetHeight,
  29323. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  29324. distanceY = ht / 2, //距离中心Y标准
  29325. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  29326. y = Math.abs(e.screenY - me.top) //离中心距离纵坐标
  29327. clearTimeout(timeID)
  29328. timeID = setTimeout(function() {
  29329. if (y > 0 && y < distanceY) {
  29330. me.setOpacity(el, '1')
  29331. } else if (y > distanceY && y < distanceY + 70) {
  29332. me.setOpacity(el, '0.5')
  29333. flag = false
  29334. } else if (y > distanceY + 70 && y < distanceY + 140) {
  29335. me.hide()
  29336. }
  29337. if (flag && x > 0 && x < distanceX) {
  29338. me.setOpacity(el, '1')
  29339. } else if (x > distanceX && x < distanceX + 70) {
  29340. me.setOpacity(el, '0.5')
  29341. } else if (x > distanceX + 70 && x < distanceX + 140) {
  29342. me.hide()
  29343. }
  29344. })
  29345. }
  29346. })
  29347. //ie\ff下 mouseout不准
  29348. if (browser.chrome) {
  29349. domUtils.on(doc, 'mouseout', function(e) {
  29350. var relatedTgt = e.relatedTarget || e.toElement
  29351. if (relatedTgt == null || relatedTgt.tagName == 'HTML') {
  29352. me.hide()
  29353. }
  29354. })
  29355. }
  29356. me.editor.addListener('afterhidepop', function() {
  29357. if (!me.isHidden) {
  29358. isSubMenuShow = true
  29359. }
  29360. })
  29361. },
  29362. initItems: function() {
  29363. if (utils.isArray(this.items)) {
  29364. for (var i = 0, len = this.items.length; i < len; i++) {
  29365. var item = this.items[i].toLowerCase()
  29366. if (UI[item]) {
  29367. this.items[i] = new UI[item](this.editor)
  29368. this.items[i].className += ' edui-shortcutsubmenu '
  29369. }
  29370. }
  29371. }
  29372. },
  29373. setOpacity: function(el, value) {
  29374. if (browser.ie && browser.version < 9) {
  29375. el.style.filter = 'alpha(opacity = ' + parseFloat(value) * 100 + ');'
  29376. } else {
  29377. el.style.opacity = value
  29378. }
  29379. },
  29380. getSubMenuMark: function() {
  29381. isSubMenuShow = false
  29382. var layerEle = uiUtils.getFixedLayer()
  29383. var list = domUtils.getElementsByTagName(layerEle, 'div', function(
  29384. node
  29385. ) {
  29386. return domUtils.hasClass(node, 'edui-shortcutsubmenu edui-popup')
  29387. })
  29388. for (var i = 0, node; (node = list[i++]); ) {
  29389. if (node.style.display != 'none') {
  29390. isSubMenuShow = true
  29391. }
  29392. }
  29393. return isSubMenuShow
  29394. },
  29395. show: function(e, hasContextmenu) {
  29396. var me = this,
  29397. offset = {},
  29398. el = this.getDom(),
  29399. fixedlayer = uiUtils.getFixedLayer()
  29400. function setPos(offset) {
  29401. if (offset.left < 0) {
  29402. offset.left = 0
  29403. }
  29404. if (offset.top < 0) {
  29405. offset.top = 0
  29406. }
  29407. el.style.cssText =
  29408. 'position:absolute;left:' +
  29409. offset.left +
  29410. 'px;top:' +
  29411. offset.top +
  29412. 'px;'
  29413. }
  29414. function setPosByCxtMenu(menu) {
  29415. if (!menu.tagName) {
  29416. menu = menu.getDom()
  29417. }
  29418. offset.left = parseInt(menu.style.left)
  29419. offset.top = parseInt(menu.style.top)
  29420. offset.top -= el.offsetHeight + 15
  29421. setPos(offset)
  29422. }
  29423. me.eventType = e.type
  29424. el.style.cssText = 'display:block;left:-9999px'
  29425. if (e.type == 'contextmenu' && hasContextmenu) {
  29426. var menu = domUtils.getElementsByTagName(
  29427. fixedlayer,
  29428. 'div',
  29429. 'edui-contextmenu'
  29430. )[0]
  29431. if (menu) {
  29432. setPosByCxtMenu(menu)
  29433. } else {
  29434. me.editor.addListener('aftershowcontextmenu', function(type, menu) {
  29435. setPosByCxtMenu(menu)
  29436. })
  29437. }
  29438. } else {
  29439. offset = uiUtils.getViewportOffsetByEvent(e)
  29440. offset.top -= el.offsetHeight + me.SPACE
  29441. offset.left += me.SPACE + 20
  29442. setPos(offset)
  29443. me.setOpacity(el, 0.2)
  29444. }
  29445. me.isHidden = false
  29446. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE
  29447. me.top = e.screenY - el.offsetHeight / 2 - me.SPACE
  29448. if (me.editor) {
  29449. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10
  29450. fixedlayer.style.zIndex = el.style.zIndex - 1
  29451. }
  29452. },
  29453. hide: function() {
  29454. if (this.getDom()) {
  29455. this.getDom().style.display = 'none'
  29456. }
  29457. this.isHidden = true
  29458. },
  29459. postRender: function() {
  29460. if (utils.isArray(this.items)) {
  29461. for (var i = 0, item; (item = this.items[i++]); ) {
  29462. item.postRender()
  29463. }
  29464. }
  29465. },
  29466. getHtmlTpl: function() {
  29467. var buff
  29468. if (utils.isArray(this.items)) {
  29469. buff = []
  29470. for (var i = 0; i < this.items.length; i++) {
  29471. buff[i] = this.items[i].renderHtml()
  29472. }
  29473. buff = buff.join('')
  29474. } else {
  29475. buff = this.items
  29476. }
  29477. return (
  29478. '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  29479. buff +
  29480. '</div>'
  29481. )
  29482. }
  29483. }
  29484. utils.inherits(ShortCutMenu, UIBase)
  29485. function hideAllMenu(e) {
  29486. var tgt = e.target || e.srcElement,
  29487. cur = domUtils.findParent(
  29488. tgt,
  29489. function(node) {
  29490. return (
  29491. domUtils.hasClass(node, 'edui-shortcutmenu') ||
  29492. domUtils.hasClass(node, 'edui-popup')
  29493. )
  29494. },
  29495. true
  29496. )
  29497. if (!cur) {
  29498. for (var i = 0, menu; (menu = allMenus[i++]); ) {
  29499. menu.hide()
  29500. }
  29501. }
  29502. }
  29503. domUtils.on(document, 'mousedown', function(e) {
  29504. hideAllMenu(e)
  29505. })
  29506. domUtils.on(window, 'scroll', function(e) {
  29507. hideAllMenu(e)
  29508. })
  29509. })()
  29510. // ui/breakline.js
  29511. ;(function() {
  29512. var utils = baidu.editor.utils,
  29513. UIBase = baidu.editor.ui.UIBase,
  29514. Breakline = (baidu.editor.ui.Breakline = function(options) {
  29515. this.initOptions(options)
  29516. this.initSeparator()
  29517. })
  29518. Breakline.prototype = {
  29519. uiName: 'Breakline',
  29520. initSeparator: function() {
  29521. this.initUIBase()
  29522. },
  29523. getHtmlTpl: function() {
  29524. return '<br/>'
  29525. }
  29526. }
  29527. utils.inherits(Breakline, UIBase)
  29528. })()
  29529. // ui/message.js
  29530. ///import core
  29531. ///import uicore
  29532. ;(function() {
  29533. var utils = baidu.editor.utils,
  29534. domUtils = baidu.editor.dom.domUtils,
  29535. UIBase = baidu.editor.ui.UIBase,
  29536. Message = (baidu.editor.ui.Message = function(options) {
  29537. this.initOptions(options)
  29538. this.initMessage()
  29539. })
  29540. Message.prototype = {
  29541. initMessage: function() {
  29542. this.initUIBase()
  29543. },
  29544. getHtmlTpl: function() {
  29545. return (
  29546. '<div id="##" class="edui-message %%">' +
  29547. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  29548. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  29549. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  29550. ' <div class="edui-shadow"></div>' +
  29551. ' <div id="##_content" class="edui-message-content">' +
  29552. ' </div>' +
  29553. ' </div>' +
  29554. '</div>'
  29555. )
  29556. },
  29557. reset: function(opt) {
  29558. var me = this
  29559. if (!opt.keepshow) {
  29560. clearTimeout(this.timer)
  29561. me.timer = setTimeout(function() {
  29562. me.hide()
  29563. }, opt.timeout || 4000)
  29564. }
  29565. opt.content !== undefined && me.setContent(opt.content)
  29566. opt.type !== undefined && me.setType(opt.type)
  29567. me.show()
  29568. },
  29569. postRender: function() {
  29570. var me = this,
  29571. closer = this.getDom('closer')
  29572. closer &&
  29573. domUtils.on(closer, 'click', function() {
  29574. me.hide()
  29575. })
  29576. },
  29577. setContent: function(content) {
  29578. this.getDom('content').innerHTML = content
  29579. },
  29580. setType: function(type) {
  29581. type = type || 'info'
  29582. var body = this.getDom('body')
  29583. body.className = body.className.replace(
  29584. /edui-message-type-[\w-]+/,
  29585. 'edui-message-type-' + type
  29586. )
  29587. },
  29588. getContent: function() {
  29589. return this.getDom('content').innerHTML
  29590. },
  29591. getType: function() {
  29592. var arr = this.getDom('body').match(/edui-message-type-([\w-]+)/)
  29593. return arr ? arr[1] : ''
  29594. },
  29595. show: function() {
  29596. this.getDom().style.display = 'block'
  29597. },
  29598. hide: function() {
  29599. var dom = this.getDom()
  29600. if (dom) {
  29601. dom.style.display = 'none'
  29602. dom.parentNode && dom.parentNode.removeChild(dom)
  29603. }
  29604. }
  29605. }
  29606. utils.inherits(Message, UIBase)
  29607. })()
  29608. // adapter/editorui.js
  29609. //ui跟编辑器的适配層
  29610. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  29611. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  29612. ;(function() {
  29613. var utils = baidu.editor.utils
  29614. var editorui = baidu.editor.ui
  29615. var _Dialog = editorui.Dialog
  29616. editorui.buttons = {}
  29617. editorui.Dialog = function(options) {
  29618. var dialog = new _Dialog(options)
  29619. dialog.addListener('hide', function() {
  29620. if (dialog.editor) {
  29621. var editor = dialog.editor
  29622. try {
  29623. if (browser.gecko) {
  29624. var y = editor.window.scrollY,
  29625. x = editor.window.scrollX
  29626. editor.body.focus()
  29627. editor.window.scrollTo(x, y)
  29628. } else {
  29629. editor.focus()
  29630. }
  29631. } catch (ex) {}
  29632. }
  29633. })
  29634. return dialog
  29635. }
  29636. var iframeUrlMap = {
  29637. anchor: '~/dialogs/anchor/anchor.html',
  29638. insertimage: '~/dialogs/image/image.html',
  29639. link: '~/dialogs/link/link.html',
  29640. spechars: '~/dialogs/spechars/spechars.html',
  29641. searchreplace: '~/dialogs/searchreplace/searchreplace.html',
  29642. map: '~/dialogs/map/map.html',
  29643. gmap: '~/dialogs/gmap/gmap.html',
  29644. insertvideo: '~/dialogs/video/video.html',
  29645. help: '~/dialogs/help/help.html',
  29646. preview: '~/dialogs/preview/preview.html',
  29647. emotion: '~/dialogs/emotion/emotion.html',
  29648. wordimage: '~/dialogs/wordimage/wordimage.html',
  29649. attachment: '~/dialogs/attachment/attachment.html',
  29650. insertframe: '~/dialogs/insertframe/insertframe.html',
  29651. edittip: '~/dialogs/table/edittip.html',
  29652. edittable: '~/dialogs/table/edittable.html',
  29653. edittd: '~/dialogs/table/edittd.html',
  29654. webapp: '~/dialogs/webapp/webapp.html',
  29655. snapscreen: '~/dialogs/snapscreen/snapscreen.html',
  29656. scrawl: '~/dialogs/scrawl/scrawl.html',
  29657. music: '~/dialogs/music/music.html',
  29658. template: '~/dialogs/template/template.html',
  29659. background: '~/dialogs/background/background.html',
  29660. charts: '~/dialogs/charts/charts.html'
  29661. }
  29662. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  29663. var btnCmds = [
  29664. 'undo',
  29665. 'redo',
  29666. 'formatmatch',
  29667. 'bold',
  29668. 'italic',
  29669. 'underline',
  29670. 'fontborder',
  29671. 'touppercase',
  29672. 'tolowercase',
  29673. 'strikethrough',
  29674. 'subscript',
  29675. 'superscript',
  29676. 'source',
  29677. 'indent',
  29678. 'outdent',
  29679. 'blockquote',
  29680. 'pasteplain',
  29681. 'pagebreak',
  29682. 'selectall',
  29683. 'print',
  29684. 'horizontal',
  29685. 'removeformat',
  29686. 'time',
  29687. 'date',
  29688. 'unlink',
  29689. 'insertparagraphbeforetable',
  29690. 'insertrow',
  29691. 'insertcol',
  29692. 'mergeright',
  29693. 'mergedown',
  29694. 'deleterow',
  29695. 'deletecol',
  29696. 'splittorows',
  29697. 'splittocols',
  29698. 'splittocells',
  29699. 'mergecells',
  29700. 'deletetable',
  29701. 'drafts'
  29702. ]
  29703. for (var i = 0, ci; (ci = btnCmds[i++]); ) {
  29704. ci = ci.toLowerCase()
  29705. editorui[ci] = (function(cmd) {
  29706. return function(editor) {
  29707. var ui = new editorui.Button({
  29708. className: 'edui-for-' + cmd,
  29709. title:
  29710. editor.options.labelMap[cmd] ||
  29711. editor.getLang('labelMap.' + cmd) ||
  29712. '',
  29713. onclick: function() {
  29714. editor.execCommand(cmd)
  29715. },
  29716. theme: editor.options.theme,
  29717. showText: false
  29718. })
  29719. editorui.buttons[cmd] = ui
  29720. editor.addListener('selectionchange', function(
  29721. type,
  29722. causeByUi,
  29723. uiReady
  29724. ) {
  29725. var state = editor.queryCommandState(cmd)
  29726. if (state == -1) {
  29727. ui.setDisabled(true)
  29728. ui.setChecked(false)
  29729. } else {
  29730. if (!uiReady) {
  29731. ui.setDisabled(false)
  29732. ui.setChecked(state)
  29733. }
  29734. }
  29735. })
  29736. return ui
  29737. }
  29738. })(ci)
  29739. }
  29740. //清除文档
  29741. editorui.cleardoc = function(editor) {
  29742. var ui = new editorui.Button({
  29743. className: 'edui-for-cleardoc',
  29744. title:
  29745. editor.options.labelMap.cleardoc ||
  29746. editor.getLang('labelMap.cleardoc') ||
  29747. '',
  29748. theme: editor.options.theme,
  29749. onclick: function() {
  29750. if (confirm(editor.getLang('confirmClear'))) {
  29751. editor.execCommand('cleardoc')
  29752. }
  29753. }
  29754. })
  29755. editorui.buttons['cleardoc'] = ui
  29756. editor.addListener('selectionchange', function() {
  29757. ui.setDisabled(editor.queryCommandState('cleardoc') == -1)
  29758. })
  29759. return ui
  29760. }
  29761. //排版,图片排版,文字方向
  29762. var typeset = {
  29763. justify: ['left', 'right', 'center', 'justify'],
  29764. imagefloat: ['none', 'left', 'center', 'right'],
  29765. directionality: ['ltr', 'rtl']
  29766. }
  29767. for (var p in typeset) {
  29768. ;(function(cmd, val) {
  29769. for (var i = 0, ci; (ci = val[i++]); ) {
  29770. ;(function(cmd2) {
  29771. editorui[cmd.replace('float', '') + cmd2] = function(editor) {
  29772. var ui = new editorui.Button({
  29773. className: 'edui-for-' + cmd.replace('float', '') + cmd2,
  29774. title:
  29775. editor.options.labelMap[cmd.replace('float', '') + cmd2] ||
  29776. editor.getLang(
  29777. 'labelMap.' + cmd.replace('float', '') + cmd2
  29778. ) ||
  29779. '',
  29780. theme: editor.options.theme,
  29781. onclick: function() {
  29782. editor.execCommand(cmd, cmd2)
  29783. }
  29784. })
  29785. editorui.buttons[cmd] = ui
  29786. editor.addListener('selectionchange', function(
  29787. type,
  29788. causeByUi,
  29789. uiReady
  29790. ) {
  29791. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  29792. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady)
  29793. })
  29794. return ui
  29795. }
  29796. })(ci)
  29797. }
  29798. })(p, typeset[p])
  29799. }
  29800. //字体颜色和背景颜色
  29801. for (var i = 0, ci; (ci = ['backcolor', 'forecolor'][i++]); ) {
  29802. editorui[ci] = (function(cmd) {
  29803. return function(editor) {
  29804. var ui = new editorui.ColorButton({
  29805. className: 'edui-for-' + cmd,
  29806. color: 'default',
  29807. title:
  29808. editor.options.labelMap[cmd] ||
  29809. editor.getLang('labelMap.' + cmd) ||
  29810. '',
  29811. editor: editor,
  29812. onpickcolor: function(t, color) {
  29813. editor.execCommand(cmd, color)
  29814. },
  29815. onpicknocolor: function() {
  29816. editor.execCommand(cmd, 'default')
  29817. this.setColor('transparent')
  29818. this.color = 'default'
  29819. },
  29820. onbuttonclick: function() {
  29821. editor.execCommand(cmd, this.color)
  29822. }
  29823. })
  29824. editorui.buttons[cmd] = ui
  29825. editor.addListener('selectionchange', function() {
  29826. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  29827. })
  29828. return ui
  29829. }
  29830. })(ci)
  29831. }
  29832. var dialogBtns = {
  29833. noOk: ['searchreplace', 'help', 'spechars', 'webapp', 'preview'],
  29834. ok: [
  29835. 'attachment',
  29836. 'anchor',
  29837. 'link',
  29838. 'insertimage',
  29839. 'map',
  29840. 'gmap',
  29841. 'insertframe',
  29842. 'wordimage',
  29843. 'insertvideo',
  29844. 'insertframe',
  29845. 'edittip',
  29846. 'edittable',
  29847. 'edittd',
  29848. 'scrawl',
  29849. 'template',
  29850. 'music',
  29851. 'background',
  29852. 'charts'
  29853. ]
  29854. }
  29855. for (var p in dialogBtns) {
  29856. ;(function(type, vals) {
  29857. for (var i = 0, ci; (ci = vals[i++]); ) {
  29858. //todo opera下存在问题
  29859. if (browser.opera && ci === 'searchreplace') {
  29860. continue
  29861. }
  29862. ;(function(cmd) {
  29863. editorui[cmd] = function(editor, iframeUrl, title) {
  29864. iframeUrl =
  29865. iframeUrl ||
  29866. (editor.options.iframeUrlMap || {})[cmd] ||
  29867. iframeUrlMap[cmd]
  29868. title =
  29869. editor.options.labelMap[cmd] ||
  29870. editor.getLang('labelMap.' + cmd) ||
  29871. ''
  29872. var dialog
  29873. //没有iframeUrl不创建dialog
  29874. if (iframeUrl) {
  29875. dialog = new editorui.Dialog(
  29876. utils.extend(
  29877. {
  29878. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29879. editor: editor,
  29880. className: 'edui-for-' + cmd,
  29881. title: title,
  29882. holdScroll: cmd === 'insertimage',
  29883. fullscreen: /charts|preview/.test(cmd),
  29884. closeDialog: editor.getLang('closeDialog')
  29885. },
  29886. type == 'ok'
  29887. ? {
  29888. buttons: [
  29889. {
  29890. className: 'edui-okbutton',
  29891. label: editor.getLang('ok'),
  29892. editor: editor,
  29893. onclick: function() {
  29894. dialog.close(true)
  29895. }
  29896. },
  29897. {
  29898. className: 'edui-cancelbutton',
  29899. label: editor.getLang('cancel'),
  29900. editor: editor,
  29901. onclick: function() {
  29902. dialog.close(false)
  29903. }
  29904. }
  29905. ]
  29906. }
  29907. : {}
  29908. )
  29909. )
  29910. editor.ui._dialogs[cmd + 'Dialog'] = dialog
  29911. }
  29912. var ui = new editorui.Button({
  29913. className: 'edui-for-' + cmd,
  29914. title: title,
  29915. onclick: function() {
  29916. if (dialog) {
  29917. switch (cmd) {
  29918. case 'wordimage':
  29919. var images = editor.execCommand('wordimage')
  29920. if (images && images.length) {
  29921. dialog.render()
  29922. dialog.open()
  29923. }
  29924. break
  29925. case 'scrawl':
  29926. if (editor.queryCommandState('scrawl') != -1) {
  29927. dialog.render()
  29928. dialog.open()
  29929. }
  29930. break
  29931. default:
  29932. dialog.render()
  29933. dialog.open()
  29934. }
  29935. }
  29936. },
  29937. theme: editor.options.theme,
  29938. disabled:
  29939. (cmd == 'scrawl' &&
  29940. editor.queryCommandState('scrawl') == -1) ||
  29941. cmd == 'charts'
  29942. })
  29943. editorui.buttons[cmd] = ui
  29944. editor.addListener('selectionchange', function() {
  29945. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  29946. var unNeedCheckState = {edittable: 1}
  29947. if (cmd in unNeedCheckState) return
  29948. var state = editor.queryCommandState(cmd)
  29949. if (ui.getDom()) {
  29950. ui.setDisabled(state == -1)
  29951. ui.setChecked(state)
  29952. }
  29953. })
  29954. return ui
  29955. }
  29956. })(ci.toLowerCase())
  29957. }
  29958. })(p, dialogBtns[p])
  29959. }
  29960. editorui.snapscreen = function(editor, iframeUrl, title) {
  29961. title =
  29962. editor.options.labelMap['snapscreen'] ||
  29963. editor.getLang('labelMap.snapscreen') ||
  29964. ''
  29965. var ui = new editorui.Button({
  29966. className: 'edui-for-snapscreen',
  29967. title: title,
  29968. onclick: function() {
  29969. editor.execCommand('snapscreen')
  29970. },
  29971. theme: editor.options.theme
  29972. })
  29973. editorui.buttons['snapscreen'] = ui
  29974. iframeUrl =
  29975. iframeUrl ||
  29976. (editor.options.iframeUrlMap || {})['snapscreen'] ||
  29977. iframeUrlMap['snapscreen']
  29978. if (iframeUrl) {
  29979. var dialog = new editorui.Dialog({
  29980. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29981. editor: editor,
  29982. className: 'edui-for-snapscreen',
  29983. title: title,
  29984. buttons: [
  29985. {
  29986. className: 'edui-okbutton',
  29987. label: editor.getLang('ok'),
  29988. editor: editor,
  29989. onclick: function() {
  29990. dialog.close(true)
  29991. }
  29992. },
  29993. {
  29994. className: 'edui-cancelbutton',
  29995. label: editor.getLang('cancel'),
  29996. editor: editor,
  29997. onclick: function() {
  29998. dialog.close(false)
  29999. }
  30000. }
  30001. ]
  30002. })
  30003. dialog.render()
  30004. editor.ui._dialogs['snapscreenDialog'] = dialog
  30005. }
  30006. editor.addListener('selectionchange', function() {
  30007. ui.setDisabled(editor.queryCommandState('snapscreen') == -1)
  30008. })
  30009. return ui
  30010. }
  30011. editorui.insertcode = function(editor, list, title) {
  30012. list = editor.options['insertcode'] || []
  30013. title =
  30014. editor.options.labelMap['insertcode'] ||
  30015. editor.getLang('labelMap.insertcode') ||
  30016. ''
  30017. // if (!list.length) return;
  30018. var items = []
  30019. utils.each(list, function(key, val) {
  30020. items.push({
  30021. label: key,
  30022. value: val,
  30023. theme: editor.options.theme,
  30024. renderLabelHtml: function() {
  30025. return (
  30026. '<div class="edui-label %%-label" >' +
  30027. (this.label || '') +
  30028. '</div>'
  30029. )
  30030. }
  30031. })
  30032. })
  30033. var ui = new editorui.Combox({
  30034. editor: editor,
  30035. items: items,
  30036. onselect: function(t, index) {
  30037. editor.execCommand('insertcode', this.items[index].value)
  30038. },
  30039. onbuttonclick: function() {
  30040. this.showPopup()
  30041. },
  30042. title: title,
  30043. initValue: title,
  30044. className: 'edui-for-insertcode',
  30045. indexByValue: function(value) {
  30046. if (value) {
  30047. for (var i = 0, ci; (ci = this.items[i]); i++) {
  30048. if (ci.value.indexOf(value) != -1) return i
  30049. }
  30050. }
  30051. return -1
  30052. }
  30053. })
  30054. editorui.buttons['insertcode'] = ui
  30055. editor.addListener('selectionchange', function(type, causeByUi, uiReady) {
  30056. if (!uiReady) {
  30057. var state = editor.queryCommandState('insertcode')
  30058. if (state == -1) {
  30059. ui.setDisabled(true)
  30060. } else {
  30061. ui.setDisabled(false)
  30062. var value = editor.queryCommandValue('insertcode')
  30063. if (!value) {
  30064. ui.setValue(title)
  30065. return
  30066. }
  30067. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  30068. value && (value = value.replace(/['"]/g, '').split(',')[0])
  30069. ui.setValue(value)
  30070. }
  30071. }
  30072. })
  30073. return ui
  30074. }
  30075. editorui.fontfamily = function(editor, list, title) {
  30076. list = editor.options['fontfamily'] || []
  30077. title =
  30078. editor.options.labelMap['fontfamily'] ||
  30079. editor.getLang('labelMap.fontfamily') ||
  30080. ''
  30081. if (!list.length) return
  30082. for (var i = 0, ci, items = []; (ci = list[i]); i++) {
  30083. var langLabel = editor.getLang('fontfamily')[ci.name] || ''
  30084. ;(function(key, val) {
  30085. items.push({
  30086. label: key,
  30087. value: val,
  30088. theme: editor.options.theme,
  30089. renderLabelHtml: function() {
  30090. return (
  30091. '<div class="edui-label %%-label" style="font-family:' +
  30092. utils.unhtml(this.value) +
  30093. '">' +
  30094. (this.label || '') +
  30095. '</div>'
  30096. )
  30097. }
  30098. })
  30099. })(ci.label || langLabel, ci.val)
  30100. }
  30101. var ui = new editorui.Combox({
  30102. editor: editor,
  30103. items: items,
  30104. onselect: function(t, index) {
  30105. editor.execCommand('FontFamily', this.items[index].value)
  30106. },
  30107. onbuttonclick: function() {
  30108. this.showPopup()
  30109. },
  30110. title: title,
  30111. initValue: title,
  30112. className: 'edui-for-fontfamily',
  30113. indexByValue: function(value) {
  30114. if (value) {
  30115. for (var i = 0, ci; (ci = this.items[i]); i++) {
  30116. if (ci.value.indexOf(value) != -1) return i
  30117. }
  30118. }
  30119. return -1
  30120. }
  30121. })
  30122. editorui.buttons['fontfamily'] = ui
  30123. editor.addListener('selectionchange', function(type, causeByUi, uiReady) {
  30124. if (!uiReady) {
  30125. var state = editor.queryCommandState('FontFamily')
  30126. if (state == -1) {
  30127. ui.setDisabled(true)
  30128. } else {
  30129. ui.setDisabled(false)
  30130. var value = editor.queryCommandValue('FontFamily')
  30131. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  30132. value && (value = value.replace(/['"]/g, '').split(',')[0])
  30133. ui.setValue(value)
  30134. }
  30135. }
  30136. })
  30137. return ui
  30138. }
  30139. editorui.fontsize = function(editor, list, title) {
  30140. title =
  30141. editor.options.labelMap['fontsize'] ||
  30142. editor.getLang('labelMap.fontsize') ||
  30143. ''
  30144. list = list || editor.options['fontsize'] || []
  30145. if (!list.length) return
  30146. var items = []
  30147. for (var i = 0; i < list.length; i++) {
  30148. var size = list[i] + 'px'
  30149. items.push({
  30150. label: size,
  30151. value: size,
  30152. theme: editor.options.theme,
  30153. renderLabelHtml: function() {
  30154. return (
  30155. '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  30156. this.value +
  30157. '">' +
  30158. (this.label || '') +
  30159. '</div>'
  30160. )
  30161. }
  30162. })
  30163. }
  30164. var ui = new editorui.Combox({
  30165. editor: editor,
  30166. items: items,
  30167. title: title,
  30168. initValue: title,
  30169. onselect: function(t, index) {
  30170. editor.execCommand('FontSize', this.items[index].value)
  30171. },
  30172. onbuttonclick: function() {
  30173. this.showPopup()
  30174. },
  30175. className: 'edui-for-fontsize'
  30176. })
  30177. editorui.buttons['fontsize'] = ui
  30178. editor.addListener('selectionchange', function(type, causeByUi, uiReady) {
  30179. if (!uiReady) {
  30180. var state = editor.queryCommandState('FontSize')
  30181. if (state == -1) {
  30182. ui.setDisabled(true)
  30183. } else {
  30184. ui.setDisabled(false)
  30185. ui.setValue(editor.queryCommandValue('FontSize'))
  30186. }
  30187. }
  30188. })
  30189. return ui
  30190. }
  30191. editorui.paragraph = function(editor, list, title) {
  30192. title =
  30193. editor.options.labelMap['paragraph'] ||
  30194. editor.getLang('labelMap.paragraph') ||
  30195. ''
  30196. list = editor.options['paragraph'] || []
  30197. if (utils.isEmptyObject(list)) return
  30198. var items = []
  30199. for (var i in list) {
  30200. items.push({
  30201. value: i,
  30202. label: list[i] || editor.getLang('paragraph')[i],
  30203. theme: editor.options.theme,
  30204. renderLabelHtml: function() {
  30205. return (
  30206. '<div class="edui-label %%-label"><span class="edui-for-' +
  30207. this.value +
  30208. '">' +
  30209. (this.label || '') +
  30210. '</span></div>'
  30211. )
  30212. }
  30213. })
  30214. }
  30215. var ui = new editorui.Combox({
  30216. editor: editor,
  30217. items: items,
  30218. title: title,
  30219. initValue: title,
  30220. className: 'edui-for-paragraph',
  30221. onselect: function(t, index) {
  30222. editor.execCommand('Paragraph', this.items[index].value)
  30223. },
  30224. onbuttonclick: function() {
  30225. this.showPopup()
  30226. }
  30227. })
  30228. editorui.buttons['paragraph'] = ui
  30229. editor.addListener('selectionchange', function(type, causeByUi, uiReady) {
  30230. if (!uiReady) {
  30231. var state = editor.queryCommandState('Paragraph')
  30232. if (state == -1) {
  30233. ui.setDisabled(true)
  30234. } else {
  30235. ui.setDisabled(false)
  30236. var value = editor.queryCommandValue('Paragraph')
  30237. var index = ui.indexByValue(value)
  30238. if (index != -1) {
  30239. ui.setValue(value)
  30240. } else {
  30241. ui.setValue(ui.initValue)
  30242. }
  30243. }
  30244. }
  30245. })
  30246. return ui
  30247. }
  30248. //自定义标题
  30249. editorui.customstyle = function(editor) {
  30250. var list = editor.options['customstyle'] || [],
  30251. title =
  30252. editor.options.labelMap['customstyle'] ||
  30253. editor.getLang('labelMap.customstyle') ||
  30254. ''
  30255. if (!list.length) return
  30256. var langCs = editor.getLang('customstyle')
  30257. for (var i = 0, items = [], t; (t = list[i++]); ) {
  30258. ;(function(t) {
  30259. var ck = {}
  30260. ck.label = t.label ? t.label : langCs[t.name]
  30261. ck.style = t.style
  30262. ck.className = t.className
  30263. ck.tag = t.tag
  30264. items.push({
  30265. label: ck.label,
  30266. value: ck,
  30267. theme: editor.options.theme,
  30268. renderLabelHtml: function() {
  30269. return (
  30270. '<div class="edui-label %%-label">' +
  30271. '<' +
  30272. ck.tag +
  30273. ' ' +
  30274. (ck.className ? ' class="' + ck.className + '"' : '') +
  30275. (ck.style ? ' style="' + ck.style + '"' : '') +
  30276. '>' +
  30277. ck.label +
  30278. '</' +
  30279. ck.tag +
  30280. '>' +
  30281. '</div>'
  30282. )
  30283. }
  30284. })
  30285. })(t)
  30286. }
  30287. var ui = new editorui.Combox({
  30288. editor: editor,
  30289. items: items,
  30290. title: title,
  30291. initValue: title,
  30292. className: 'edui-for-customstyle',
  30293. onselect: function(t, index) {
  30294. editor.execCommand('customstyle', this.items[index].value)
  30295. },
  30296. onbuttonclick: function() {
  30297. this.showPopup()
  30298. },
  30299. indexByValue: function(value) {
  30300. for (var i = 0, ti; (ti = this.items[i++]); ) {
  30301. if (ti.label == value) {
  30302. return i - 1
  30303. }
  30304. }
  30305. return -1
  30306. }
  30307. })
  30308. editorui.buttons['customstyle'] = ui
  30309. editor.addListener('selectionchange', function(type, causeByUi, uiReady) {
  30310. if (!uiReady) {
  30311. var state = editor.queryCommandState('customstyle')
  30312. if (state == -1) {
  30313. ui.setDisabled(true)
  30314. } else {
  30315. ui.setDisabled(false)
  30316. var value = editor.queryCommandValue('customstyle')
  30317. var index = ui.indexByValue(value)
  30318. if (index != -1) {
  30319. ui.setValue(value)
  30320. } else {
  30321. ui.setValue(ui.initValue)
  30322. }
  30323. }
  30324. }
  30325. })
  30326. return ui
  30327. }
  30328. editorui.inserttable = function(editor, iframeUrl, title) {
  30329. title =
  30330. editor.options.labelMap['inserttable'] ||
  30331. editor.getLang('labelMap.inserttable') ||
  30332. ''
  30333. var ui = new editorui.TableButton({
  30334. editor: editor,
  30335. title: title,
  30336. className: 'edui-for-inserttable',
  30337. onpicktable: function(t, numCols, numRows) {
  30338. editor.execCommand('InsertTable', {
  30339. numRows: numRows,
  30340. numCols: numCols,
  30341. border: 1
  30342. })
  30343. },
  30344. onbuttonclick: function() {
  30345. this.showPopup()
  30346. }
  30347. })
  30348. editorui.buttons['inserttable'] = ui
  30349. editor.addListener('selectionchange', function() {
  30350. ui.setDisabled(editor.queryCommandState('inserttable') == -1)
  30351. })
  30352. return ui
  30353. }
  30354. editorui.lineheight = function(editor) {
  30355. var val = editor.options.lineheight || []
  30356. if (!val.length) return
  30357. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30358. items.push({
  30359. //todo:写死了
  30360. label: ci,
  30361. value: ci,
  30362. theme: editor.options.theme,
  30363. onclick: function() {
  30364. editor.execCommand('lineheight', this.value)
  30365. }
  30366. })
  30367. }
  30368. var ui = new editorui.MenuButton({
  30369. editor: editor,
  30370. className: 'edui-for-lineheight',
  30371. title:
  30372. editor.options.labelMap['lineheight'] ||
  30373. editor.getLang('labelMap.lineheight') ||
  30374. '',
  30375. items: items,
  30376. onbuttonclick: function() {
  30377. var value = editor.queryCommandValue('LineHeight') || this.value
  30378. editor.execCommand('LineHeight', value)
  30379. }
  30380. })
  30381. editorui.buttons['lineheight'] = ui
  30382. editor.addListener('selectionchange', function() {
  30383. var state = editor.queryCommandState('LineHeight')
  30384. if (state == -1) {
  30385. ui.setDisabled(true)
  30386. } else {
  30387. ui.setDisabled(false)
  30388. var value = editor.queryCommandValue('LineHeight')
  30389. value && ui.setValue((value + '').replace(/cm/, ''))
  30390. ui.setChecked(state)
  30391. }
  30392. })
  30393. return ui
  30394. }
  30395. var rowspacings = ['top', 'bottom']
  30396. for (var r = 0, ri; (ri = rowspacings[r++]); ) {
  30397. ;(function(cmd) {
  30398. editorui['rowspacing' + cmd] = function(editor) {
  30399. var val = editor.options['rowspacing' + cmd] || []
  30400. if (!val.length) return null
  30401. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30402. items.push({
  30403. label: ci,
  30404. value: ci,
  30405. theme: editor.options.theme,
  30406. onclick: function() {
  30407. editor.execCommand('rowspacing', this.value, cmd)
  30408. }
  30409. })
  30410. }
  30411. var ui = new editorui.MenuButton({
  30412. editor: editor,
  30413. className: 'edui-for-rowspacing' + cmd,
  30414. title:
  30415. editor.options.labelMap['rowspacing' + cmd] ||
  30416. editor.getLang('labelMap.rowspacing' + cmd) ||
  30417. '',
  30418. items: items,
  30419. onbuttonclick: function() {
  30420. var value =
  30421. editor.queryCommandValue('rowspacing', cmd) || this.value
  30422. editor.execCommand('rowspacing', value, cmd)
  30423. }
  30424. })
  30425. editorui.buttons[cmd] = ui
  30426. editor.addListener('selectionchange', function() {
  30427. var state = editor.queryCommandState('rowspacing', cmd)
  30428. if (state == -1) {
  30429. ui.setDisabled(true)
  30430. } else {
  30431. ui.setDisabled(false)
  30432. var value = editor.queryCommandValue('rowspacing', cmd)
  30433. value && ui.setValue((value + '').replace(/%/, ''))
  30434. ui.setChecked(state)
  30435. }
  30436. })
  30437. return ui
  30438. }
  30439. })(ri)
  30440. }
  30441. //有序,无序列表
  30442. var lists = ['insertorderedlist', 'insertunorderedlist']
  30443. for (var l = 0, cl; (cl = lists[l++]); ) {
  30444. ;(function(cmd) {
  30445. editorui[cmd] = function(editor) {
  30446. var vals = editor.options[cmd],
  30447. _onMenuClick = function() {
  30448. editor.execCommand(cmd, this.value)
  30449. },
  30450. items = []
  30451. for (var i in vals) {
  30452. items.push({
  30453. label: vals[i] || editor.getLang()[cmd][i] || '',
  30454. value: i,
  30455. theme: editor.options.theme,
  30456. onclick: _onMenuClick
  30457. })
  30458. }
  30459. var ui = new editorui.MenuButton({
  30460. editor: editor,
  30461. className: 'edui-for-' + cmd,
  30462. title: editor.getLang('labelMap.' + cmd) || '',
  30463. items: items,
  30464. onbuttonclick: function() {
  30465. var value = editor.queryCommandValue(cmd) || this.value
  30466. editor.execCommand(cmd, value)
  30467. }
  30468. })
  30469. editorui.buttons[cmd] = ui
  30470. editor.addListener('selectionchange', function() {
  30471. var state = editor.queryCommandState(cmd)
  30472. if (state == -1) {
  30473. ui.setDisabled(true)
  30474. } else {
  30475. ui.setDisabled(false)
  30476. var value = editor.queryCommandValue(cmd)
  30477. ui.setValue(value)
  30478. ui.setChecked(state)
  30479. }
  30480. })
  30481. return ui
  30482. }
  30483. })(cl)
  30484. }
  30485. editorui.fullscreen = function(editor, title) {
  30486. title =
  30487. editor.options.labelMap['fullscreen'] ||
  30488. editor.getLang('labelMap.fullscreen') ||
  30489. ''
  30490. var ui = new editorui.Button({
  30491. className: 'edui-for-fullscreen',
  30492. title: title,
  30493. theme: editor.options.theme,
  30494. onclick: function() {
  30495. if (editor.ui) {
  30496. editor.ui.setFullScreen(!editor.ui.isFullScreen())
  30497. }
  30498. this.setChecked(editor.ui.isFullScreen())
  30499. }
  30500. })
  30501. editorui.buttons['fullscreen'] = ui
  30502. editor.addListener('selectionchange', function() {
  30503. var state = editor.queryCommandState('fullscreen')
  30504. ui.setDisabled(state == -1)
  30505. ui.setChecked(editor.ui.isFullScreen())
  30506. })
  30507. return ui
  30508. }
  30509. // 表情
  30510. editorui['emotion'] = function(editor, iframeUrl) {
  30511. var cmd = 'emotion'
  30512. var ui = new editorui.MultiMenuPop({
  30513. title:
  30514. editor.options.labelMap[cmd] ||
  30515. editor.getLang('labelMap.' + cmd + '') ||
  30516. '',
  30517. editor: editor,
  30518. className: 'edui-for-' + cmd,
  30519. iframeUrl: editor.ui.mapUrl(
  30520. iframeUrl ||
  30521. (editor.options.iframeUrlMap || {})[cmd] ||
  30522. iframeUrlMap[cmd]
  30523. )
  30524. })
  30525. editorui.buttons[cmd] = ui
  30526. editor.addListener('selectionchange', function() {
  30527. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  30528. })
  30529. return ui
  30530. }
  30531. editorui.autotypeset = function(editor) {
  30532. var ui = new editorui.AutoTypeSetButton({
  30533. editor: editor,
  30534. title:
  30535. editor.options.labelMap['autotypeset'] ||
  30536. editor.getLang('labelMap.autotypeset') ||
  30537. '',
  30538. className: 'edui-for-autotypeset',
  30539. onbuttonclick: function() {
  30540. editor.execCommand('autotypeset')
  30541. }
  30542. })
  30543. editorui.buttons['autotypeset'] = ui
  30544. editor.addListener('selectionchange', function() {
  30545. ui.setDisabled(editor.queryCommandState('autotypeset') == -1)
  30546. })
  30547. return ui
  30548. }
  30549. /* 简单上传插件 */
  30550. editorui['simpleupload'] = function(editor) {
  30551. var name = 'simpleupload',
  30552. ui = new editorui.Button({
  30553. className: 'edui-for-' + name,
  30554. title:
  30555. editor.options.labelMap[name] ||
  30556. editor.getLang('labelMap.' + name) ||
  30557. '',
  30558. onclick: function() {},
  30559. theme: editor.options.theme,
  30560. showText: false
  30561. })
  30562. editorui.buttons[name] = ui
  30563. editor.addListener('ready', function() {
  30564. var b = ui.getDom('body'),
  30565. iconSpan = b.children[0]
  30566. editor.fireEvent('simpleuploadbtnready', iconSpan)
  30567. })
  30568. editor.addListener('selectionchange', function(type, causeByUi, uiReady) {
  30569. var state = editor.queryCommandState(name)
  30570. if (state == -1) {
  30571. ui.setDisabled(true)
  30572. ui.setChecked(false)
  30573. } else {
  30574. if (!uiReady) {
  30575. ui.setDisabled(false)
  30576. ui.setChecked(state)
  30577. }
  30578. }
  30579. })
  30580. return ui
  30581. }
  30582. })()
  30583. // adapter/editor.js
  30584. ///import core
  30585. ///commands 全屏
  30586. ///commandsName FullScreen
  30587. ///commandsTitle 全屏
  30588. ;(function() {
  30589. var utils = baidu.editor.utils,
  30590. uiUtils = baidu.editor.ui.uiUtils,
  30591. UIBase = baidu.editor.ui.UIBase,
  30592. domUtils = baidu.editor.dom.domUtils
  30593. var nodeStack = []
  30594. function EditorUI(options) {
  30595. this.initOptions(options)
  30596. this.initEditorUI()
  30597. }
  30598. EditorUI.prototype = {
  30599. uiName: 'editor',
  30600. initEditorUI: function() {
  30601. this.editor.ui = this
  30602. this._dialogs = {}
  30603. this.initUIBase()
  30604. this._initToolbars()
  30605. var editor = this.editor,
  30606. me = this
  30607. editor.addListener('ready', function() {
  30608. //提供getDialog方法
  30609. editor.getDialog = function(name) {
  30610. return editor.ui._dialogs[name + 'Dialog']
  30611. }
  30612. domUtils.on(editor.window, 'scroll', function(evt) {
  30613. baidu.editor.ui.Popup.postHide(evt)
  30614. })
  30615. //提供编辑器实时宽高(全屏时宽高不变化)
  30616. editor.ui._actualFrameWidth = editor.options.initialFrameWidth
  30617. UE.browser.ie &&
  30618. UE.browser.version === 6 &&
  30619. editor.container.ownerDocument.execCommand(
  30620. 'BackgroundImageCache',
  30621. false,
  30622. true
  30623. )
  30624. //display bottom-bar label based on config
  30625. if (editor.options.elementPathEnabled) {
  30626. editor.ui.getDom('elementpath').innerHTML =
  30627. '<div class="edui-editor-breadcrumb">' +
  30628. editor.getLang('elementPathTip') +
  30629. ':</div>'
  30630. }
  30631. if (editor.options.wordCount) {
  30632. function countFn() {
  30633. setCount(editor, me)
  30634. domUtils.un(editor.document, 'click', arguments.callee)
  30635. }
  30636. domUtils.on(editor.document, 'click', countFn)
  30637. editor.ui.getDom('wordcount').innerHTML = editor.getLang(
  30638. 'wordCountTip'
  30639. )
  30640. }
  30641. editor.ui._scale()
  30642. if (editor.options.scaleEnabled) {
  30643. if (editor.autoHeightEnabled) {
  30644. editor.disableAutoHeight()
  30645. }
  30646. me.enableScale()
  30647. } else {
  30648. me.disableScale()
  30649. }
  30650. if (
  30651. !editor.options.elementPathEnabled &&
  30652. !editor.options.wordCount &&
  30653. !editor.options.scaleEnabled
  30654. ) {
  30655. editor.ui.getDom('elementpath').style.display = 'none'
  30656. editor.ui.getDom('wordcount').style.display = 'none'
  30657. editor.ui.getDom('scale').style.display = 'none'
  30658. }
  30659. if (!editor.selection.isFocus()) return
  30660. editor.fireEvent('selectionchange', false, true)
  30661. })
  30662. editor.addListener('mousedown', function(t, evt) {
  30663. var el = evt.target || evt.srcElement
  30664. baidu.editor.ui.Popup.postHide(evt, el)
  30665. baidu.editor.ui.ShortCutMenu.postHide(evt)
  30666. })
  30667. editor.addListener('delcells', function() {
  30668. if (UE.ui['edittip']) {
  30669. new UE.ui['edittip'](editor)
  30670. }
  30671. editor.getDialog('edittip').open()
  30672. })
  30673. var pastePop,
  30674. isPaste = false,
  30675. timer
  30676. editor.addListener('afterpaste', function() {
  30677. if (editor.queryCommandState('pasteplain')) return
  30678. if (baidu.editor.ui.PastePicker) {
  30679. pastePop = new baidu.editor.ui.Popup({
  30680. content: new baidu.editor.ui.PastePicker({editor: editor}),
  30681. editor: editor,
  30682. className: 'edui-wordpastepop'
  30683. })
  30684. pastePop.render()
  30685. }
  30686. isPaste = true
  30687. })
  30688. editor.addListener('afterinserthtml', function() {
  30689. clearTimeout(timer)
  30690. timer = setTimeout(function() {
  30691. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  30692. if (pastePop.isHidden()) {
  30693. var span = domUtils.createElement(editor.document, 'span', {
  30694. style: 'line-height:0px;',
  30695. innerHTML: '\ufeff'
  30696. }),
  30697. range = editor.selection.getRange()
  30698. range.insertNode(span)
  30699. var tmp = getDomNode(span, 'firstChild', 'previousSibling')
  30700. tmp &&
  30701. pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp)
  30702. domUtils.remove(span)
  30703. } else {
  30704. pastePop.show()
  30705. }
  30706. delete editor.ui._isTransfer
  30707. isPaste = false
  30708. }
  30709. }, 200)
  30710. })
  30711. editor.addListener('contextmenu', function(t, evt) {
  30712. baidu.editor.ui.Popup.postHide(evt)
  30713. })
  30714. editor.addListener('keydown', function(t, evt) {
  30715. if (pastePop) pastePop.dispose(evt)
  30716. var keyCode = evt.keyCode || evt.which
  30717. if (evt.altKey && keyCode == 90) {
  30718. UE.ui.buttons['fullscreen'].onclick()
  30719. }
  30720. })
  30721. editor.addListener('wordcount', function(type) {
  30722. setCount(this, me)
  30723. })
  30724. function setCount(editor, ui) {
  30725. editor.setOpt({
  30726. wordCount: true,
  30727. maximumWords: 10000,
  30728. wordCountMsg:
  30729. editor.options.wordCountMsg || editor.getLang('wordCountMsg'),
  30730. wordOverFlowMsg:
  30731. editor.options.wordOverFlowMsg ||
  30732. editor.getLang('wordOverFlowMsg')
  30733. })
  30734. var opt = editor.options,
  30735. max = opt.maximumWords,
  30736. msg = opt.wordCountMsg,
  30737. errMsg = opt.wordOverFlowMsg,
  30738. countDom = ui.getDom('wordcount')
  30739. if (!opt.wordCount) {
  30740. return
  30741. }
  30742. var count = editor.getContentLength(true)
  30743. if (count > max) {
  30744. countDom.innerHTML = errMsg
  30745. editor.fireEvent('wordcountoverflow')
  30746. } else {
  30747. countDom.innerHTML = msg
  30748. .replace('{#leave}', max - count)
  30749. .replace('{#count}', count)
  30750. }
  30751. }
  30752. editor.addListener('selectionchange', function() {
  30753. if (editor.options.elementPathEnabled) {
  30754. me[
  30755. (editor.queryCommandState('elementpath') == -1 ? 'dis' : 'en') +
  30756. 'ableElementPath'
  30757. ]()
  30758. }
  30759. if (editor.options.scaleEnabled) {
  30760. me[
  30761. (editor.queryCommandState('scale') == -1 ? 'dis' : 'en') +
  30762. 'ableScale'
  30763. ]()
  30764. }
  30765. })
  30766. var popup = new baidu.editor.ui.Popup({
  30767. editor: editor,
  30768. content: '',
  30769. className: 'edui-bubble',
  30770. _onEditButtonClick: function() {
  30771. this.hide()
  30772. editor.ui._dialogs.linkDialog.open()
  30773. },
  30774. _onImgEditButtonClick: function(name) {
  30775. this.hide()
  30776. editor.ui._dialogs[name] && editor.ui._dialogs[name].open()
  30777. },
  30778. _onImgSetFloat: function(value) {
  30779. this.hide()
  30780. editor.execCommand('imagefloat', value)
  30781. },
  30782. _setIframeAlign: function(value) {
  30783. var frame = popup.anchorEl
  30784. var newFrame = frame.cloneNode(true)
  30785. switch (value) {
  30786. case -2:
  30787. newFrame.setAttribute('align', '')
  30788. break
  30789. case -1:
  30790. newFrame.setAttribute('align', 'left')
  30791. break
  30792. case 1:
  30793. newFrame.setAttribute('align', 'right')
  30794. break
  30795. }
  30796. frame.parentNode.insertBefore(newFrame, frame)
  30797. domUtils.remove(frame)
  30798. popup.anchorEl = newFrame
  30799. popup.showAnchor(popup.anchorEl)
  30800. },
  30801. _updateIframe: function() {
  30802. var frame = (editor._iframe = popup.anchorEl)
  30803. if (domUtils.hasClass(frame, 'ueditor_baidumap')) {
  30804. editor.selection
  30805. .getRange()
  30806. .selectNode(frame)
  30807. .select()
  30808. editor.ui._dialogs.mapDialog.open()
  30809. popup.hide()
  30810. } else {
  30811. editor.ui._dialogs.insertframeDialog.open()
  30812. popup.hide()
  30813. }
  30814. },
  30815. _onRemoveButtonClick: function(cmdName) {
  30816. editor.execCommand(cmdName)
  30817. this.hide()
  30818. },
  30819. queryAutoHide: function(el) {
  30820. if (el && el.ownerDocument == editor.document) {
  30821. if (
  30822. el.tagName.toLowerCase() == 'img' ||
  30823. domUtils.findParentByTagName(el, 'a', true)
  30824. ) {
  30825. return el !== popup.anchorEl
  30826. }
  30827. }
  30828. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el)
  30829. }
  30830. })
  30831. popup.render()
  30832. if (editor.options.imagePopup) {
  30833. editor.addListener('mouseover', function(t, evt) {
  30834. evt = evt || window.event
  30835. var el = evt.target || evt.srcElement
  30836. if (
  30837. editor.ui._dialogs.insertframeDialog &&
  30838. /iframe/gi.test(el.tagName)
  30839. ) {
  30840. var html = popup.formatHtml(
  30841. '<nobr>' +
  30842. editor.getLang('property') +
  30843. ': <span onclick=$$._setIframeAlign(-2) class="edui-clickable">' +
  30844. editor.getLang('default') +
  30845. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' +
  30846. editor.getLang('justifyleft') +
  30847. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' +
  30848. editor.getLang('justifyright') +
  30849. '</span>&nbsp;&nbsp;' +
  30850. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' +
  30851. editor.getLang('modify') +
  30852. '</span></nobr>'
  30853. )
  30854. if (html) {
  30855. popup.getDom('content').innerHTML = html
  30856. popup.anchorEl = el
  30857. popup.showAnchor(popup.anchorEl)
  30858. } else {
  30859. popup.hide()
  30860. }
  30861. }
  30862. })
  30863. editor.addListener('selectionchange', function(t, causeByUi) {
  30864. if (!causeByUi) return
  30865. var html = '',
  30866. str = '',
  30867. img = editor.selection.getRange().getClosedNode(),
  30868. dialogs = editor.ui._dialogs
  30869. if (img && img.tagName == 'IMG') {
  30870. var dialogName = 'insertimageDialog'
  30871. if (
  30872. img.className.indexOf('edui-faked-video') != -1 ||
  30873. img.className.indexOf('edui-upload-video') != -1
  30874. ) {
  30875. dialogName = 'insertvideoDialog'
  30876. }
  30877. if (img.className.indexOf('edui-faked-webapp') != -1) {
  30878. dialogName = 'webappDialog'
  30879. }
  30880. if (img.src.indexOf('http://api.map.baidu.com') != -1) {
  30881. dialogName = 'mapDialog'
  30882. }
  30883. if (img.className.indexOf('edui-faked-music') != -1) {
  30884. dialogName = 'musicDialog'
  30885. }
  30886. if (
  30887. img.src.indexOf('http://maps.google.com/maps/api/staticmap') !=
  30888. -1
  30889. ) {
  30890. dialogName = 'gmapDialog'
  30891. }
  30892. if (img.getAttribute('anchorname')) {
  30893. dialogName = 'anchorDialog'
  30894. html = popup.formatHtml(
  30895. '<nobr>' +
  30896. editor.getLang('property') +
  30897. ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' +
  30898. editor.getLang('modify') +
  30899. '</span>&nbsp;&nbsp;' +
  30900. '<span onclick=$$._onRemoveButtonClick(\'anchor\') class="edui-clickable">' +
  30901. editor.getLang('delete') +
  30902. '</span></nobr>'
  30903. )
  30904. }
  30905. if (img.getAttribute('word_img')) {
  30906. //todo 放到dialog去做查询
  30907. editor.word_img = [img.getAttribute('word_img')]
  30908. dialogName = 'wordimageDialog'
  30909. }
  30910. if (
  30911. domUtils.hasClass(img, 'loadingclass') ||
  30912. domUtils.hasClass(img, 'loaderrorclass')
  30913. ) {
  30914. dialogName = ''
  30915. }
  30916. if (!dialogs[dialogName]) {
  30917. return
  30918. }
  30919. str =
  30920. '<nobr>' +
  30921. editor.getLang('property') +
  30922. ': ' +
  30923. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' +
  30924. editor.getLang('default') +
  30925. '</span>&nbsp;&nbsp;' +
  30926. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' +
  30927. editor.getLang('justifyleft') +
  30928. '</span>&nbsp;&nbsp;' +
  30929. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' +
  30930. editor.getLang('justifyright') +
  30931. '</span>&nbsp;&nbsp;' +
  30932. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' +
  30933. editor.getLang('justifycenter') +
  30934. '</span>&nbsp;&nbsp;' +
  30935. '<span onclick="$$._onImgEditButtonClick(\'' +
  30936. dialogName +
  30937. '\');" class="edui-clickable">' +
  30938. editor.getLang('modify') +
  30939. '</span></nobr>'
  30940. !html && (html = popup.formatHtml(str))
  30941. }
  30942. if (editor.ui._dialogs.linkDialog) {
  30943. var link = editor.queryCommandValue('link')
  30944. var url
  30945. if (
  30946. link &&
  30947. (url =
  30948. link.getAttribute('_href') || link.getAttribute('href', 2))
  30949. ) {
  30950. var txt = url
  30951. if (url.length > 30) {
  30952. txt = url.substring(0, 20) + '...'
  30953. }
  30954. if (html) {
  30955. html += '<div style="height:5px;"></div>'
  30956. }
  30957. html += popup.formatHtml(
  30958. '<nobr>' +
  30959. editor.getLang('anthorMsg') +
  30960. ': <a target="_blank" href="' +
  30961. url +
  30962. '" title="' +
  30963. url +
  30964. '" >' +
  30965. txt +
  30966. '</a>' +
  30967. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' +
  30968. editor.getLang('modify') +
  30969. '</span>' +
  30970. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' +
  30971. editor.getLang('clear') +
  30972. '</span></nobr>'
  30973. )
  30974. popup.showAnchor(link)
  30975. }
  30976. }
  30977. if (html) {
  30978. popup.getDom('content').innerHTML = html
  30979. popup.anchorEl = img || link
  30980. popup.showAnchor(popup.anchorEl)
  30981. } else {
  30982. popup.hide()
  30983. }
  30984. })
  30985. }
  30986. },
  30987. _initToolbars: function() {
  30988. var editor = this.editor
  30989. var toolbars = this.toolbars || []
  30990. var toolbarUis = []
  30991. for (var i = 0; i < toolbars.length; i++) {
  30992. var toolbar = toolbars[i]
  30993. var toolbarUi = new baidu.editor.ui.Toolbar({
  30994. theme: editor.options.theme
  30995. })
  30996. for (var j = 0; j < toolbar.length; j++) {
  30997. var toolbarItem = toolbar[j]
  30998. var toolbarItemUi = null
  30999. if (typeof toolbarItem == 'string') {
  31000. toolbarItem = toolbarItem.toLowerCase()
  31001. if (toolbarItem == '|') {
  31002. toolbarItem = 'Separator'
  31003. }
  31004. if (toolbarItem == '||') {
  31005. toolbarItem = 'Breakline'
  31006. }
  31007. if (baidu.editor.ui[toolbarItem]) {
  31008. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor)
  31009. }
  31010. //fullscreen这里单独处理一下,放到首行去
  31011. if (toolbarItem == 'fullscreen') {
  31012. if (toolbarUis && toolbarUis[0]) {
  31013. toolbarUis[0].items.splice(0, 0, toolbarItemUi)
  31014. } else {
  31015. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi)
  31016. }
  31017. continue
  31018. }
  31019. } else {
  31020. toolbarItemUi = toolbarItem
  31021. }
  31022. if (toolbarItemUi && toolbarItemUi.id) {
  31023. toolbarUi.add(toolbarItemUi)
  31024. }
  31025. }
  31026. toolbarUis[i] = toolbarUi
  31027. }
  31028. //接受外部定制的UI
  31029. utils.each(UE._customizeUI, function(obj, key) {
  31030. var itemUI, index
  31031. if (obj.id && obj.id != editor.key) {
  31032. return false
  31033. }
  31034. itemUI = obj.execFn.call(editor, editor, key)
  31035. if (itemUI) {
  31036. index = obj.index
  31037. if (index === undefined) {
  31038. index = toolbarUi.items.length
  31039. }
  31040. toolbarUi.add(itemUI, index)
  31041. }
  31042. })
  31043. this.toolbars = toolbarUis
  31044. },
  31045. getHtmlTpl: function() {
  31046. return (
  31047. '<div id="##" class="%%">' +
  31048. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  31049. (this.toolbars.length
  31050. ? '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  31051. this.renderToolbarBoxHtml() +
  31052. '</div></div>'
  31053. : '') +
  31054. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  31055. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' +
  31056. this.editor.getLang('clickToUpload') +
  31057. '</div>' +
  31058. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  31059. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  31060. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  31061. '</div>' +
  31062. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  31063. '</div>' +
  31064. '<div id="##_iframeholder" class="%%-iframeholder">' +
  31065. '</div>' +
  31066. //modify wdcount by matao
  31067. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  31068. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  31069. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  31070. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  31071. '</tr></table></div>' +
  31072. '<div id="##_scalelayer"></div>' +
  31073. '</div>'
  31074. )
  31075. },
  31076. showWordImageDialog: function() {
  31077. this._dialogs['wordimageDialog'].open()
  31078. },
  31079. renderToolbarBoxHtml: function() {
  31080. var buff = []
  31081. for (var i = 0; i < this.toolbars.length; i++) {
  31082. buff.push(this.toolbars[i].renderHtml())
  31083. }
  31084. return buff.join('')
  31085. },
  31086. setFullScreen: function(fullscreen) {
  31087. var editor = this.editor,
  31088. container = editor.container.parentNode.parentNode
  31089. if (this._fullscreen != fullscreen) {
  31090. this._fullscreen = fullscreen
  31091. this.editor.fireEvent('beforefullscreenchange', fullscreen)
  31092. if (baidu.editor.browser.gecko) {
  31093. var bk = editor.selection.getRange().createBookmark()
  31094. }
  31095. if (fullscreen) {
  31096. while (container.tagName != 'BODY') {
  31097. var position = baidu.editor.dom.domUtils.getComputedStyle(
  31098. container,
  31099. 'position'
  31100. )
  31101. nodeStack.push(position)
  31102. container.style.position = 'static'
  31103. container = container.parentNode
  31104. }
  31105. this._bakHtmlOverflow = document.documentElement.style.overflow
  31106. this._bakBodyOverflow = document.body.style.overflow
  31107. this._bakAutoHeight = this.editor.autoHeightEnabled
  31108. this._bakScrollTop = Math.max(
  31109. document.documentElement.scrollTop,
  31110. document.body.scrollTop
  31111. )
  31112. this._bakEditorContaninerWidth =
  31113. editor.iframe.parentNode.offsetWidth
  31114. if (this._bakAutoHeight) {
  31115. //当全屏时不能执行自动长高
  31116. editor.autoHeightEnabled = false
  31117. this.editor.disableAutoHeight()
  31118. }
  31119. document.documentElement.style.overflow = 'hidden'
  31120. //修复,滚动条不收起的问题
  31121. window.scrollTo(0, window.scrollY)
  31122. this._bakCssText = this.getDom().style.cssText
  31123. this._bakCssText1 = this.getDom('iframeholder').style.cssText
  31124. editor.iframe.parentNode.style.width = ''
  31125. this._updateFullScreen()
  31126. } else {
  31127. while (container.tagName != 'BODY') {
  31128. container.style.position = nodeStack.shift()
  31129. container = container.parentNode
  31130. }
  31131. this.getDom().style.cssText = this._bakCssText
  31132. this.getDom('iframeholder').style.cssText = this._bakCssText1
  31133. if (this._bakAutoHeight) {
  31134. editor.autoHeightEnabled = true
  31135. this.editor.enableAutoHeight()
  31136. }
  31137. document.documentElement.style.overflow = this._bakHtmlOverflow
  31138. document.body.style.overflow = this._bakBodyOverflow
  31139. editor.iframe.parentNode.style.width =
  31140. this._bakEditorContaninerWidth + 'px'
  31141. window.scrollTo(0, this._bakScrollTop)
  31142. }
  31143. if (browser.gecko && editor.body.contentEditable === 'true') {
  31144. var input = document.createElement('input')
  31145. document.body.appendChild(input)
  31146. editor.body.contentEditable = false
  31147. setTimeout(function() {
  31148. input.focus()
  31149. setTimeout(function() {
  31150. editor.body.contentEditable = true
  31151. editor.fireEvent('fullscreenchanged', fullscreen)
  31152. editor.selection
  31153. .getRange()
  31154. .moveToBookmark(bk)
  31155. .select(true)
  31156. baidu.editor.dom.domUtils.remove(input)
  31157. fullscreen && window.scroll(0, 0)
  31158. }, 0)
  31159. }, 0)
  31160. }
  31161. if (editor.body.contentEditable === 'true') {
  31162. this.editor.fireEvent('fullscreenchanged', fullscreen)
  31163. this.triggerLayout()
  31164. }
  31165. }
  31166. },
  31167. _updateFullScreen: function() {
  31168. if (this._fullscreen) {
  31169. var vpRect = uiUtils.getViewportRect()
  31170. this.getDom().style.cssText =
  31171. 'border:0;position:absolute;left:0;top:' +
  31172. (this.editor.options.topOffset || 0) +
  31173. 'px;width:' +
  31174. vpRect.width +
  31175. 'px;height:' +
  31176. vpRect.height +
  31177. 'px;z-index:' +
  31178. (this.getDom().style.zIndex * 1 + 100)
  31179. uiUtils.setViewportOffset(this.getDom(), {
  31180. left: 0,
  31181. top: this.editor.options.topOffset || 0
  31182. })
  31183. this.editor.setHeight(
  31184. vpRect.height -
  31185. this.getDom('toolbarbox').offsetHeight -
  31186. this.getDom('bottombar').offsetHeight -
  31187. (this.editor.options.topOffset || 0),
  31188. true
  31189. )
  31190. //不手动调一下,会导致全屏失效
  31191. if (browser.gecko) {
  31192. try {
  31193. window.onresize()
  31194. } catch (e) {}
  31195. }
  31196. }
  31197. },
  31198. _updateElementPath: function() {
  31199. var bottom = this.getDom('elementpath'),
  31200. list
  31201. if (
  31202. this.elementPathEnabled &&
  31203. (list = this.editor.queryCommandValue('elementpath'))
  31204. ) {
  31205. var buff = []
  31206. for (var i = 0, ci; (ci = list[i]); i++) {
  31207. buff[i] = this.formatHtml(
  31208. '<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' +
  31209. i +
  31210. '&quot;);">' +
  31211. ci +
  31212. '</span>'
  31213. )
  31214. }
  31215. bottom.innerHTML =
  31216. '<div class="edui-editor-breadcrumb" onmousedown="return false;">' +
  31217. this.editor.getLang('elementPathTip') +
  31218. ': ' +
  31219. buff.join(' &gt; ') +
  31220. '</div>'
  31221. } else {
  31222. bottom.style.display = 'none'
  31223. }
  31224. },
  31225. disableElementPath: function() {
  31226. var bottom = this.getDom('elementpath')
  31227. bottom.innerHTML = ''
  31228. bottom.style.display = 'none'
  31229. this.elementPathEnabled = false
  31230. },
  31231. enableElementPath: function() {
  31232. var bottom = this.getDom('elementpath')
  31233. bottom.style.display = ''
  31234. this.elementPathEnabled = true
  31235. this._updateElementPath()
  31236. },
  31237. _scale: function() {
  31238. var doc = document,
  31239. editor = this.editor,
  31240. editorHolder = editor.container,
  31241. editorDocument = editor.document,
  31242. toolbarBox = this.getDom('toolbarbox'),
  31243. bottombar = this.getDom('bottombar'),
  31244. scale = this.getDom('scale'),
  31245. scalelayer = this.getDom('scalelayer')
  31246. var isMouseMove = false,
  31247. position = null,
  31248. minEditorHeight = 0,
  31249. minEditorWidth = editor.options.minFrameWidth,
  31250. pageX = 0,
  31251. pageY = 0,
  31252. scaleWidth = 0,
  31253. scaleHeight = 0
  31254. function down() {
  31255. position = domUtils.getXY(editorHolder)
  31256. if (!minEditorHeight) {
  31257. minEditorHeight =
  31258. editor.options.minFrameHeight +
  31259. toolbarBox.offsetHeight +
  31260. bottombar.offsetHeight
  31261. }
  31262. scalelayer.style.cssText =
  31263. 'position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:' +
  31264. editorHolder.offsetWidth +
  31265. 'px;height:' +
  31266. editorHolder.offsetHeight +
  31267. 'px;z-index:' +
  31268. (editor.options.zIndex + 1)
  31269. domUtils.on(doc, 'mousemove', move)
  31270. domUtils.on(editorDocument, 'mouseup', up)
  31271. domUtils.on(doc, 'mouseup', up)
  31272. }
  31273. var me = this
  31274. //by xuheng 全屏时关掉缩放
  31275. this.editor.addListener('fullscreenchanged', function(e, fullScreen) {
  31276. if (fullScreen) {
  31277. me.disableScale()
  31278. } else {
  31279. if (me.editor.options.scaleEnabled) {
  31280. me.enableScale()
  31281. var tmpNode = me.editor.document.createElement('span')
  31282. me.editor.body.appendChild(tmpNode)
  31283. me.editor.body.style.height =
  31284. Math.max(
  31285. domUtils.getXY(tmpNode).y,
  31286. me.editor.iframe.offsetHeight - 20
  31287. ) + 'px'
  31288. domUtils.remove(tmpNode)
  31289. }
  31290. }
  31291. })
  31292. function move(event) {
  31293. clearSelection()
  31294. var e = event || window.event
  31295. pageX = e.pageX || doc.documentElement.scrollLeft + e.clientX
  31296. pageY = e.pageY || doc.documentElement.scrollTop + e.clientY
  31297. scaleWidth = pageX - position.x
  31298. scaleHeight = pageY - position.y
  31299. if (scaleWidth >= minEditorWidth) {
  31300. isMouseMove = true
  31301. scalelayer.style.width = scaleWidth + 'px'
  31302. }
  31303. if (scaleHeight >= minEditorHeight) {
  31304. isMouseMove = true
  31305. scalelayer.style.height = scaleHeight + 'px'
  31306. }
  31307. }
  31308. function up() {
  31309. if (isMouseMove) {
  31310. isMouseMove = false
  31311. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2
  31312. editorHolder.style.width = editor.ui._actualFrameWidth + 'px'
  31313. editor.setHeight(
  31314. scalelayer.offsetHeight -
  31315. bottombar.offsetHeight -
  31316. toolbarBox.offsetHeight -
  31317. 2,
  31318. true
  31319. )
  31320. }
  31321. if (scalelayer) {
  31322. scalelayer.style.display = 'none'
  31323. }
  31324. clearSelection()
  31325. domUtils.un(doc, 'mousemove', move)
  31326. domUtils.un(editorDocument, 'mouseup', up)
  31327. domUtils.un(doc, 'mouseup', up)
  31328. }
  31329. function clearSelection() {
  31330. if (browser.ie) doc.selection.clear()
  31331. else window.getSelection().removeAllRanges()
  31332. }
  31333. this.enableScale = function() {
  31334. //trace:2868
  31335. if (editor.queryCommandState('source') == 1) return
  31336. scale.style.display = ''
  31337. this.scaleEnabled = true
  31338. domUtils.on(scale, 'mousedown', down)
  31339. }
  31340. this.disableScale = function() {
  31341. scale.style.display = 'none'
  31342. this.scaleEnabled = false
  31343. domUtils.un(scale, 'mousedown', down)
  31344. }
  31345. },
  31346. isFullScreen: function() {
  31347. return this._fullscreen
  31348. },
  31349. postRender: function() {
  31350. UIBase.prototype.postRender.call(this)
  31351. for (var i = 0; i < this.toolbars.length; i++) {
  31352. this.toolbars[i].postRender()
  31353. }
  31354. var me = this
  31355. var timerId,
  31356. domUtils = baidu.editor.dom.domUtils,
  31357. updateFullScreenTime = function() {
  31358. clearTimeout(timerId)
  31359. timerId = setTimeout(function() {
  31360. me._updateFullScreen()
  31361. })
  31362. }
  31363. domUtils.on(window, 'resize', updateFullScreenTime)
  31364. me.addListener('destroy', function() {
  31365. domUtils.un(window, 'resize', updateFullScreenTime)
  31366. clearTimeout(timerId)
  31367. })
  31368. },
  31369. showToolbarMsg: function(msg, flag) {
  31370. this.getDom('toolbarmsg_label').innerHTML = msg
  31371. this.getDom('toolbarmsg').style.display = ''
  31372. //
  31373. if (!flag) {
  31374. var w = this.getDom('upload_dialog')
  31375. w.style.display = 'none'
  31376. }
  31377. },
  31378. hideToolbarMsg: function() {
  31379. this.getDom('toolbarmsg').style.display = 'none'
  31380. },
  31381. mapUrl: function(url) {
  31382. return url
  31383. ? url.replace('~/', this.editor.options.UEDITOR_HOME_URL || '')
  31384. : ''
  31385. },
  31386. triggerLayout: function() {
  31387. var dom = this.getDom()
  31388. if (dom.style.zoom == '1') {
  31389. dom.style.zoom = '100%'
  31390. } else {
  31391. dom.style.zoom = '1'
  31392. }
  31393. }
  31394. }
  31395. utils.inherits(EditorUI, baidu.editor.ui.UIBase)
  31396. var instances = {}
  31397. UE.ui.Editor = function(options) {
  31398. var editor = new UE.Editor(options)
  31399. editor.options.editor = editor
  31400. utils.loadFile(document, {
  31401. href:
  31402. editor.options.themePath + editor.options.theme + '/css/ueditor.css',
  31403. tag: 'link',
  31404. type: 'text/css',
  31405. rel: 'stylesheet'
  31406. })
  31407. var oldRender = editor.render
  31408. editor.render = function(holder) {
  31409. if (holder.constructor === String) {
  31410. editor.key = holder
  31411. instances[holder] = editor
  31412. }
  31413. utils.domReady(function() {
  31414. editor.langIsReady
  31415. ? renderUI()
  31416. : editor.addListener('langReady', renderUI)
  31417. function renderUI() {
  31418. editor.setOpt({
  31419. labelMap: editor.options.labelMap || editor.getLang('labelMap')
  31420. })
  31421. new EditorUI(editor.options)
  31422. if (holder) {
  31423. if (holder.constructor === String) {
  31424. holder = document.getElementById(holder)
  31425. }
  31426. holder &&
  31427. holder.getAttribute('name') &&
  31428. (editor.options.textarea = holder.getAttribute('name'))
  31429. if (holder && /script|textarea/gi.test(holder.tagName)) {
  31430. var newDiv = document.createElement('div')
  31431. holder.parentNode.insertBefore(newDiv, holder)
  31432. var cont = holder.value || holder.innerHTML
  31433. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont)
  31434. ? editor.options.initialContent
  31435. : cont
  31436. .replace(/>[\n\r\t]+([ ]{4})+/g, '>')
  31437. .replace(/[\n\r\t]+([ ]{4})+</g, '<')
  31438. .replace(/>[\n\r\t]+</g, '><')
  31439. holder.className && (newDiv.className = holder.className)
  31440. holder.style.cssText &&
  31441. (newDiv.style.cssText = holder.style.cssText)
  31442. if (/textarea/i.test(holder.tagName)) {
  31443. editor.textarea = holder
  31444. editor.textarea.style.display = 'none'
  31445. } else {
  31446. holder.parentNode.removeChild(holder)
  31447. }
  31448. if (holder.id) {
  31449. newDiv.id = holder.id
  31450. domUtils.removeAttributes(holder, 'id')
  31451. }
  31452. holder = newDiv
  31453. holder.innerHTML = ''
  31454. }
  31455. }
  31456. domUtils.addClass(holder, 'edui-' + editor.options.theme)
  31457. editor.ui.render(holder)
  31458. var opt = editor.options
  31459. //给实例添加一个编辑器的容器引用
  31460. editor.container = editor.ui.getDom()
  31461. var parents = domUtils.findParents(holder, true)
  31462. var displays = []
  31463. for (var i = 0, ci; (ci = parents[i]); i++) {
  31464. displays[i] = ci.style.display
  31465. ci.style.display = 'block'
  31466. }
  31467. if (opt.initialFrameWidth) {
  31468. opt.minFrameWidth = opt.initialFrameWidth
  31469. } else {
  31470. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth
  31471. var styleWidth = holder.style.width
  31472. if (/%$/.test(styleWidth)) {
  31473. opt.initialFrameWidth = styleWidth
  31474. }
  31475. }
  31476. if (opt.initialFrameHeight) {
  31477. opt.minFrameHeight = opt.initialFrameHeight
  31478. } else {
  31479. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight
  31480. }
  31481. for (var i = 0, ci; (ci = parents[i]); i++) {
  31482. ci.style.display = displays[i]
  31483. }
  31484. //编辑器最外容器设置了高度,会导致,编辑器不占位
  31485. //todo 先去掉,没有找到原因
  31486. if (holder.style.height) {
  31487. holder.style.height = ''
  31488. }
  31489. editor.container.style.width =
  31490. opt.initialFrameWidth +
  31491. (/%$/.test(opt.initialFrameWidth) ? '' : 'px')
  31492. editor.container.style.zIndex = opt.zIndex
  31493. oldRender.call(editor, editor.ui.getDom('iframeholder'))
  31494. editor.fireEvent('afteruiready')
  31495. }
  31496. })
  31497. }
  31498. return editor
  31499. }
  31500. /**
  31501. * @file
  31502. * @name UE
  31503. * @short UE
  31504. * @desc UEditor的顶部命名空间
  31505. */
  31506. /**
  31507. * @name getEditor
  31508. * @since 1.2.4+
  31509. * @grammar UE.getEditor(id,[opt]) => Editor实例
  31510. * @desc 提供一个全局的方法得到编辑器实例
  31511. *
  31512. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  31513. * * ''opt'' 编辑器的可选参数
  31514. * @example
  31515. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  31516. * this.setContent('hello')
  31517. * }});
  31518. * UE.getEditor('containerId'); //返回刚创建的实例
  31519. *
  31520. */
  31521. UE.getEditor = function(id, opt) {
  31522. var editor = instances[id]
  31523. if (!editor) {
  31524. editor = instances[id] = new UE.ui.Editor(opt)
  31525. editor.render(id)
  31526. }
  31527. return editor
  31528. }
  31529. UE.delEditor = function(id) {
  31530. var editor
  31531. if ((editor = instances[id])) {
  31532. editor.key && editor.destroy()
  31533. delete instances[id]
  31534. }
  31535. }
  31536. UE.registerUI = function(uiName, fn, index, editorId) {
  31537. utils.each(uiName.split(/\s+/), function(name) {
  31538. UE._customizeUI[name] = {
  31539. id: editorId,
  31540. execFn: fn,
  31541. index: index
  31542. }
  31543. })
  31544. }
  31545. })()
  31546. // adapter/message.js
  31547. UE.registerUI('message', function(editor) {
  31548. var editorui = baidu.editor.ui
  31549. var Message = editorui.Message
  31550. var holder
  31551. var _messageItems = []
  31552. var me = editor
  31553. me.addListener('ready', function() {
  31554. holder = document.getElementById(me.ui.id + '_message_holder')
  31555. updateHolderPos()
  31556. setTimeout(function() {
  31557. updateHolderPos()
  31558. }, 500)
  31559. })
  31560. me.addListener('showmessage', function(type, opt) {
  31561. opt = utils.isString(opt)
  31562. ? {
  31563. content: opt
  31564. }
  31565. : opt
  31566. var message = new Message({
  31567. timeout: opt.timeout,
  31568. type: opt.type,
  31569. content: opt.content,
  31570. keepshow: opt.keepshow,
  31571. editor: me
  31572. }),
  31573. mid = opt.id || 'msg_' + (+new Date()).toString(36)
  31574. message.render(holder)
  31575. _messageItems[mid] = message
  31576. message.reset(opt)
  31577. updateHolderPos()
  31578. return mid
  31579. })
  31580. me.addListener('updatemessage', function(type, id, opt) {
  31581. opt = utils.isString(opt)
  31582. ? {
  31583. content: opt
  31584. }
  31585. : opt
  31586. var message = _messageItems[id]
  31587. message.render(holder)
  31588. message && message.reset(opt)
  31589. })
  31590. me.addListener('hidemessage', function(type, id) {
  31591. var message = _messageItems[id]
  31592. message && message.hide()
  31593. })
  31594. function updateHolderPos() {
  31595. var toolbarbox = me.ui.getDom('toolbarbox')
  31596. if (toolbarbox) {
  31597. holder.style.top = toolbarbox.offsetHeight + 3 + 'px'
  31598. }
  31599. holder.style.zIndex =
  31600. Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1
  31601. }
  31602. })
  31603. // adapter/autosave.js
  31604. UE.registerUI('autosave', function(editor) {
  31605. var timer = null,
  31606. uid = null
  31607. editor.on('afterautosave', function() {
  31608. clearTimeout(timer)
  31609. timer = setTimeout(function() {
  31610. if (uid) {
  31611. editor.trigger('hidemessage', uid)
  31612. }
  31613. uid = editor.trigger('showmessage', {
  31614. content: editor.getLang('autosave.success'),
  31615. timeout: 2000
  31616. })
  31617. }, 2000)
  31618. })
  31619. })
  31620. })()