ueditor.all.js 988 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039
  1. /*!
  2. * ueditor
  3. * version: 2.1.20
  4. * build: Fri Jun 24 2022 14:43:56 GMT+0800 (中国标准时间)
  5. */
  6. (function(){
  7. // editor.js
  8. UEDITOR_CONFIG = window.UEDITOR_CONFIG || {};
  9. var baidu = window.baidu || {};
  10. window.baidu = baidu;
  11. window.UE = baidu.editor = {
  12. plugins: {},
  13. commands: {},
  14. instants: {},
  15. I18N: {},
  16. _customizeUI: {},
  17. version: "1.5.0"
  18. };
  19. var dom = (UE.dom = {});
  20. // core/browser.js
  21. /**
  22. * 浏览器判断模块
  23. * @file
  24. * @module UE.browser
  25. * @since 1.2.6.1
  26. */
  27. /**
  28. * 提供浏览器检测的模块
  29. * @unfile
  30. * @module UE.browser
  31. */
  32. var browser = (UE.browser = (function() {
  33. var agent = navigator.userAgent.toLowerCase(),
  34. opera = window.opera,
  35. browser = {
  36. /**
  37. * @property {boolean} ie 检测当前浏览器是否为IE
  38. * @example
  39. * ```javascript
  40. * if ( UE.browser.ie ) {
  41. * console.log( '当前浏览器是IE' );
  42. * }
  43. * ```
  44. */
  45. ie: /(msie\s|trident.*rv:)([\w.]+)/i.test(agent),
  46. /**
  47. * @property {boolean} opera 检测当前浏览器是否为Opera
  48. * @example
  49. * ```javascript
  50. * if ( UE.browser.opera ) {
  51. * console.log( '当前浏览器是Opera' );
  52. * }
  53. * ```
  54. */
  55. opera: !!opera && opera.version,
  56. /**
  57. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  58. * @example
  59. * ```javascript
  60. * if ( UE.browser.webkit ) {
  61. * console.log( '当前浏览器是webkit内核浏览器' );
  62. * }
  63. * ```
  64. */
  65. webkit: agent.indexOf(" applewebkit/") > -1,
  66. /**
  67. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  68. * @example
  69. * ```javascript
  70. * if ( UE.browser.mac ) {
  71. * console.log( '当前浏览器运行在mac平台下' );
  72. * }
  73. * ```
  74. */
  75. mac: agent.indexOf("macintosh") > -1,
  76. /**
  77. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  78. * @example
  79. * ```javascript
  80. * if ( UE.browser.quirks ) {
  81. * console.log( '当前浏览器运行处于“怪异模式”' );
  82. * }
  83. * ```
  84. */
  85. quirks: document.compatMode == "BackCompat"
  86. };
  87. /**
  88. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  89. * @example
  90. * ```javascript
  91. * if ( UE.browser.gecko ) {
  92. * console.log( '当前浏览器内核是gecko内核' );
  93. * }
  94. * ```
  95. */
  96. browser.gecko =
  97. navigator.product == "Gecko" &&
  98. !browser.webkit &&
  99. !browser.opera &&
  100. !browser.ie;
  101. var version = 0;
  102. // Internet Explorer 6.0+
  103. if (browser.ie) {
  104. var v1 = agent.match(/(?:msie\s([\w.]+))/);
  105. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/);
  106. if (v1 && v2 && v1[1] && v2[1]) {
  107. version = Math.max(v1[1] * 1, v2[1] * 1);
  108. } else if (v1 && v1[1]) {
  109. version = v1[1] * 1;
  110. } else if (v2 && v2[1]) {
  111. version = v2[1] * 1;
  112. } else {
  113. version = 0;
  114. }
  115. browser.ie11Compat = document.documentMode == 11;
  116. /**
  117. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  118. * @warning 如果浏览器不是IE, 则该值为undefined
  119. * @example
  120. * ```javascript
  121. * if ( UE.browser.ie9Compat ) {
  122. * console.log( '当前浏览器运行在IE9兼容模式下' );
  123. * }
  124. * ```
  125. */
  126. browser.ie9Compat = document.documentMode == 9;
  127. /**
  128. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  129. * @warning 如果浏览器不是IE, 则该值为undefined
  130. * @example
  131. * ```javascript
  132. * if ( UE.browser.ie8 ) {
  133. * console.log( '当前浏览器是IE8浏览器' );
  134. * }
  135. * ```
  136. */
  137. browser.ie8 = !!document.documentMode;
  138. /**
  139. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  140. * @warning 如果浏览器不是IE, 则该值为undefined
  141. * @example
  142. * ```javascript
  143. * if ( UE.browser.ie8Compat ) {
  144. * console.log( '当前浏览器运行在IE8兼容模式下' );
  145. * }
  146. * ```
  147. */
  148. browser.ie8Compat = document.documentMode == 8;
  149. /**
  150. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  151. * @warning 如果浏览器不是IE, 则该值为undefined
  152. * @example
  153. * ```javascript
  154. * if ( UE.browser.ie7Compat ) {
  155. * console.log( '当前浏览器运行在IE7兼容模式下' );
  156. * }
  157. * ```
  158. */
  159. browser.ie7Compat =
  160. (version == 7 && !document.documentMode) || document.documentMode == 7;
  161. /**
  162. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  163. * @warning 如果浏览器不是IE, 则该值为undefined
  164. * @example
  165. * ```javascript
  166. * if ( UE.browser.ie6Compat ) {
  167. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  168. * }
  169. * ```
  170. */
  171. browser.ie6Compat = version < 7 || browser.quirks;
  172. browser.ie9above = version > 8;
  173. browser.ie9below = version < 9;
  174. browser.ie11above = version > 10;
  175. browser.ie11below = version < 11;
  176. }
  177. // Gecko.
  178. if (browser.gecko) {
  179. var geckoRelease = agent.match(/rv:([\d\.]+)/);
  180. if (geckoRelease) {
  181. geckoRelease = geckoRelease[1].split(".");
  182. version =
  183. geckoRelease[0] * 10000 +
  184. (geckoRelease[1] || 0) * 100 +
  185. (geckoRelease[2] || 0) * 1;
  186. }
  187. }
  188. /**
  189. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  190. * @warning 如果浏览器不是chrome, 则该值为undefined
  191. * @example
  192. * ```javascript
  193. * if ( UE.browser.chrome ) {
  194. * console.log( '当前浏览器是Chrome' );
  195. * }
  196. * ```
  197. */
  198. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  199. browser.chrome = +RegExp["\x241"];
  200. }
  201. /**
  202. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  203. * @warning 如果浏览器不是safari, 则该值为undefined
  204. * @example
  205. * ```javascript
  206. * if ( UE.browser.safari ) {
  207. * console.log( '当前浏览器是Safari' );
  208. * }
  209. * ```
  210. */
  211. if (
  212. /(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) &&
  213. !/chrome/i.test(agent)
  214. ) {
  215. browser.safari = +(RegExp["\x241"] || RegExp["\x242"]);
  216. }
  217. // Opera 9.50+
  218. if (browser.opera) version = parseFloat(opera.version());
  219. // WebKit 522+ (Safari 3+)
  220. if (browser.webkit)
  221. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1]);
  222. /**
  223. * @property { Number } version 检测当前浏览器版本号
  224. * @remind
  225. * <ul>
  226. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  227. * <li>gecko系列会返回10900,158900等</li>
  228. * <li>webkit系列会返回其build号 (如 522等)</li>
  229. * </ul>
  230. * @example
  231. * ```javascript
  232. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  233. * ```
  234. */
  235. browser.version = version;
  236. /**
  237. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  238. * @example
  239. * ```javascript
  240. * if ( UE.browser.isCompatible ) {
  241. * console.log( '浏览器与UEditor能够良好兼容' );
  242. * }
  243. * ```
  244. */
  245. browser.isCompatible =
  246. !browser.mobile &&
  247. ((browser.ie && version >= 6) ||
  248. (browser.gecko && version >= 10801) ||
  249. (browser.opera && version >= 9.5) ||
  250. (browser.air && version >= 1) ||
  251. (browser.webkit && version >= 522) ||
  252. false);
  253. return browser;
  254. })());
  255. //快捷方式
  256. var ie = browser.ie,
  257. webkit = browser.webkit,
  258. gecko = browser.gecko,
  259. opera = browser.opera;
  260. // core/utils.js
  261. /**
  262. * 工具函数包
  263. * @file
  264. * @module UE.utils
  265. * @since 1.2.6.1
  266. */
  267. /**
  268. * UEditor封装使用的静态工具函数
  269. * @module UE.utils
  270. * @unfile
  271. */
  272. var utils = (UE.utils = {
  273. /**
  274. * 用给定的迭代器遍历对象
  275. * @method each
  276. * @param { Object } obj 需要遍历的对象
  277. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  278. * @example
  279. * ```javascript
  280. * var demoObj = {
  281. * key1: 1,
  282. * key2: 2
  283. * };
  284. *
  285. * //output: key1: 1, key2: 2
  286. * UE.utils.each( demoObj, funciton ( value, key ) {
  287. *
  288. * console.log( key + ":" + value );
  289. *
  290. * } );
  291. * ```
  292. */
  293. /**
  294. * 用给定的迭代器遍历数组或类数组对象
  295. * @method each
  296. * @param { Array } array 需要遍历的数组或者类数组
  297. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  298. * @example
  299. * ```javascript
  300. * var divs = document.getElmentByTagNames( "div" );
  301. *
  302. * //output: 0: DIV, 1: DIV ...
  303. * UE.utils.each( divs, funciton ( value, key ) {
  304. *
  305. * console.log( key + ":" + value.tagName );
  306. *
  307. * } );
  308. * ```
  309. */
  310. each: function(obj, iterator, context) {
  311. if (obj == null) return;
  312. if (obj.length === +obj.length) {
  313. for (var i = 0, l = obj.length; i < l; i++) {
  314. if (iterator.call(context, obj[i], i, obj) === false) return false;
  315. }
  316. } else {
  317. for (var key in obj) {
  318. if (obj.hasOwnProperty(key)) {
  319. if (iterator.call(context, obj[key], key, obj) === false)
  320. return false;
  321. }
  322. }
  323. }
  324. },
  325. /**
  326. * 以给定对象作为原型创建一个新对象
  327. * @method makeInstance
  328. * @param { Object } protoObject 该对象将作为新创建对象的原型
  329. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  330. * @example
  331. * ```javascript
  332. *
  333. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  334. *
  335. * var newObject = UE.utils.makeInstance( protoObject );
  336. * //output: Hello UEditor!
  337. * newObject.sayHello();
  338. * ```
  339. */
  340. makeInstance: function(obj) {
  341. var noop = new Function();
  342. noop.prototype = obj;
  343. obj = new noop();
  344. noop.prototype = null;
  345. return obj;
  346. },
  347. /**
  348. * 将source对象中的属性扩展到target对象上
  349. * @method extend
  350. * @remind 该方法将强制把source对象上的属性复制到target对象上
  351. * @see UE.utils.extend(Object,Object,Boolean)
  352. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  353. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  354. * @return { Object } 返回target对象
  355. * @example
  356. * ```javascript
  357. *
  358. * var target = { name: 'target', sex: 1 },
  359. * source = { name: 'source', age: 17 };
  360. *
  361. * UE.utils.extend( target, source );
  362. *
  363. * //output: { name: 'source', sex: 1, age: 17 }
  364. * console.log( target );
  365. *
  366. * ```
  367. */
  368. /**
  369. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  370. * 源对象属性名相同的属性值。
  371. * @method extend
  372. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  373. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  374. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  375. * @return { Object } 返回target对象
  376. * @example
  377. * ```javascript
  378. *
  379. * var target = { name: 'target', sex: 1 },
  380. * source = { name: 'source', age: 17 };
  381. *
  382. * UE.utils.extend( target, source, true );
  383. *
  384. * //output: { name: 'target', sex: 1, age: 17 }
  385. * console.log( target );
  386. *
  387. * ```
  388. */
  389. extend: function(t, s, b) {
  390. if (s) {
  391. for (var k in s) {
  392. if (!b || !t.hasOwnProperty(k)) {
  393. t[k] = s[k];
  394. }
  395. }
  396. }
  397. return t;
  398. },
  399. /**
  400. * 将给定的多个对象的属性复制到目标对象target上
  401. * @method extend2
  402. * @remind 该方法将强制把源对象上的属性复制到target对象上
  403. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  404. * 将会覆盖掉之前的值。
  405. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  406. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  407. * @return { Object } 返回target对象
  408. * @example
  409. * ```javascript
  410. *
  411. * var target = {},
  412. * source1 = { name: 'source', age: 17 },
  413. * source2 = { title: 'dev' };
  414. *
  415. * UE.utils.extend2( target, source1, source2 );
  416. *
  417. * //output: { name: 'source', age: 17, title: 'dev' }
  418. * console.log( target );
  419. *
  420. * ```
  421. */
  422. extend2: function(t) {
  423. var a = arguments;
  424. for (var i = 1; i < a.length; i++) {
  425. var x = a[i];
  426. for (var k in x) {
  427. if (!t.hasOwnProperty(k)) {
  428. t[k] = x[k];
  429. }
  430. }
  431. }
  432. return t;
  433. },
  434. /**
  435. * 模拟继承机制, 使得subClass继承自superClass
  436. * @method inherits
  437. * @param { Object } subClass 子类对象
  438. * @param { Object } superClass 超类对象
  439. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  440. * @return { Object } 继承superClass后的子类对象
  441. * @example
  442. * ```javascript
  443. * function SuperClass(){
  444. * this.name = "小李";
  445. * }
  446. *
  447. * SuperClass.prototype = {
  448. * hello:function(str){
  449. * console.log(this.name + str);
  450. * }
  451. * }
  452. *
  453. * function SubClass(){
  454. * this.name = "小张";
  455. * }
  456. *
  457. * UE.utils.inherits(SubClass,SuperClass);
  458. *
  459. * var sub = new SubClass();
  460. * //output: '小张早上好!
  461. * sub.hello("早上好!");
  462. * ```
  463. */
  464. inherits: function(subClass, superClass) {
  465. var oldP = subClass.prototype,
  466. newP = utils.makeInstance(superClass.prototype);
  467. utils.extend(newP, oldP, true);
  468. subClass.prototype = newP;
  469. return (newP.constructor = subClass);
  470. },
  471. /**
  472. * 用指定的context对象作为函数fn的上下文
  473. * @method bind
  474. * @param { Function } fn 需要绑定上下文的函数对象
  475. * @param { Object } content 函数fn新的上下文对象
  476. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  477. * @example
  478. * ```javascript
  479. *
  480. * var name = 'window',
  481. * newTest = null;
  482. *
  483. * function test () {
  484. * console.log( this.name );
  485. * }
  486. *
  487. * newTest = UE.utils.bind( test, { name: 'object' } );
  488. *
  489. * //output: object
  490. * newTest();
  491. *
  492. * //output: window
  493. * test();
  494. *
  495. * ```
  496. */
  497. bind: function(fn, context) {
  498. return function() {
  499. return fn.apply(context, arguments);
  500. };
  501. },
  502. /**
  503. * 创建延迟指定时间后执行的函数fn
  504. * @method defer
  505. * @param { Function } fn 需要延迟执行的函数对象
  506. * @param { int } delay 延迟的时间, 单位是毫秒
  507. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  508. * 而不能保证刚好到达延迟时间时执行。
  509. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  510. * @example
  511. * ```javascript
  512. * var start = 0;
  513. *
  514. * function test(){
  515. * console.log( new Date() - start );
  516. * }
  517. *
  518. * var testDefer = UE.utils.defer( test, 1000 );
  519. * //
  520. * start = new Date();
  521. * //output: (大约在1000毫秒之后输出) 1000
  522. * testDefer();
  523. * ```
  524. */
  525. /**
  526. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  527. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  528. * @method defer
  529. * @param { Function } fn 需要延迟执行的函数对象
  530. * @param { int } delay 延迟的时间, 单位是毫秒
  531. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  532. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  533. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  534. * 而不能保证刚好到达延迟时间时执行。
  535. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  536. * @example
  537. * ```javascript
  538. *
  539. * function test(){
  540. * console.log(1);
  541. * }
  542. *
  543. * var testDefer = UE.utils.defer( test, 1000, true );
  544. *
  545. * //output: (两次调用仅有一次输出) 1
  546. * testDefer();
  547. * testDefer();
  548. * ```
  549. */
  550. defer: function(fn, delay, exclusion) {
  551. var timerID;
  552. return function() {
  553. if (exclusion) {
  554. clearTimeout(timerID);
  555. }
  556. timerID = setTimeout(fn, delay);
  557. };
  558. },
  559. /**
  560. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  561. * @method indexOf
  562. * @remind 该方法的匹配过程使用的是恒等“===”
  563. * @param { Array } array 需要查找的数组对象
  564. * @param { * } item 需要在目标数组中查找的值
  565. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  566. * @example
  567. * ```javascript
  568. * var item = 1,
  569. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  570. *
  571. * //output: 4
  572. * console.log( UE.utils.indexOf( arr, item ) );
  573. * ```
  574. */
  575. /**
  576. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  577. * @method indexOf
  578. * @remind 该方法的匹配过程使用的是恒等“===”
  579. * @param { Array } array 需要查找的数组对象
  580. * @param { * } item 需要在目标数组中查找的值
  581. * @param { int } start 搜索的起始位置
  582. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  583. * @example
  584. * ```javascript
  585. * var item = 1,
  586. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  587. *
  588. * //output: 9
  589. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  590. * ```
  591. */
  592. indexOf: function(array, item, start) {
  593. var index = -1;
  594. start = this.isNumber(start) ? start : 0;
  595. this.each(array, function(v, i) {
  596. if (i >= start && v === item) {
  597. index = i;
  598. return false;
  599. }
  600. });
  601. return index;
  602. },
  603. /**
  604. * 移除数组array中所有的元素item
  605. * @method removeItem
  606. * @param { Array } array 要移除元素的目标数组
  607. * @param { * } item 将要被移除的元素
  608. * @remind 该方法的匹配过程使用的是恒等“===”
  609. * @example
  610. * ```javascript
  611. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  612. *
  613. * UE.utils.removeItem( arr, 4 );
  614. * //output: [ 5, 7, 1, 3, 6 ]
  615. * console.log( arr );
  616. *
  617. * ```
  618. */
  619. removeItem: function(array, item) {
  620. for (var i = 0, l = array.length; i < l; i++) {
  621. if (array[i] === item) {
  622. array.splice(i, 1);
  623. i--;
  624. }
  625. }
  626. },
  627. /**
  628. * 删除字符串str的首尾空格
  629. * @method trim
  630. * @param { String } str 需要删除首尾空格的字符串
  631. * @return { String } 删除了首尾的空格后的字符串
  632. * @example
  633. * ```javascript
  634. *
  635. * var str = " UEdtior ";
  636. *
  637. * //output: 9
  638. * console.log( str.length );
  639. *
  640. * //output: 7
  641. * console.log( UE.utils.trim( " UEdtior " ).length );
  642. *
  643. * //output: 9
  644. * console.log( str.length );
  645. *
  646. * ```
  647. */
  648. trim: function(str) {
  649. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, "");
  650. },
  651. /**
  652. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  653. * @method listToMap
  654. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  655. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  656. * @return { Object } 转化之后的hash对象
  657. * @example
  658. * ```javascript
  659. *
  660. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  661. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  662. *
  663. * ```
  664. */
  665. /**
  666. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  667. * @method listToMap
  668. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  669. * @param { Array } arr 字符串数组
  670. * @return { Object } 转化之后的hash对象
  671. * @example
  672. * ```javascript
  673. *
  674. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  675. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  676. *
  677. * ```
  678. */
  679. listToMap: function(list) {
  680. if (!list) return {};
  681. list = utils.isArray(list) ? list : list.split(",");
  682. for (var i = 0, ci, obj = {}; (ci = list[i++]); ) {
  683. obj[ci.toUpperCase()] = obj[ci] = 1;
  684. }
  685. return obj;
  686. },
  687. /**
  688. * 将str中的html符号转义,将转义“',&,<,",>,”,“”七个字符
  689. * @method unhtml
  690. * @param { String } str 需要转义的字符串
  691. * @return { String } 转义后的字符串
  692. * @example
  693. * ```javascript
  694. * var html = '<body>&</body>';
  695. *
  696. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  697. * console.log( UE.utils.unhtml( html ) );
  698. *
  699. * ```
  700. */
  701. unhtml: function(str, reg) {
  702. return str
  703. ? str.replace(
  704. reg || /[&<">'](?:(amp|lt|ldquo|rdquo|quot|gt|#39|nbsp|#\d+);)?/g,
  705. function(a, b) {
  706. if (b) {
  707. return a;
  708. } else {
  709. return {
  710. "<": "&lt;",
  711. "&": "&amp;",
  712. '"': "&quot;",
  713. "“": "&ldquo;",
  714. "”": "&rdquo;",
  715. ">": "&gt;",
  716. "'": "&#39;"
  717. }[a];
  718. }
  719. }
  720. )
  721. : "";
  722. },
  723. /**
  724. * 将str中的转义字符还原成html字符
  725. * @see UE.utils.unhtml(String);
  726. * @method html
  727. * @param { String } str 需要逆转义的字符串
  728. * @return { String } 逆转义后的字符串
  729. * @example
  730. * ```javascript
  731. *
  732. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  733. *
  734. * //output: <body>&</body>
  735. * console.log( UE.utils.html( str ) );
  736. *
  737. * ```
  738. */
  739. html: function(str) {
  740. return str
  741. ? str.replace(/&((g|l|quo|ldquo|rdquo)t|amp|#39|nbsp);/g, function(m) {
  742. return {
  743. "&lt;": "<",
  744. "&amp;": "&",
  745. "&quot;": '"',
  746. "&ldquo;": "“",
  747. "&rdquo;": "”",
  748. "&gt;": ">",
  749. "&#39;": "'",
  750. "&nbsp;": " "
  751. }[m];
  752. })
  753. : "";
  754. },
  755. /**
  756. * 将css样式转换为驼峰的形式
  757. * @method cssStyleToDomStyle
  758. * @param { String } cssName 需要转换的css样式名
  759. * @return { String } 转换成驼峰形式后的css样式名
  760. * @example
  761. * ```javascript
  762. *
  763. * var str = 'border-top';
  764. *
  765. * //output: borderTop
  766. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  767. *
  768. * ```
  769. */
  770. cssStyleToDomStyle: (function() {
  771. var test = document.createElement("div").style,
  772. cache = {
  773. float: test.cssFloat != undefined
  774. ? "cssFloat"
  775. : test.styleFloat != undefined ? "styleFloat" : "float"
  776. };
  777. return function(cssName) {
  778. return (
  779. cache[cssName] ||
  780. (cache[cssName] = cssName.toLowerCase().replace(/-./g, function(match) {
  781. return match.charAt(1).toUpperCase();
  782. }))
  783. );
  784. };
  785. })(),
  786. /**
  787. * 动态加载文件到doc中
  788. * @method loadFile
  789. * @param { DomDocument } document 需要加载资源文件的文档对象
  790. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  791. * @example
  792. * ```javascript
  793. *
  794. * UE.utils.loadFile( document, {
  795. * src:"test.js",
  796. * tag:"script",
  797. * type:"text/javascript",
  798. * defer:"defer"
  799. * } );
  800. *
  801. * ```
  802. */
  803. /**
  804. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  805. * @method loadFile
  806. * @param { DomDocument } document 需要加载资源文件的文档对象
  807. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  808. * @param { Function } fn 资源文件加载成功之后执行的回调
  809. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  810. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  811. * @example
  812. * ```javascript
  813. *
  814. * UE.utils.loadFile( document, {
  815. * src:"test.js",
  816. * tag:"script",
  817. * type:"text/javascript",
  818. * defer:"defer"
  819. * }, function () {
  820. * console.log('加载成功');
  821. * } );
  822. *
  823. * ```
  824. */
  825. loadFile: (function() {
  826. var tmpList = [];
  827. function getItem(doc, obj) {
  828. try {
  829. for (var i = 0, ci; (ci = tmpList[i++]); ) {
  830. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  831. return ci;
  832. }
  833. }
  834. } catch (e) {
  835. return null;
  836. }
  837. }
  838. return function(doc, obj, fn) {
  839. var item = getItem(doc, obj);
  840. if (item) {
  841. if (item.ready) {
  842. fn && fn();
  843. } else {
  844. item.funs.push(fn);
  845. }
  846. return;
  847. }
  848. tmpList.push({
  849. doc: doc,
  850. url: obj.src || obj.href,
  851. funs: [fn]
  852. });
  853. if (!doc.body) {
  854. var html = [];
  855. for (var p in obj) {
  856. if (p == "tag") continue;
  857. html.push(p + '="' + obj[p] + '"');
  858. }
  859. doc.write(
  860. "<" + obj.tag + " " + html.join(" ") + " ></" + obj.tag + ">"
  861. );
  862. return;
  863. }
  864. if (obj.id && doc.getElementById(obj.id)) {
  865. return;
  866. }
  867. var element = doc.createElement(obj.tag);
  868. delete obj.tag;
  869. for (var p in obj) {
  870. element.setAttribute(p, obj[p]);
  871. }
  872. element.onload = element.onreadystatechange = function() {
  873. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  874. item = getItem(doc, obj);
  875. if (item.funs.length > 0) {
  876. item.ready = 1;
  877. for (var fi; (fi = item.funs.pop()); ) {
  878. fi();
  879. }
  880. }
  881. element.onload = element.onreadystatechange = null;
  882. }
  883. };
  884. element.onerror = function() {
  885. throw Error(
  886. "The load " +
  887. (obj.href || obj.src) +
  888. " fails,check the url settings of file ueditor.config.js "
  889. );
  890. };
  891. doc.getElementsByTagName("head")[0].appendChild(element);
  892. };
  893. })(),
  894. /**
  895. * 判断obj对象是否为空
  896. * @method isEmptyObject
  897. * @param { * } obj 需要判断的对象
  898. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  899. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  900. * @return { Boolean } 对象是否为空
  901. * @example
  902. * ```javascript
  903. *
  904. * //output: true
  905. * console.log( UE.utils.isEmptyObject( {} ) );
  906. *
  907. * //output: true
  908. * console.log( UE.utils.isEmptyObject( [] ) );
  909. *
  910. * //output: true
  911. * console.log( UE.utils.isEmptyObject( "" ) );
  912. *
  913. * //output: false
  914. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  915. *
  916. * //output: false
  917. * console.log( UE.utils.isEmptyObject( [1] ) );
  918. *
  919. * //output: false
  920. * console.log( UE.utils.isEmptyObject( "1" ) );
  921. *
  922. * ```
  923. */
  924. isEmptyObject: function(obj) {
  925. if (obj == null) return true;
  926. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  927. for (var key in obj) if (obj.hasOwnProperty(key)) return false;
  928. return true;
  929. },
  930. /**
  931. * 把rgb格式的颜色值转换成16进制格式
  932. * @method fixColor
  933. * @param { String } rgb格式的颜色值
  934. * @param { String }
  935. * @example
  936. * rgb(255,255,255) => "#ffffff"
  937. */
  938. fixColor: function(name, value) {
  939. if (/color/i.test(name) && /rgba?/.test(value)) {
  940. var array = value.split(",");
  941. if (array.length > 3) return "";
  942. value = "#";
  943. for (var i = 0, color; (color = array[i++]); ) {
  944. color = parseInt(color.replace(/[^\d]/gi, ""), 10).toString(16);
  945. value += color.length == 1 ? "0" + color : color;
  946. }
  947. value = value.toUpperCase();
  948. }
  949. return value;
  950. },
  951. /**
  952. * 只针对border,padding,margin做了处理,因为性能问题
  953. * @public
  954. * @function
  955. * @param {String} val style字符串
  956. */
  957. optCss: function(val) {
  958. var padding, margin, border;
  959. val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function(
  960. str,
  961. key,
  962. name,
  963. val
  964. ) {
  965. if (val.split(" ").length == 1) {
  966. switch (key) {
  967. case "padding":
  968. !padding && (padding = {});
  969. padding[name] = val;
  970. return "";
  971. case "margin":
  972. !margin && (margin = {});
  973. margin[name] = val;
  974. return "";
  975. case "border":
  976. return val == "initial" ? "" : str;
  977. }
  978. }
  979. return str;
  980. });
  981. function opt(obj, name) {
  982. if (!obj) {
  983. return "";
  984. }
  985. var t = obj.top,
  986. b = obj.bottom,
  987. l = obj.left,
  988. r = obj.right,
  989. val = "";
  990. if (!t || !l || !b || !r) {
  991. for (var p in obj) {
  992. val += ";" + name + "-" + p + ":" + obj[p] + ";";
  993. }
  994. } else {
  995. val +=
  996. ";" +
  997. name +
  998. ":" +
  999. (t == b && b == l && l == r
  1000. ? t
  1001. : t == b && l == r
  1002. ? t + " " + l
  1003. : l == r
  1004. ? t + " " + l + " " + b
  1005. : t + " " + r + " " + b + " " + l) +
  1006. ";";
  1007. }
  1008. return val;
  1009. }
  1010. val += opt(padding, "padding") + opt(margin, "margin");
  1011. return val
  1012. .replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, "")
  1013. .replace(/;([ \n\r\t]+)|\1;/g, ";")
  1014. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function(a, b) {
  1015. return b ? b + ";;" : ";";
  1016. });
  1017. },
  1018. /**
  1019. * 克隆对象
  1020. * @method clone
  1021. * @param { Object } source 源对象
  1022. * @return { Object } source的一个副本
  1023. */
  1024. /**
  1025. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  1026. * @method clone
  1027. * @param { Object } source 源对象
  1028. * @param { Object } target 目标对象
  1029. * @return { Object } 附加了source对象所有属性的target对象
  1030. */
  1031. clone: function(source, target) {
  1032. var tmp;
  1033. target = target || {};
  1034. for (var i in source) {
  1035. if (source.hasOwnProperty(i)) {
  1036. tmp = source[i];
  1037. if (typeof tmp == "object") {
  1038. target[i] = utils.isArray(tmp) ? [] : {};
  1039. utils.clone(source[i], target[i]);
  1040. } else {
  1041. target[i] = tmp;
  1042. }
  1043. }
  1044. }
  1045. return target;
  1046. },
  1047. /**
  1048. * 把cm/pt为单位的值转换为px为单位的值
  1049. * @method transUnitToPx
  1050. * @param { String } 待转换的带单位的字符串
  1051. * @return { String } 转换为px为计量单位的值的字符串
  1052. * @example
  1053. * ```javascript
  1054. *
  1055. * //output: 500px
  1056. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1057. *
  1058. * //output: 27px
  1059. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1060. *
  1061. * ```
  1062. */
  1063. transUnitToPx: function(val) {
  1064. if (!/(pt|cm)/.test(val)) {
  1065. return val;
  1066. }
  1067. var unit;
  1068. val.replace(/([\d.]+)(\w+)/, function(str, v, u) {
  1069. val = v;
  1070. unit = u;
  1071. });
  1072. switch (unit) {
  1073. case "cm":
  1074. val = parseFloat(val) * 25;
  1075. break;
  1076. case "pt":
  1077. val = Math.round(parseFloat(val) * 96 / 72);
  1078. }
  1079. return val + (val ? "px" : "");
  1080. },
  1081. /**
  1082. * 在dom树ready之后执行给定的回调函数
  1083. * @method domReady
  1084. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1085. * @param { Function } fn dom树ready之后的回调函数
  1086. * @example
  1087. * ```javascript
  1088. *
  1089. * UE.utils.domReady( function () {
  1090. *
  1091. * console.log('123');
  1092. *
  1093. * } );
  1094. *
  1095. * ```
  1096. */
  1097. domReady: (function() {
  1098. var fnArr = [];
  1099. function doReady(doc) {
  1100. //确保onready只执行一次
  1101. doc.isReady = true;
  1102. for (var ci; (ci = fnArr.pop()); ci()) {}
  1103. }
  1104. return function(onready, win) {
  1105. win = win || window;
  1106. var doc = win.document;
  1107. onready && fnArr.push(onready);
  1108. if (doc.readyState === "complete") {
  1109. doReady(doc);
  1110. } else {
  1111. doc.isReady && doReady(doc);
  1112. if (browser.ie && browser.version != 11) {
  1113. (function() {
  1114. if (doc.isReady) return;
  1115. try {
  1116. doc.documentElement.doScroll("left");
  1117. } catch (error) {
  1118. setTimeout(arguments.callee, 0);
  1119. return;
  1120. }
  1121. doReady(doc);
  1122. })();
  1123. win.attachEvent("onload", function() {
  1124. doReady(doc);
  1125. });
  1126. } else {
  1127. doc.addEventListener(
  1128. "DOMContentLoaded",
  1129. function() {
  1130. doc.removeEventListener(
  1131. "DOMContentLoaded",
  1132. arguments.callee,
  1133. false
  1134. );
  1135. doReady(doc);
  1136. },
  1137. false
  1138. );
  1139. win.addEventListener(
  1140. "load",
  1141. function() {
  1142. doReady(doc);
  1143. },
  1144. false
  1145. );
  1146. }
  1147. }
  1148. };
  1149. })(),
  1150. /**
  1151. * 动态添加css样式
  1152. * @method cssRule
  1153. * @param { String } 节点名称
  1154. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1155. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1156. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1157. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1158. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1159. */
  1160. cssRule: browser.ie && browser.version != 11
  1161. ? function(key, style, doc) {
  1162. var indexList, index;
  1163. if (
  1164. style === undefined ||
  1165. (style && style.nodeType && style.nodeType == 9)
  1166. ) {
  1167. //获取样式
  1168. doc = style && style.nodeType && style.nodeType == 9
  1169. ? style
  1170. : doc || document;
  1171. indexList = doc.indexList || (doc.indexList = {});
  1172. index = indexList[key];
  1173. if (index !== undefined) {
  1174. return doc.styleSheets[index].cssText;
  1175. }
  1176. return undefined;
  1177. }
  1178. doc = doc || document;
  1179. indexList = doc.indexList || (doc.indexList = {});
  1180. index = indexList[key];
  1181. //清除样式
  1182. if (style === "") {
  1183. if (index !== undefined) {
  1184. doc.styleSheets[index].cssText = "";
  1185. delete indexList[key];
  1186. return true;
  1187. }
  1188. return false;
  1189. }
  1190. //添加样式
  1191. if (index !== undefined) {
  1192. sheetStyle = doc.styleSheets[index];
  1193. } else {
  1194. sheetStyle = doc.createStyleSheet(
  1195. "",
  1196. (index = doc.styleSheets.length)
  1197. );
  1198. indexList[key] = index;
  1199. }
  1200. sheetStyle.cssText = style;
  1201. }
  1202. : function(key, style, doc) {
  1203. var head, node;
  1204. if (
  1205. style === undefined ||
  1206. (style && style.nodeType && style.nodeType == 9)
  1207. ) {
  1208. //获取样式
  1209. doc = style && style.nodeType && style.nodeType == 9
  1210. ? style
  1211. : doc || document;
  1212. node = doc.getElementById(key);
  1213. return node ? node.innerHTML : undefined;
  1214. }
  1215. doc = doc || document;
  1216. node = doc.getElementById(key);
  1217. //清除样式
  1218. if (style === "") {
  1219. if (node) {
  1220. node.parentNode.removeChild(node);
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. //添加样式
  1226. if (node) {
  1227. node.innerHTML = style;
  1228. } else {
  1229. node = doc.createElement("style");
  1230. node.id = key;
  1231. node.innerHTML = style;
  1232. doc.getElementsByTagName("head")[0].appendChild(node);
  1233. }
  1234. },
  1235. sort: function(array, compareFn) {
  1236. compareFn =
  1237. compareFn ||
  1238. function(item1, item2) {
  1239. return item1.localeCompare(item2);
  1240. };
  1241. for (var i = 0, len = array.length; i < len; i++) {
  1242. for (var j = i, length = array.length; j < length; j++) {
  1243. if (compareFn(array[i], array[j]) > 0) {
  1244. var t = array[i];
  1245. array[i] = array[j];
  1246. array[j] = t;
  1247. }
  1248. }
  1249. }
  1250. return array;
  1251. },
  1252. serializeParam: function(json) {
  1253. var strArr = [];
  1254. for (var i in json) {
  1255. //忽略默认的几个参数
  1256. if (i == "method" || i == "timeout" || i == "async") continue;
  1257. //传递过来的对象和函数不在提交之列
  1258. if (
  1259. !(
  1260. (typeof json[i]).toLowerCase() == "function" ||
  1261. (typeof json[i]).toLowerCase() == "object"
  1262. )
  1263. ) {
  1264. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  1265. } else if (utils.isArray(json[i])) {
  1266. //支持传数组内容
  1267. for (var j = 0; j < json[i].length; j++) {
  1268. strArr.push(
  1269. encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j])
  1270. );
  1271. }
  1272. }
  1273. }
  1274. return strArr.join("&");
  1275. },
  1276. formatUrl: function(url) {
  1277. var u = url.replace(/&&/g, "&");
  1278. u = u.replace(/\?&/g, "?");
  1279. u = u.replace(/&$/g, "");
  1280. u = u.replace(/&#/g, "#");
  1281. u = u.replace(/&+/g, "&");
  1282. return u;
  1283. },
  1284. isCrossDomainUrl: function(url) {
  1285. var a = document.createElement("a");
  1286. a.href = url;
  1287. if (browser.ie) {
  1288. a.href = a.href;
  1289. }
  1290. return !(
  1291. a.protocol == location.protocol &&
  1292. a.hostname == location.hostname &&
  1293. (a.port == location.port ||
  1294. (a.port == "80" && location.port == "") ||
  1295. (a.port == "" && location.port == "80"))
  1296. );
  1297. },
  1298. clearEmptyAttrs: function(obj) {
  1299. for (var p in obj) {
  1300. if (obj[p] === "") {
  1301. delete obj[p];
  1302. }
  1303. }
  1304. return obj;
  1305. },
  1306. str2json: function(s) {
  1307. if (!utils.isString(s)) return null;
  1308. if (window.JSON) {
  1309. return JSON.parse(s);
  1310. } else {
  1311. return new Function("return " + utils.trim(s || ""))();
  1312. }
  1313. },
  1314. json2str: (function() {
  1315. if (window.JSON) {
  1316. return JSON.stringify;
  1317. } else {
  1318. var escapeMap = {
  1319. "\b": "\\b",
  1320. "\t": "\\t",
  1321. "\n": "\\n",
  1322. "\f": "\\f",
  1323. "\r": "\\r",
  1324. '"': '\\"',
  1325. "\\": "\\\\"
  1326. };
  1327. function encodeString(source) {
  1328. if (/["\\\x00-\x1f]/.test(source)) {
  1329. source = source.replace(/["\\\x00-\x1f]/g, function(match) {
  1330. var c = escapeMap[match];
  1331. if (c) {
  1332. return c;
  1333. }
  1334. c = match.charCodeAt();
  1335. return (
  1336. "\\u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16)
  1337. );
  1338. });
  1339. }
  1340. return '"' + source + '"';
  1341. }
  1342. function encodeArray(source) {
  1343. var result = ["["],
  1344. l = source.length,
  1345. preComma,
  1346. i,
  1347. item;
  1348. for (i = 0; i < l; i++) {
  1349. item = source[i];
  1350. switch (typeof item) {
  1351. case "undefined":
  1352. case "function":
  1353. case "unknown":
  1354. break;
  1355. default:
  1356. if (preComma) {
  1357. result.push(",");
  1358. }
  1359. result.push(utils.json2str(item));
  1360. preComma = 1;
  1361. }
  1362. }
  1363. result.push("]");
  1364. return result.join("");
  1365. }
  1366. function pad(source) {
  1367. return source < 10 ? "0" + source : source;
  1368. }
  1369. function encodeDate(source) {
  1370. return (
  1371. '"' +
  1372. source.getFullYear() +
  1373. "-" +
  1374. pad(source.getMonth() + 1) +
  1375. "-" +
  1376. pad(source.getDate()) +
  1377. "T" +
  1378. pad(source.getHours()) +
  1379. ":" +
  1380. pad(source.getMinutes()) +
  1381. ":" +
  1382. pad(source.getSeconds()) +
  1383. '"'
  1384. );
  1385. }
  1386. return function(value) {
  1387. switch (typeof value) {
  1388. case "undefined":
  1389. return "undefined";
  1390. case "number":
  1391. return isFinite(value) ? String(value) : "null";
  1392. case "string":
  1393. return encodeString(value);
  1394. case "boolean":
  1395. return String(value);
  1396. default:
  1397. if (value === null) {
  1398. return "null";
  1399. } else if (utils.isArray(value)) {
  1400. return encodeArray(value);
  1401. } else if (utils.isDate(value)) {
  1402. return encodeDate(value);
  1403. } else {
  1404. var result = ["{"],
  1405. encode = utils.json2str,
  1406. preComma,
  1407. item;
  1408. for (var key in value) {
  1409. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1410. item = value[key];
  1411. switch (typeof item) {
  1412. case "undefined":
  1413. case "unknown":
  1414. case "function":
  1415. break;
  1416. default:
  1417. if (preComma) {
  1418. result.push(",");
  1419. }
  1420. preComma = 1;
  1421. result.push(encode(key) + ":" + encode(item));
  1422. }
  1423. }
  1424. }
  1425. result.push("}");
  1426. return result.join("");
  1427. }
  1428. }
  1429. };
  1430. }
  1431. })(),
  1432. renderTplstr: function(tpl, data) {
  1433. return tpl.replace(/\$\{\s*(\w*?)\s*\}/g, function (match, variable) {
  1434. if (data.hasOwnProperty(variable)) {
  1435. return data[variable];
  1436. }
  1437. });
  1438. }
  1439. });
  1440. /**
  1441. * 判断给定的对象是否是字符串
  1442. * @method isString
  1443. * @param { * } object 需要判断的对象
  1444. * @return { Boolean } 给定的对象是否是字符串
  1445. */
  1446. /**
  1447. * 判断给定的对象是否是数组
  1448. * @method isArray
  1449. * @param { * } object 需要判断的对象
  1450. * @return { Boolean } 给定的对象是否是数组
  1451. */
  1452. /**
  1453. * 判断给定的对象是否是一个Function
  1454. * @method isFunction
  1455. * @param { * } object 需要判断的对象
  1456. * @return { Boolean } 给定的对象是否是Function
  1457. */
  1458. /**
  1459. * 判断给定的对象是否是Number
  1460. * @method isNumber
  1461. * @param { * } object 需要判断的对象
  1462. * @return { Boolean } 给定的对象是否是Number
  1463. */
  1464. /**
  1465. * 判断给定的对象是否是一个正则表达式
  1466. * @method isRegExp
  1467. * @param { * } object 需要判断的对象
  1468. * @return { Boolean } 给定的对象是否是正则表达式
  1469. */
  1470. /**
  1471. * 判断给定的对象是否是一个普通对象
  1472. * @method isObject
  1473. * @param { * } object 需要判断的对象
  1474. * @return { Boolean } 给定的对象是否是普通对象
  1475. */
  1476. utils.each(
  1477. ["String", "Function", "Array", "Number", "RegExp", "Object", "Date"],
  1478. function(v) {
  1479. UE.utils["is" + v] = function(obj) {
  1480. return Object.prototype.toString.apply(obj) == "[object " + v + "]";
  1481. };
  1482. }
  1483. );
  1484. // core/EventBase.js
  1485. /**
  1486. * UE采用的事件基类
  1487. * @file
  1488. * @module UE
  1489. * @class EventBase
  1490. * @since 1.2.6.1
  1491. */
  1492. /**
  1493. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1494. * @unfile
  1495. * @module UE
  1496. */
  1497. /**
  1498. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1499. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1500. * @unfile
  1501. * @module UE
  1502. * @class EventBase
  1503. */
  1504. /**
  1505. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1506. * @constructor
  1507. * @example
  1508. * ```javascript
  1509. * UE.EventBase.call(editor);
  1510. * ```
  1511. */
  1512. var EventBase = (UE.EventBase = function() {});
  1513. EventBase.prototype = {
  1514. /**
  1515. * 注册事件监听器
  1516. * @method addListener
  1517. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1518. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1519. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1520. * @example
  1521. * ```javascript
  1522. * editor.addListener('selectionchange',function(){
  1523. * console.log("选区已经变化!");
  1524. * })
  1525. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1526. * if(type == 'beforegetcontent'){
  1527. * //do something
  1528. * }else{
  1529. * //do something
  1530. * }
  1531. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1532. * })
  1533. * ```
  1534. * @see UE.EventBase:fireEvent(String)
  1535. */
  1536. addListener: function(types, listener) {
  1537. types = utils.trim(types).split(/\s+/);
  1538. for (var i = 0, ti; (ti = types[i++]); ) {
  1539. getListener(this, ti, true).push(listener);
  1540. }
  1541. },
  1542. on: function(types, listener) {
  1543. return this.addListener(types, listener);
  1544. },
  1545. off: function(types, listener) {
  1546. return this.removeListener(types, listener);
  1547. },
  1548. trigger: function() {
  1549. return this.fireEvent.apply(this, arguments);
  1550. },
  1551. /**
  1552. * 移除事件监听器
  1553. * @method removeListener
  1554. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1555. * @param { Function } fn 移除监听事件的函数引用
  1556. * @example
  1557. * ```javascript
  1558. * //changeCallback为方法体
  1559. * editor.removeListener("selectionchange",changeCallback);
  1560. * ```
  1561. */
  1562. removeListener: function(types, listener) {
  1563. types = utils.trim(types).split(/\s+/);
  1564. for (var i = 0, ti; (ti = types[i++]); ) {
  1565. utils.removeItem(getListener(this, ti) || [], listener);
  1566. }
  1567. },
  1568. /**
  1569. * 触发事件
  1570. * @method fireEvent
  1571. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1572. * @remind 该方法会触发addListener
  1573. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1574. * @example
  1575. * ```javascript
  1576. * editor.fireEvent("selectionchange");
  1577. * ```
  1578. */
  1579. /**
  1580. * 触发事件
  1581. * @method fireEvent
  1582. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1583. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1584. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1585. * @example
  1586. * ```javascript
  1587. *
  1588. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1589. *
  1590. * console.log( arg1 + " " + arg2 );
  1591. *
  1592. * } );
  1593. *
  1594. * //触发selectionchange事件, 会执行上面的事件监听器
  1595. * //output: Hello World
  1596. * editor.fireEvent("selectionchange", "Hello", "World");
  1597. * ```
  1598. */
  1599. fireEvent: function() {
  1600. var types = arguments[0];
  1601. types = utils.trim(types).split(" ");
  1602. for (var i = 0, ti; (ti = types[i++]); ) {
  1603. var listeners = getListener(this, ti),
  1604. r,
  1605. t,
  1606. k;
  1607. if (listeners) {
  1608. k = listeners.length;
  1609. while (k--) {
  1610. if (!listeners[k]) continue;
  1611. t = listeners[k].apply(this, arguments);
  1612. if (t === true) {
  1613. return t;
  1614. }
  1615. if (t !== undefined) {
  1616. r = t;
  1617. }
  1618. }
  1619. }
  1620. if ((t = this["on" + ti.toLowerCase()])) {
  1621. r = t.apply(this, arguments);
  1622. }
  1623. }
  1624. return r;
  1625. }
  1626. };
  1627. /**
  1628. * 获得对象所拥有监听类型的所有监听器
  1629. * @unfile
  1630. * @module UE
  1631. * @since 1.2.6.1
  1632. * @method getListener
  1633. * @public
  1634. * @param { Object } obj 查询监听器的对象
  1635. * @param { String } type 事件类型
  1636. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1637. * @return { Array } 监听器数组
  1638. */
  1639. function getListener(obj, type, force) {
  1640. var allListeners;
  1641. type = type.toLowerCase();
  1642. return (
  1643. (allListeners =
  1644. obj.__allListeners || (force && (obj.__allListeners = {}))) &&
  1645. (allListeners[type] || (force && (allListeners[type] = [])))
  1646. );
  1647. }
  1648. // core/dtd.js
  1649. ///import editor.js
  1650. ///import core/dom/dom.js
  1651. ///import core/utils.js
  1652. /**
  1653. * dtd html语义化的体现类
  1654. * @constructor
  1655. * @namespace dtd
  1656. */
  1657. var dtd = (dom.dtd = (function() {
  1658. function _(s) {
  1659. for (var k in s) {
  1660. s[k.toUpperCase()] = s[k];
  1661. }
  1662. return s;
  1663. }
  1664. var X = utils.extend2;
  1665. var A = _({ isindex: 1, fieldset: 1 }),
  1666. B = _({ input: 1, button: 1, select: 1, textarea: 1, label: 1 }),
  1667. C = X(_({ a: 1 }), B),
  1668. D = X({ iframe: 1 }, C),
  1669. E = _({
  1670. hr: 1,
  1671. ul: 1,
  1672. menu: 1,
  1673. div: 1,
  1674. blockquote: 1,
  1675. noscript: 1,
  1676. table: 1,
  1677. center: 1,
  1678. address: 1,
  1679. dir: 1,
  1680. pre: 1,
  1681. h5: 1,
  1682. dl: 1,
  1683. h4: 1,
  1684. noframes: 1,
  1685. h6: 1,
  1686. ol: 1,
  1687. h1: 1,
  1688. h3: 1,
  1689. h2: 1
  1690. }),
  1691. F = _({ ins: 1, del: 1, script: 1, style: 1 }),
  1692. G = X(
  1693. _({
  1694. mark: 1,
  1695. b: 1,
  1696. acronym: 1,
  1697. bdo: 1,
  1698. var: 1,
  1699. "#": 1,
  1700. abbr: 1,
  1701. code: 1,
  1702. br: 1,
  1703. i: 1,
  1704. cite: 1,
  1705. kbd: 1,
  1706. u: 1,
  1707. strike: 1,
  1708. s: 1,
  1709. tt: 1,
  1710. strong: 1,
  1711. q: 1,
  1712. samp: 1,
  1713. em: 1,
  1714. dfn: 1,
  1715. span: 1
  1716. }),
  1717. F
  1718. ),
  1719. H = X(
  1720. _({
  1721. sub: 1,
  1722. img: 1,
  1723. embed: 1,
  1724. object: 1,
  1725. sup: 1,
  1726. basefont: 1,
  1727. map: 1,
  1728. applet: 1,
  1729. font: 1,
  1730. big: 1,
  1731. small: 1
  1732. }),
  1733. G
  1734. ),
  1735. I = X(_({ p: 1 }), H),
  1736. J = X(_({ iframe: 1 }), H, B),
  1737. K = _({
  1738. img: 1,
  1739. embed: 1,
  1740. noscript: 1,
  1741. br: 1,
  1742. kbd: 1,
  1743. center: 1,
  1744. button: 1,
  1745. basefont: 1,
  1746. h5: 1,
  1747. h4: 1,
  1748. samp: 1,
  1749. h6: 1,
  1750. ol: 1,
  1751. h1: 1,
  1752. h3: 1,
  1753. h2: 1,
  1754. form: 1,
  1755. font: 1,
  1756. "#": 1,
  1757. select: 1,
  1758. menu: 1,
  1759. ins: 1,
  1760. abbr: 1,
  1761. label: 1,
  1762. code: 1,
  1763. table: 1,
  1764. script: 1,
  1765. cite: 1,
  1766. input: 1,
  1767. iframe: 1,
  1768. strong: 1,
  1769. textarea: 1,
  1770. noframes: 1,
  1771. big: 1,
  1772. small: 1,
  1773. span: 1,
  1774. hr: 1,
  1775. sub: 1,
  1776. bdo: 1,
  1777. var: 1,
  1778. div: 1,
  1779. object: 1,
  1780. sup: 1,
  1781. strike: 1,
  1782. dir: 1,
  1783. map: 1,
  1784. dl: 1,
  1785. applet: 1,
  1786. del: 1,
  1787. isindex: 1,
  1788. fieldset: 1,
  1789. ul: 1,
  1790. b: 1,
  1791. acronym: 1,
  1792. a: 1,
  1793. blockquote: 1,
  1794. i: 1,
  1795. u: 1,
  1796. s: 1,
  1797. tt: 1,
  1798. address: 1,
  1799. q: 1,
  1800. pre: 1,
  1801. p: 1,
  1802. em: 1,
  1803. dfn: 1
  1804. }),
  1805. L = X(_({ a: 0 }), J), //a不能被切开,所以把他
  1806. M = _({ tr: 1 }),
  1807. N = _({ "#": 1 }),
  1808. O = X(_({ param: 1 }), K),
  1809. P = X(_({ form: 1 }), A, D, E, I),
  1810. Q = _({ li: 1, ol: 1, ul: 1 }),
  1811. R = _({ style: 1, script: 1 }),
  1812. S = _({ base: 1, link: 1, meta: 1, title: 1 }),
  1813. T = X(S, R),
  1814. U = _({ head: 1, body: 1 }),
  1815. V = _({ html: 1 });
  1816. var block = _({
  1817. address: 1,
  1818. blockquote: 1,
  1819. center: 1,
  1820. dir: 1,
  1821. div: 1,
  1822. dl: 1,
  1823. fieldset: 1,
  1824. form: 1,
  1825. h1: 1,
  1826. h2: 1,
  1827. h3: 1,
  1828. h4: 1,
  1829. h5: 1,
  1830. h6: 1,
  1831. hr: 1,
  1832. isindex: 1,
  1833. menu: 1,
  1834. noframes: 1,
  1835. ol: 1,
  1836. p: 1,
  1837. pre: 1,
  1838. table: 1,
  1839. ul: 1
  1840. }),
  1841. empty = _({
  1842. area: 1,
  1843. base: 1,
  1844. basefont: 1,
  1845. br: 1,
  1846. col: 1,
  1847. command: 1,
  1848. dialog: 1,
  1849. embed: 1,
  1850. hr: 1,
  1851. img: 1,
  1852. input: 1,
  1853. isindex: 1,
  1854. keygen: 1,
  1855. link: 1,
  1856. meta: 1,
  1857. param: 1,
  1858. source: 1,
  1859. track: 1,
  1860. wbr: 1
  1861. });
  1862. return _({
  1863. // $ 表示自定的属性
  1864. // body外的元素列表.
  1865. $nonBodyContent: X(V, U, S),
  1866. //块结构元素列表
  1867. $block: block,
  1868. //内联元素列表
  1869. $inline: L,
  1870. $inlineWithA: X(_({ a: 1 }), L),
  1871. $body: X(_({ script: 1, style: 1 }), block),
  1872. $cdata: _({ script: 1, style: 1 }),
  1873. //自闭和元素
  1874. $empty: empty,
  1875. //不是自闭合,但不能让range选中里边
  1876. $nonChild: _({ iframe: 1, textarea: 1 }),
  1877. //列表元素列表
  1878. $listItem: _({ dd: 1, dt: 1, li: 1 }),
  1879. //列表根元素列表
  1880. $list: _({ ul: 1, ol: 1, dl: 1 }),
  1881. //不能认为是空的元素
  1882. $isNotEmpty: _({
  1883. table: 1,
  1884. ul: 1,
  1885. ol: 1,
  1886. dl: 1,
  1887. iframe: 1,
  1888. area: 1,
  1889. base: 1,
  1890. col: 1,
  1891. hr: 1,
  1892. img: 1,
  1893. embed: 1,
  1894. input: 1,
  1895. textarea: 1,
  1896. link: 1,
  1897. meta: 1,
  1898. param: 1,
  1899. h1: 1,
  1900. h2: 1,
  1901. h3: 1,
  1902. h4: 1,
  1903. h5: 1,
  1904. h6: 1
  1905. }),
  1906. //如果没有子节点就可以删除的元素列表,像span,a
  1907. $removeEmpty: _({
  1908. a: 1,
  1909. abbr: 1,
  1910. acronym: 1,
  1911. address: 1,
  1912. b: 1,
  1913. bdo: 1,
  1914. big: 1,
  1915. cite: 1,
  1916. code: 1,
  1917. del: 1,
  1918. dfn: 1,
  1919. em: 1,
  1920. font: 1,
  1921. i: 1,
  1922. ins: 1,
  1923. label: 1,
  1924. kbd: 1,
  1925. q: 1,
  1926. s: 1,
  1927. samp: 1,
  1928. small: 1,
  1929. span: 1,
  1930. strike: 1,
  1931. strong: 1,
  1932. sub: 1,
  1933. sup: 1,
  1934. tt: 1,
  1935. u: 1,
  1936. var: 1
  1937. }),
  1938. $removeEmptyBlock: _({ p: 1, div: 1 }),
  1939. //在table元素里的元素列表
  1940. $tableContent: _({
  1941. caption: 1,
  1942. col: 1,
  1943. colgroup: 1,
  1944. tbody: 1,
  1945. td: 1,
  1946. tfoot: 1,
  1947. th: 1,
  1948. thead: 1,
  1949. tr: 1,
  1950. table: 1
  1951. }),
  1952. //不转换的标签
  1953. $notTransContent: _({ pre: 1, script: 1, style: 1, textarea: 1 }),
  1954. html: U,
  1955. head: T,
  1956. style: N,
  1957. script: N,
  1958. body: P,
  1959. base: {},
  1960. link: {},
  1961. meta: {},
  1962. title: N,
  1963. col: {},
  1964. tr: _({ td: 1, th: 1 }),
  1965. img: {},
  1966. embed: {},
  1967. colgroup: _({ thead: 1, col: 1, tbody: 1, tr: 1, tfoot: 1 }),
  1968. noscript: P,
  1969. td: P,
  1970. br: {},
  1971. th: P,
  1972. center: P,
  1973. kbd: L,
  1974. button: X(I, E),
  1975. basefont: {},
  1976. h5: L,
  1977. h4: L,
  1978. samp: L,
  1979. h6: L,
  1980. ol: Q,
  1981. h1: L,
  1982. h3: L,
  1983. option: N,
  1984. h2: L,
  1985. form: X(A, D, E, I),
  1986. select: _({ optgroup: 1, option: 1 }),
  1987. font: L,
  1988. ins: L,
  1989. menu: Q,
  1990. abbr: L,
  1991. label: L,
  1992. table: _({
  1993. thead: 1,
  1994. col: 1,
  1995. tbody: 1,
  1996. tr: 1,
  1997. colgroup: 1,
  1998. caption: 1,
  1999. tfoot: 1
  2000. }),
  2001. code: L,
  2002. tfoot: M,
  2003. cite: L,
  2004. li: P,
  2005. input: {},
  2006. iframe: P,
  2007. strong: L,
  2008. textarea: N,
  2009. noframes: P,
  2010. big: L,
  2011. small: L,
  2012. //trace:
  2013. span: _({
  2014. "#": 1,
  2015. br: 1,
  2016. b: 1,
  2017. strong: 1,
  2018. u: 1,
  2019. i: 1,
  2020. em: 1,
  2021. sub: 1,
  2022. sup: 1,
  2023. strike: 1,
  2024. span: 1
  2025. }),
  2026. hr: L,
  2027. dt: L,
  2028. sub: L,
  2029. optgroup: _({ option: 1 }),
  2030. param: {},
  2031. bdo: L,
  2032. var: L,
  2033. div: P,
  2034. object: O,
  2035. sup: L,
  2036. dd: P,
  2037. strike: L,
  2038. area: {},
  2039. dir: Q,
  2040. map: X(_({ area: 1, form: 1, p: 1 }), A, F, E),
  2041. applet: O,
  2042. dl: _({ dt: 1, dd: 1 }),
  2043. del: L,
  2044. isindex: {},
  2045. fieldset: X(_({ legend: 1 }), K),
  2046. thead: M,
  2047. ul: Q,
  2048. acronym: L,
  2049. b: L,
  2050. a: X(_({ a: 1 }), J),
  2051. blockquote: X(_({ td: 1, tr: 1, tbody: 1, li: 1 }), P),
  2052. caption: L,
  2053. i: L,
  2054. u: L,
  2055. tbody: M,
  2056. s: L,
  2057. address: X(D, I),
  2058. tt: L,
  2059. legend: L,
  2060. q: L,
  2061. pre: X(G, C),
  2062. p: X(_({ a: 1 }), L),
  2063. em: L,
  2064. dfn: L,
  2065. mark: L
  2066. });
  2067. })());
  2068. // core/domUtils.js
  2069. /**
  2070. * Dom操作工具包
  2071. * @file
  2072. * @module UE.dom.domUtils
  2073. * @since 1.2.6.1
  2074. */
  2075. /**
  2076. * Dom操作工具包
  2077. * @unfile
  2078. * @module UE.dom.domUtils
  2079. */
  2080. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2081. var tmpNode = startFromChild && node[start],
  2082. parent;
  2083. !tmpNode && (tmpNode = node[ltr]);
  2084. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2085. if (parent.tagName == "BODY" || (guard && !guard(parent))) {
  2086. return null;
  2087. }
  2088. tmpNode = parent[ltr];
  2089. }
  2090. if (tmpNode && fn && !fn(tmpNode)) {
  2091. return getDomNode(tmpNode, start, ltr, false, fn);
  2092. }
  2093. return tmpNode;
  2094. }
  2095. var attrFix = ie && browser.version < 9
  2096. ? {
  2097. tabindex: "tabIndex",
  2098. readonly: "readOnly",
  2099. for: "htmlFor",
  2100. class: "className",
  2101. maxlength: "maxLength",
  2102. cellspacing: "cellSpacing",
  2103. cellpadding: "cellPadding",
  2104. rowspan: "rowSpan",
  2105. colspan: "colSpan",
  2106. usemap: "useMap",
  2107. frameborder: "frameBorder"
  2108. }
  2109. : {
  2110. tabindex: "tabIndex",
  2111. readonly: "readOnly"
  2112. },
  2113. styleBlock = utils.listToMap([
  2114. "-webkit-box",
  2115. "-moz-box",
  2116. "block",
  2117. "list-item",
  2118. "table",
  2119. "table-row-group",
  2120. "table-header-group",
  2121. "table-footer-group",
  2122. "table-row",
  2123. "table-column-group",
  2124. "table-column",
  2125. "table-cell",
  2126. "table-caption"
  2127. ]);
  2128. var domUtils = (dom.domUtils = {
  2129. //节点常量
  2130. NODE_ELEMENT: 1,
  2131. NODE_DOCUMENT: 9,
  2132. NODE_TEXT: 3,
  2133. NODE_COMMENT: 8,
  2134. NODE_DOCUMENT_FRAGMENT: 11,
  2135. //位置关系
  2136. POSITION_IDENTICAL: 0,
  2137. POSITION_DISCONNECTED: 1,
  2138. POSITION_FOLLOWING: 2,
  2139. POSITION_PRECEDING: 4,
  2140. POSITION_IS_CONTAINED: 8,
  2141. POSITION_CONTAINS: 16,
  2142. //ie6使用其他的会有一段空白出现
  2143. fillChar: ie && browser.version == "6" ? "\ufeff" : "\u200B",
  2144. //-------------------------Node部分--------------------------------
  2145. keys: {
  2146. /*Backspace*/ 8: 1,
  2147. /*Delete*/ 46: 1,
  2148. /*Shift*/ 16: 1,
  2149. /*Ctrl*/ 17: 1,
  2150. /*Alt*/ 18: 1,
  2151. 37: 1,
  2152. 38: 1,
  2153. 39: 1,
  2154. 40: 1,
  2155. 13: 1 /*enter*/
  2156. },
  2157. /**
  2158. * 获取节点A相对于节点B的位置关系
  2159. * @method getPosition
  2160. * @param { Node } nodeA 需要查询位置关系的节点A
  2161. * @param { Node } nodeB 需要查询位置关系的节点B
  2162. * @return { Number } 节点A与节点B的关系
  2163. * @example
  2164. * ```javascript
  2165. * //output: 20
  2166. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2167. *
  2168. * switch ( position ) {
  2169. *
  2170. * //0
  2171. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2172. * console.log('元素相同');
  2173. * break;
  2174. * //1
  2175. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2176. * console.log('两个节点在不同的文档中');
  2177. * break;
  2178. * //2
  2179. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2180. * console.log('节点A在节点B之后');
  2181. * break;
  2182. * //4
  2183. * case UE.dom.domUtils.POSITION_PRECEDING;
  2184. * console.log('节点A在节点B之前');
  2185. * break;
  2186. * //8
  2187. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2188. * console.log('节点A被节点B包含');
  2189. * break;
  2190. * case 10:
  2191. * console.log('节点A被节点B包含且节点A在节点B之后');
  2192. * break;
  2193. * //16
  2194. * case UE.dom.domUtils.POSITION_CONTAINS:
  2195. * console.log('节点A包含节点B');
  2196. * break;
  2197. * case 20:
  2198. * console.log('节点A包含节点B且节点A在节点B之前');
  2199. * break;
  2200. *
  2201. * }
  2202. * ```
  2203. */
  2204. getPosition: function(nodeA, nodeB) {
  2205. // 如果两个节点是同一个节点
  2206. if (nodeA === nodeB) {
  2207. // domUtils.POSITION_IDENTICAL
  2208. return 0;
  2209. }
  2210. var node,
  2211. parentsA = [nodeA],
  2212. parentsB = [nodeB];
  2213. node = nodeA;
  2214. while ((node = node.parentNode)) {
  2215. // 如果nodeB是nodeA的祖先节点
  2216. if (node === nodeB) {
  2217. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2218. return 10;
  2219. }
  2220. parentsA.push(node);
  2221. }
  2222. node = nodeB;
  2223. while ((node = node.parentNode)) {
  2224. // 如果nodeA是nodeB的祖先节点
  2225. if (node === nodeA) {
  2226. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2227. return 20;
  2228. }
  2229. parentsB.push(node);
  2230. }
  2231. parentsA.reverse();
  2232. parentsB.reverse();
  2233. if (parentsA[0] !== parentsB[0]) {
  2234. // domUtils.POSITION_DISCONNECTED
  2235. return 1;
  2236. }
  2237. var i = -1;
  2238. while ((i++, parentsA[i] === parentsB[i])) {}
  2239. nodeA = parentsA[i];
  2240. nodeB = parentsB[i];
  2241. while ((nodeA = nodeA.nextSibling)) {
  2242. if (nodeA === nodeB) {
  2243. // domUtils.POSITION_PRECEDING
  2244. return 4;
  2245. }
  2246. }
  2247. // domUtils.POSITION_FOLLOWING
  2248. return 2;
  2249. },
  2250. /**
  2251. * 检测节点node在父节点中的索引位置
  2252. * @method getNodeIndex
  2253. * @param { Node } node 需要检测的节点对象
  2254. * @return { Number } 该节点在父节点中的位置
  2255. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2256. */
  2257. /**
  2258. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2259. * @method getNodeIndex
  2260. * @param { Node } node 需要检测的节点对象
  2261. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2262. * @return { Number } 该节点在父节点中的位置
  2263. * @example
  2264. * ```javascript
  2265. *
  2266. * var node = document.createElement("div");
  2267. *
  2268. * node.appendChild( document.createTextNode( "hello" ) );
  2269. * node.appendChild( document.createTextNode( "world" ) );
  2270. * node.appendChild( node = document.createElement( "div" ) );
  2271. *
  2272. * //output: 2
  2273. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2274. *
  2275. * //output: 1
  2276. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2277. *
  2278. * ```
  2279. */
  2280. getNodeIndex: function(node, ignoreTextNode) {
  2281. var preNode = node,
  2282. i = 0;
  2283. while ((preNode = preNode.previousSibling)) {
  2284. if (ignoreTextNode && preNode.nodeType == 3) {
  2285. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2286. i++;
  2287. }
  2288. continue;
  2289. }
  2290. i++;
  2291. }
  2292. return i;
  2293. },
  2294. /**
  2295. * 检测节点node是否在给定的document对象上
  2296. * @method inDoc
  2297. * @param { Node } node 需要检测的节点对象
  2298. * @param { DomDocument } doc 需要检测的document对象
  2299. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2300. * @example
  2301. * ```javascript
  2302. *
  2303. * var node = document.createElement("div");
  2304. *
  2305. * //output: false
  2306. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2307. *
  2308. * document.body.appendChild( node );
  2309. *
  2310. * //output: true
  2311. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2312. *
  2313. * ```
  2314. */
  2315. inDoc: function(node, doc) {
  2316. return domUtils.getPosition(node, doc) == 10;
  2317. },
  2318. /**
  2319. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2320. * 查找的起点是给定node节点的父节点。
  2321. * @method findParent
  2322. * @param { Node } node 需要查找的节点
  2323. * @param { Function } filterFn 自定义的过滤方法。
  2324. * @warning 查找的终点是到body节点为止
  2325. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2326. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2327. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2328. * @example
  2329. * ```javascript
  2330. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2331. *
  2332. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2333. * return node.tagName === "HTML";
  2334. *
  2335. * } );
  2336. *
  2337. * //output: true
  2338. * console.log( filterNode === null );
  2339. * ```
  2340. */
  2341. /**
  2342. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2343. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2344. * @method findParent
  2345. * @param { Node } node 需要查找的节点
  2346. * @param { Function } filterFn 自定义的过滤方法。
  2347. * @param { Boolean } includeSelf 查找过程是否包含自身
  2348. * @warning 查找的终点是到body节点为止
  2349. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2350. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2351. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2352. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2353. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2354. * @example
  2355. * ```html
  2356. * <body>
  2357. *
  2358. * <div id="test">
  2359. * </div>
  2360. *
  2361. * <script type="text/javascript">
  2362. *
  2363. * //output: DIV, BODY
  2364. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2365. *
  2366. * console.log( node.tagName );
  2367. * return false;
  2368. *
  2369. * }, true );
  2370. *
  2371. * </script>
  2372. * </body>
  2373. * ```
  2374. */
  2375. findParent: function(node, filterFn, includeSelf) {
  2376. if (node && !domUtils.isBody(node)) {
  2377. node = includeSelf ? node : node.parentNode;
  2378. while (node) {
  2379. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2380. return filterFn && !filterFn(node) && domUtils.isBody(node)
  2381. ? null
  2382. : node;
  2383. }
  2384. node = node.parentNode;
  2385. }
  2386. }
  2387. return null;
  2388. },
  2389. /**
  2390. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2391. * @method findParentByTagName
  2392. * @param { Node } node 需要查找的节点对象
  2393. * @param { Array } tagNames 需要查找的父节点的名称数组
  2394. * @warning 查找的终点是到body节点为止
  2395. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2396. * @example
  2397. * ```javascript
  2398. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2399. * //output: BODY
  2400. * console.log( node.tagName );
  2401. * ```
  2402. */
  2403. /**
  2404. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2405. * 否则, 起点是node的父节点。
  2406. * @method findParentByTagName
  2407. * @param { Node } node 需要查找的节点对象
  2408. * @param { Array } tagNames 需要查找的父节点的名称数组
  2409. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2410. * @warning 查找的终点是到body节点为止
  2411. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2412. * @example
  2413. * ```javascript
  2414. * var queryTarget = document.getElementsByTagName("div")[0];
  2415. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2416. * //output: true
  2417. * console.log( queryTarget === node );
  2418. * ```
  2419. */
  2420. findParentByTagName: function(node, tagNames, includeSelf, excludeFn) {
  2421. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  2422. return domUtils.findParent(
  2423. node,
  2424. function(node) {
  2425. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  2426. },
  2427. includeSelf
  2428. );
  2429. },
  2430. /**
  2431. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2432. * @method findParents
  2433. * @param { Node } node 需要查找的节点对象
  2434. * @return { Array } 给定节点的祖先节点数组
  2435. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2436. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2437. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2438. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2439. */
  2440. /**
  2441. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2442. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2443. * @method findParents
  2444. * @param { Node } node 需要查找的节点对象
  2445. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2446. * @return { Array } 给定节点的祖先节点数组
  2447. */
  2448. findParents: function(node, includeSelf, filterFn, closerFirst) {
  2449. var parents = includeSelf && ((filterFn && filterFn(node)) || !filterFn)
  2450. ? [node]
  2451. : [];
  2452. while ((node = domUtils.findParent(node, filterFn))) {
  2453. parents.push(node);
  2454. }
  2455. return closerFirst ? parents : parents.reverse();
  2456. },
  2457. /**
  2458. * 在节点node后面插入新节点newNode
  2459. * @method insertAfter
  2460. * @param { Node } node 目标节点
  2461. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2462. * @return { Node } 新插入的节点
  2463. */
  2464. insertAfter: function(node, newNode) {
  2465. return node.nextSibling
  2466. ? node.parentNode.insertBefore(newNode, node.nextSibling)
  2467. : node.parentNode.appendChild(newNode);
  2468. },
  2469. /**
  2470. * 删除节点node及其下属的所有节点
  2471. * @method remove
  2472. * @param { Node } node 需要删除的节点对象
  2473. * @return { Node } 返回刚删除的节点对象
  2474. * @example
  2475. * ```html
  2476. * <div id="test">
  2477. * <div id="child">你好</div>
  2478. * </div>
  2479. * <script>
  2480. * UE.dom.domUtils.remove( document.body, false );
  2481. * //output: false
  2482. * console.log( document.getElementById( "child" ) !== null );
  2483. * </script>
  2484. * ```
  2485. */
  2486. /**
  2487. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2488. * @method remove
  2489. * @param { Node } node 需要删除的节点对象
  2490. * @param { Boolean } keepChildren 是否需要保留子节点
  2491. * @return { Node } 返回刚删除的节点对象
  2492. * @example
  2493. * ```html
  2494. * <div id="test">
  2495. * <div id="child">你好</div>
  2496. * </div>
  2497. * <script>
  2498. * UE.dom.domUtils.remove( document.body, true );
  2499. * //output: true
  2500. * console.log( document.getElementById( "child" ) !== null );
  2501. * </script>
  2502. * ```
  2503. */
  2504. remove: function(node, keepChildren) {
  2505. var parent = node.parentNode,
  2506. child;
  2507. if (parent) {
  2508. if (keepChildren && node.hasChildNodes()) {
  2509. while ((child = node.firstChild)) {
  2510. parent.insertBefore(child, node);
  2511. }
  2512. }
  2513. parent.removeChild(node);
  2514. }
  2515. return node;
  2516. },
  2517. /**
  2518. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2519. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2520. * @method getNextDomNode
  2521. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2522. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2523. * @example
  2524. * ```html
  2525. * <body>
  2526. * <div id="test">
  2527. * <span></span>
  2528. * </div>
  2529. * <i>xxx</i>
  2530. * </body>
  2531. * <script>
  2532. *
  2533. * //output: i节点
  2534. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2535. *
  2536. * </script>
  2537. * ```
  2538. * @example
  2539. * ```html
  2540. * <body>
  2541. * <div>
  2542. * <span></span>
  2543. * <i id="test">xxx</i>
  2544. * </div>
  2545. * <b>xxx</b>
  2546. * </body>
  2547. * <script>
  2548. *
  2549. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2550. * //output: b节点
  2551. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2552. *
  2553. * </script>
  2554. * ```
  2555. */
  2556. /**
  2557. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2558. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2559. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2560. * @method getNextDomNode
  2561. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2562. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2563. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2564. * @see UE.dom.domUtils.getNextDomNode(Node)
  2565. */
  2566. getNextDomNode: function(node, startFromChild, filterFn, guard) {
  2567. return getDomNode(
  2568. node,
  2569. "firstChild",
  2570. "nextSibling",
  2571. startFromChild,
  2572. filterFn,
  2573. guard
  2574. );
  2575. },
  2576. getPreDomNode: function(node, startFromChild, filterFn, guard) {
  2577. return getDomNode(
  2578. node,
  2579. "lastChild",
  2580. "previousSibling",
  2581. startFromChild,
  2582. filterFn,
  2583. guard
  2584. );
  2585. },
  2586. /**
  2587. * 检测节点node是否属是UEditor定义的bookmark节点
  2588. * @method isBookmarkNode
  2589. * @private
  2590. * @param { Node } node 需要检测的节点对象
  2591. * @return { Boolean } 是否是bookmark节点
  2592. * @example
  2593. * ```html
  2594. * <span id="_baidu_bookmark_1"></span>
  2595. * <script>
  2596. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2597. * //output: true
  2598. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2599. * </script>
  2600. * ```
  2601. */
  2602. isBookmarkNode: function(node) {
  2603. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  2604. },
  2605. /**
  2606. * 获取节点node所属的window对象
  2607. * @method getWindow
  2608. * @param { Node } node 节点对象
  2609. * @return { Window } 当前节点所属的window对象
  2610. * @example
  2611. * ```javascript
  2612. * //output: true
  2613. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2614. * ```
  2615. */
  2616. getWindow: function(node) {
  2617. var doc = node.ownerDocument || node;
  2618. return doc.defaultView || doc.parentWindow;
  2619. },
  2620. /**
  2621. * 获取离nodeA与nodeB最近的公共的祖先节点
  2622. * @method getCommonAncestor
  2623. * @param { Node } nodeA 第一个节点
  2624. * @param { Node } nodeB 第二个节点
  2625. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2626. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2627. * @example
  2628. * ```javascript
  2629. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2630. * //output: true
  2631. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2632. * ```
  2633. */
  2634. getCommonAncestor: function(nodeA, nodeB) {
  2635. if (nodeA === nodeB) return nodeA;
  2636. var parentsA = [nodeA],
  2637. parentsB = [nodeB],
  2638. parent = nodeA,
  2639. i = -1;
  2640. while ((parent = parent.parentNode)) {
  2641. if (parent === nodeB) {
  2642. return parent;
  2643. }
  2644. parentsA.push(parent);
  2645. }
  2646. parent = nodeB;
  2647. while ((parent = parent.parentNode)) {
  2648. if (parent === nodeA) return parent;
  2649. parentsB.push(parent);
  2650. }
  2651. parentsA.reverse();
  2652. parentsB.reverse();
  2653. while ((i++, parentsA[i] === parentsB[i])) {}
  2654. return i == 0 ? null : parentsA[i - 1];
  2655. },
  2656. /**
  2657. * 清除node节点左右连续为空的兄弟inline节点
  2658. * @method clearEmptySibling
  2659. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2660. * 则这些兄弟节点将被删除
  2661. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2662. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2663. * @example
  2664. * ```html
  2665. * <body>
  2666. * <div></div>
  2667. * <span id="test"></span>
  2668. * <i></i>
  2669. * <b></b>
  2670. * <em>xxx</em>
  2671. * <span></span>
  2672. * </body>
  2673. * <script>
  2674. *
  2675. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2676. *
  2677. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2678. * console.log( document.body.innerHTML );
  2679. *
  2680. * </script>
  2681. * ```
  2682. */
  2683. /**
  2684. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2685. * 则忽略对右边兄弟节点的操作。
  2686. * @method clearEmptySibling
  2687. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2688. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2689. * 则这些兄弟节点将被删除
  2690. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2691. */
  2692. /**
  2693. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2694. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2695. * @method clearEmptySibling
  2696. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2697. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2698. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2699. * 则这些兄弟节点将被删除
  2700. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2701. */
  2702. clearEmptySibling: function(node, ignoreNext, ignorePre) {
  2703. function clear(next, dir) {
  2704. var tmpNode;
  2705. while (
  2706. next &&
  2707. !domUtils.isBookmarkNode(next) &&
  2708. (domUtils.isEmptyInlineElement(next) ||
  2709. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2710. !new RegExp("[^\t\n\r" + domUtils.fillChar + "]").test(
  2711. next.nodeValue
  2712. ))
  2713. ) {
  2714. tmpNode = next[dir];
  2715. domUtils.remove(next);
  2716. next = tmpNode;
  2717. }
  2718. }
  2719. !ignoreNext && clear(node.nextSibling, "nextSibling");
  2720. !ignorePre && clear(node.previousSibling, "previousSibling");
  2721. },
  2722. /**
  2723. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2724. * @method split
  2725. * @param { Node } textNode 需要拆分的文本节点对象
  2726. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2727. * @return { Node } 拆分后形成的新节点
  2728. * @example
  2729. * ```html
  2730. * <div id="test">abcdef</div>
  2731. * <script>
  2732. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2733. * //output: def
  2734. * console.log( newNode.nodeValue );
  2735. * </script>
  2736. * ```
  2737. */
  2738. split: function(node, offset) {
  2739. var doc = node.ownerDocument;
  2740. if (browser.ie && offset == node.nodeValue.length) {
  2741. var next = doc.createTextNode("");
  2742. return domUtils.insertAfter(node, next);
  2743. }
  2744. var retval = node.splitText(offset);
  2745. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2746. if (browser.ie8) {
  2747. var tmpNode = doc.createTextNode("");
  2748. domUtils.insertAfter(retval, tmpNode);
  2749. domUtils.remove(tmpNode);
  2750. }
  2751. return retval;
  2752. },
  2753. /**
  2754. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2755. * @method isWhitespace
  2756. * @param { Node } node 需要检测的节点对象
  2757. * @return { Boolean } 检测的节点是否为空
  2758. * @example
  2759. * ```html
  2760. * <div id="test">
  2761. *
  2762. * </div>
  2763. * <script>
  2764. * //output: true
  2765. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2766. * </script>
  2767. * ```
  2768. */
  2769. isWhitespace: function(node) {
  2770. return !new RegExp("[^ \t\n\r" + domUtils.fillChar + "]").test(
  2771. node.nodeValue
  2772. );
  2773. },
  2774. /**
  2775. * 获取元素element相对于viewport的位置坐标
  2776. * @method getXY
  2777. * @param { Node } element 需要计算位置的节点对象
  2778. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2779. * y代表垂直偏移距离。
  2780. *
  2781. * @example
  2782. * ```javascript
  2783. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2784. * //output: test的坐标为: 12, 24
  2785. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2786. * ```
  2787. */
  2788. getXY: function(element) {
  2789. var x = 0,
  2790. y = 0;
  2791. while (element.offsetParent) {
  2792. y += element.offsetTop;
  2793. x += element.offsetLeft;
  2794. element = element.offsetParent;
  2795. }
  2796. return { x: x, y: y };
  2797. },
  2798. /**
  2799. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2800. * @method on
  2801. * @param { Node } element 需要绑定事件的节点对象
  2802. * @param { String } type 绑定的事件类型
  2803. * @param { Function } handler 事件处理器
  2804. * @example
  2805. * ```javascript
  2806. * UE.dom.domUtils.on(document.body,"click",function(e){
  2807. * //e为事件对象,this为被点击元素对戏那个
  2808. * });
  2809. * ```
  2810. */
  2811. /**
  2812. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2813. * @method on
  2814. * @param { Node } element 需要绑定事件的节点对象
  2815. * @param { Array } type 绑定的事件类型数组
  2816. * @param { Function } handler 事件处理器
  2817. * @example
  2818. * ```javascript
  2819. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2820. * //evt为事件对象,this为被点击元素对象
  2821. * });
  2822. * ```
  2823. */
  2824. on: function(element, type, handler) {
  2825. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2826. k = types.length;
  2827. if (k)
  2828. while (k--) {
  2829. type = types[k];
  2830. if (element.addEventListener) {
  2831. element.addEventListener(type, handler, false);
  2832. } else {
  2833. if (!handler._d) {
  2834. handler._d = {
  2835. els: []
  2836. };
  2837. }
  2838. var key = type + handler.toString(),
  2839. index = utils.indexOf(handler._d.els, element);
  2840. if (!handler._d[key] || index == -1) {
  2841. if (index == -1) {
  2842. handler._d.els.push(element);
  2843. }
  2844. if (!handler._d[key]) {
  2845. handler._d[key] = function(evt) {
  2846. return handler.call(evt.srcElement, evt || window.event);
  2847. };
  2848. }
  2849. element.attachEvent("on" + type, handler._d[key]);
  2850. }
  2851. }
  2852. }
  2853. element = null;
  2854. },
  2855. /**
  2856. * 解除DOM事件绑定
  2857. * @method un
  2858. * @param { Node } element 需要解除事件绑定的节点对象
  2859. * @param { String } type 需要接触绑定的事件类型
  2860. * @param { Function } handler 对应的事件处理器
  2861. * @example
  2862. * ```javascript
  2863. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2864. * //evt为事件对象,this为被点击元素对象
  2865. * });
  2866. * ```
  2867. */
  2868. /**
  2869. * 解除DOM事件绑定
  2870. * @method un
  2871. * @param { Node } element 需要解除事件绑定的节点对象
  2872. * @param { Array } type 需要接触绑定的事件类型数组
  2873. * @param { Function } handler 对应的事件处理器
  2874. * @example
  2875. * ```javascript
  2876. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2877. * //evt为事件对象,this为被点击元素对象
  2878. * });
  2879. * ```
  2880. */
  2881. un: function(element, type, handler) {
  2882. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2883. k = types.length;
  2884. if (k)
  2885. while (k--) {
  2886. type = types[k];
  2887. if (element.removeEventListener) {
  2888. element.removeEventListener(type, handler, false);
  2889. } else {
  2890. var key = type + handler.toString();
  2891. try {
  2892. element.detachEvent(
  2893. "on" + type,
  2894. handler._d ? handler._d[key] : handler
  2895. );
  2896. } catch (e) {}
  2897. if (handler._d && handler._d[key]) {
  2898. var index = utils.indexOf(handler._d.els, element);
  2899. if (index != -1) {
  2900. handler._d.els.splice(index, 1);
  2901. }
  2902. handler._d.els.length == 0 && delete handler._d[key];
  2903. }
  2904. }
  2905. }
  2906. },
  2907. /**
  2908. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2909. * @method isSameElement
  2910. * @param { Node } nodeA 需要比较的节点
  2911. * @param { Node } nodeB 需要比较的节点
  2912. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2913. * @example
  2914. * ```html
  2915. * <span style="font-size:12px">ssss</span>
  2916. * <span style="font-size:12px">bbbbb</span>
  2917. * <span style="font-size:13px">ssss</span>
  2918. * <span style="font-size:14px">bbbbb</span>
  2919. *
  2920. * <script>
  2921. *
  2922. * var nodes = document.getElementsByTagName( "span" );
  2923. *
  2924. * //output: true
  2925. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2926. *
  2927. * //output: false
  2928. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2929. *
  2930. * </script>
  2931. * ```
  2932. */
  2933. isSameElement: function(nodeA, nodeB) {
  2934. if (nodeA.tagName != nodeB.tagName) {
  2935. return false;
  2936. }
  2937. var thisAttrs = nodeA.attributes,
  2938. otherAttrs = nodeB.attributes;
  2939. if (!ie && thisAttrs.length != otherAttrs.length) {
  2940. return false;
  2941. }
  2942. var attrA,
  2943. attrB,
  2944. al = 0,
  2945. bl = 0;
  2946. for (var i = 0; (attrA = thisAttrs[i++]); ) {
  2947. if (attrA.nodeName == "style") {
  2948. if (attrA.specified) {
  2949. al++;
  2950. }
  2951. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2952. continue;
  2953. } else {
  2954. return false;
  2955. }
  2956. }
  2957. if (ie) {
  2958. if (attrA.specified) {
  2959. al++;
  2960. attrB = otherAttrs.getNamedItem(attrA.nodeName);
  2961. } else {
  2962. continue;
  2963. }
  2964. } else {
  2965. attrB = nodeB.attributes[attrA.nodeName];
  2966. }
  2967. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2968. return false;
  2969. }
  2970. }
  2971. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2972. if (ie) {
  2973. for (i = 0; (attrB = otherAttrs[i++]); ) {
  2974. if (attrB.specified) {
  2975. bl++;
  2976. }
  2977. }
  2978. if (al != bl) {
  2979. return false;
  2980. }
  2981. }
  2982. return true;
  2983. },
  2984. /**
  2985. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  2986. * @method isSameStyle
  2987. * @param { Node } nodeA 需要比较的节点
  2988. * @param { Node } nodeB 需要比较的节点
  2989. * @return { Boolean } 两个节点是否具有相同的style属性值
  2990. * @example
  2991. * ```html
  2992. * <span style="font-size:12px">ssss</span>
  2993. * <span style="font-size:12px">bbbbb</span>
  2994. * <span style="font-size:13px">ssss</span>
  2995. * <span style="font-size:14px">bbbbb</span>
  2996. *
  2997. * <script>
  2998. *
  2999. * var nodes = document.getElementsByTagName( "span" );
  3000. *
  3001. * //output: true
  3002. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  3003. *
  3004. * //output: false
  3005. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  3006. *
  3007. * </script>
  3008. * ```
  3009. */
  3010. isSameStyle: function(nodeA, nodeB) {
  3011. var styleA = nodeA.style.cssText
  3012. .replace(/( ?; ?)/g, ";")
  3013. .replace(/( ?: ?)/g, ":"),
  3014. styleB = nodeB.style.cssText
  3015. .replace(/( ?; ?)/g, ";")
  3016. .replace(/( ?: ?)/g, ":");
  3017. if (browser.opera) {
  3018. styleA = nodeA.style;
  3019. styleB = nodeB.style;
  3020. if (styleA.length != styleB.length) return false;
  3021. for (var p in styleA) {
  3022. if (/^(\d+|csstext)$/i.test(p)) {
  3023. continue;
  3024. }
  3025. if (styleA[p] != styleB[p]) {
  3026. return false;
  3027. }
  3028. }
  3029. return true;
  3030. }
  3031. if (!styleA || !styleB) {
  3032. return styleA == styleB;
  3033. }
  3034. styleA = styleA.split(";");
  3035. styleB = styleB.split(";");
  3036. if (styleA.length != styleB.length) {
  3037. return false;
  3038. }
  3039. for (var i = 0, ci; (ci = styleA[i++]); ) {
  3040. if (utils.indexOf(styleB, ci) == -1) {
  3041. return false;
  3042. }
  3043. }
  3044. return true;
  3045. },
  3046. /**
  3047. * 检查节点node是否为block元素
  3048. * @method isBlockElm
  3049. * @param { Node } node 需要检测的节点对象
  3050. * @return { Boolean } 是否是block元素节点
  3051. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3052. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3053. * @example
  3054. * ```html
  3055. * <span id="test1" style="display: block"></span>
  3056. * <span id="test2"></span>
  3057. * <div id="test3" style="display: inline"></div>
  3058. *
  3059. * <script>
  3060. *
  3061. * //output: true
  3062. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3063. *
  3064. * //output: false
  3065. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3066. *
  3067. * //output: true
  3068. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3069. *
  3070. * </script>
  3071. * ```
  3072. */
  3073. isBlockElm: function(node) {
  3074. return (
  3075. node.nodeType == 1 &&
  3076. (dtd.$block[node.tagName] ||
  3077. styleBlock[domUtils.getComputedStyle(node, "display")]) &&
  3078. !dtd.$nonChild[node.tagName]
  3079. );
  3080. },
  3081. /**
  3082. * 检测node节点是否为body节点
  3083. * @method isBody
  3084. * @param { Element } node 需要检测的dom元素
  3085. * @return { Boolean } 给定的元素是否是body元素
  3086. * @example
  3087. * ```javascript
  3088. * //output: true
  3089. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3090. * ```
  3091. */
  3092. isBody: function(node) {
  3093. return node && node.nodeType == 1 && node.tagName.toLowerCase() == "body";
  3094. },
  3095. /**
  3096. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3097. * 拆分形成的两个节点之间是node节点
  3098. * @method breakParent
  3099. * @param { Node } node 作为分界的节点对象
  3100. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3101. * @return { Node } 给定的node分界节点
  3102. * @example
  3103. * ```javascript
  3104. *
  3105. * var node = document.createElement("span"),
  3106. * wrapNode = document.createElement( "div" ),
  3107. * parent = document.createElement("p");
  3108. *
  3109. * parent.appendChild( node );
  3110. * wrapNode.appendChild( parent );
  3111. *
  3112. * //拆分前
  3113. * //output: <p><span></span></p>
  3114. * console.log( wrapNode.innerHTML );
  3115. *
  3116. *
  3117. * UE.dom.domUtils.breakParent( node, parent );
  3118. * //拆分后
  3119. * //output: <p></p><span></span><p></p>
  3120. * console.log( wrapNode.innerHTML );
  3121. *
  3122. * ```
  3123. */
  3124. breakParent: function(node, parent) {
  3125. var tmpNode,
  3126. parentClone = node,
  3127. clone = node,
  3128. leftNodes,
  3129. rightNodes;
  3130. do {
  3131. parentClone = parentClone.parentNode;
  3132. if (leftNodes) {
  3133. tmpNode = parentClone.cloneNode(false);
  3134. tmpNode.appendChild(leftNodes);
  3135. leftNodes = tmpNode;
  3136. tmpNode = parentClone.cloneNode(false);
  3137. tmpNode.appendChild(rightNodes);
  3138. rightNodes = tmpNode;
  3139. } else {
  3140. leftNodes = parentClone.cloneNode(false);
  3141. rightNodes = leftNodes.cloneNode(false);
  3142. }
  3143. while ((tmpNode = clone.previousSibling)) {
  3144. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  3145. }
  3146. while ((tmpNode = clone.nextSibling)) {
  3147. rightNodes.appendChild(tmpNode);
  3148. }
  3149. clone = parentClone;
  3150. } while (parent !== parentClone);
  3151. tmpNode = parent.parentNode;
  3152. tmpNode.insertBefore(leftNodes, parent);
  3153. tmpNode.insertBefore(rightNodes, parent);
  3154. tmpNode.insertBefore(node, rightNodes);
  3155. domUtils.remove(parent);
  3156. return node;
  3157. },
  3158. /**
  3159. * 检查节点node是否是空inline节点
  3160. * @method isEmptyInlineElement
  3161. * @param { Node } node 需要检测的节点对象
  3162. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3163. * @example
  3164. * ```html
  3165. * <b><i></i></b> => 1
  3166. * <b><i></i><u></u></b> => 1
  3167. * <b></b> => 1
  3168. * <b>xx<i></i></b> => 0
  3169. * ```
  3170. */
  3171. isEmptyInlineElement: function(node) {
  3172. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3173. return 0;
  3174. }
  3175. node = node.firstChild;
  3176. while (node) {
  3177. //如果是创建的bookmark就跳过
  3178. if (domUtils.isBookmarkNode(node)) {
  3179. return 0;
  3180. }
  3181. if (
  3182. (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node)) ||
  3183. (node.nodeType == 3 && !domUtils.isWhitespace(node))
  3184. ) {
  3185. return 0;
  3186. }
  3187. node = node.nextSibling;
  3188. }
  3189. return 1;
  3190. },
  3191. /**
  3192. * 删除node节点下首尾两端的空白文本子节点
  3193. * @method trimWhiteTextNode
  3194. * @param { Element } node 需要执行删除操作的元素对象
  3195. * @example
  3196. * ```javascript
  3197. * var node = document.createElement("div");
  3198. *
  3199. * node.appendChild( document.createTextNode( "" ) );
  3200. *
  3201. * node.appendChild( document.createElement("div") );
  3202. *
  3203. * node.appendChild( document.createTextNode( "" ) );
  3204. *
  3205. * //3
  3206. * console.log( node.childNodes.length );
  3207. *
  3208. * UE.dom.domUtils.trimWhiteTextNode( node );
  3209. *
  3210. * //1
  3211. * console.log( node.childNodes.length );
  3212. * ```
  3213. */
  3214. trimWhiteTextNode: function(node) {
  3215. function remove(dir) {
  3216. var child;
  3217. while (
  3218. (child = node[dir]) &&
  3219. child.nodeType == 3 &&
  3220. domUtils.isWhitespace(child)
  3221. ) {
  3222. node.removeChild(child);
  3223. }
  3224. }
  3225. remove("firstChild");
  3226. remove("lastChild");
  3227. },
  3228. /**
  3229. * 合并node节点下相同的子节点
  3230. * @name mergeChild
  3231. * @desc
  3232. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3233. * @example
  3234. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3235. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3236. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3237. */
  3238. mergeChild: function(node, tagName, attrs) {
  3239. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase());
  3240. for (var i = 0, ci; (ci = list[i++]); ) {
  3241. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3242. continue;
  3243. }
  3244. //span单独处理
  3245. if (ci.tagName.toLowerCase() == "span") {
  3246. if (node === ci.parentNode) {
  3247. domUtils.trimWhiteTextNode(node);
  3248. if (node.childNodes.length == 1) {
  3249. node.style.cssText = ci.style.cssText + ";" + node.style.cssText;
  3250. domUtils.remove(ci, true);
  3251. continue;
  3252. }
  3253. }
  3254. ci.style.cssText = node.style.cssText + ";" + ci.style.cssText;
  3255. if (attrs) {
  3256. var style = attrs.style;
  3257. if (style) {
  3258. style = style.split(";");
  3259. for (var j = 0, s; (s = style[j++]); ) {
  3260. ci.style[utils.cssStyleToDomStyle(s.split(":")[0])] = s.split(
  3261. ":"
  3262. )[1];
  3263. }
  3264. }
  3265. }
  3266. if (domUtils.isSameStyle(ci, node)) {
  3267. domUtils.remove(ci, true);
  3268. }
  3269. continue;
  3270. }
  3271. if (domUtils.isSameElement(node, ci)) {
  3272. domUtils.remove(ci, true);
  3273. }
  3274. }
  3275. },
  3276. /**
  3277. * 原生方法getElementsByTagName的封装
  3278. * @method getElementsByTagName
  3279. * @param { Node } node 目标节点对象
  3280. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3281. * @return { Array } 符合条件的节点集合
  3282. */
  3283. getElementsByTagName: function(node, name, filter) {
  3284. if (filter && utils.isString(filter)) {
  3285. var className = filter;
  3286. filter = function(node) {
  3287. return domUtils.hasClass(node, className);
  3288. };
  3289. }
  3290. name = utils.trim(name).replace(/[ ]{2,}/g, " ").split(" ");
  3291. var arr = [];
  3292. for (var n = 0, ni; (ni = name[n++]); ) {
  3293. var list = node.getElementsByTagName(ni);
  3294. for (var i = 0, ci; (ci = list[i++]); ) {
  3295. if (!filter || filter(ci)) arr.push(ci);
  3296. }
  3297. }
  3298. return arr;
  3299. },
  3300. /**
  3301. * 将节点node提取到父节点上
  3302. * @method mergeToParent
  3303. * @param { Element } node 需要提取的元素对象
  3304. * @example
  3305. * ```html
  3306. * <div id="parent">
  3307. * <div id="sub">
  3308. * <span id="child"></span>
  3309. * </div>
  3310. * </div>
  3311. *
  3312. * <script>
  3313. *
  3314. * var child = document.getElementById( "child" );
  3315. *
  3316. * //output: sub
  3317. * console.log( child.parentNode.id );
  3318. *
  3319. * UE.dom.domUtils.mergeToParent( child );
  3320. *
  3321. * //output: parent
  3322. * console.log( child.parentNode.id );
  3323. *
  3324. * </script>
  3325. * ```
  3326. */
  3327. mergeToParent: function(node) {
  3328. var parent = node.parentNode;
  3329. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3330. if (parent.tagName == node.tagName || parent.tagName == "A") {
  3331. //针对a标签单独处理
  3332. domUtils.trimWhiteTextNode(parent);
  3333. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3334. if (
  3335. (parent.tagName == "SPAN" && !domUtils.isSameStyle(parent, node)) ||
  3336. (parent.tagName == "A" && node.tagName == "SPAN")
  3337. ) {
  3338. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3339. node.style.cssText =
  3340. parent.style.cssText + ";" + node.style.cssText;
  3341. parent = parent.parentNode;
  3342. continue;
  3343. } else {
  3344. parent.style.cssText += ";" + node.style.cssText;
  3345. //trace:952 a标签要保持下划线
  3346. if (parent.tagName == "A") {
  3347. parent.style.textDecoration = "underline";
  3348. }
  3349. }
  3350. }
  3351. if (parent.tagName != "A") {
  3352. parent === node.parentNode && domUtils.remove(node, true);
  3353. break;
  3354. }
  3355. }
  3356. parent = parent.parentNode;
  3357. }
  3358. },
  3359. /**
  3360. * 合并节点node的左右兄弟节点
  3361. * @method mergeSibling
  3362. * @param { Element } node 需要合并的目标节点
  3363. * @example
  3364. * ```html
  3365. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3366. *
  3367. * <script>
  3368. * var demoNode = document.getElementById("test");
  3369. * UE.dom.domUtils.mergeSibling( demoNode );
  3370. * //output: xxxxoooxxxx
  3371. * console.log( demoNode.innerHTML );
  3372. * </script>
  3373. * ```
  3374. */
  3375. /**
  3376. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3377. * @method mergeSibling
  3378. * @param { Element } node 需要合并的目标节点
  3379. * @param { Boolean } ignorePre 是否忽略合并左节点
  3380. * @example
  3381. * ```html
  3382. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3383. *
  3384. * <script>
  3385. * var demoNode = document.getElementById("test");
  3386. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3387. * //output: oooxxxx
  3388. * console.log( demoNode.innerHTML );
  3389. * </script>
  3390. * ```
  3391. */
  3392. /**
  3393. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3394. * @method mergeSibling
  3395. * @param { Element } node 需要合并的目标节点
  3396. * @param { Boolean } ignorePre 是否忽略合并左节点
  3397. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3398. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3399. * @example
  3400. * ```html
  3401. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3402. *
  3403. * <script>
  3404. * var demoNode = document.getElementById("test");
  3405. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3406. * //output: xxxxooo
  3407. * console.log( demoNode.innerHTML );
  3408. * </script>
  3409. * ```
  3410. */
  3411. mergeSibling: function(node, ignorePre, ignoreNext) {
  3412. function merge(rtl, start, node) {
  3413. var next;
  3414. if (
  3415. (next = node[rtl]) &&
  3416. !domUtils.isBookmarkNode(next) &&
  3417. next.nodeType == 1 &&
  3418. domUtils.isSameElement(node, next)
  3419. ) {
  3420. while (next.firstChild) {
  3421. if (start == "firstChild") {
  3422. node.insertBefore(next.lastChild, node.firstChild);
  3423. } else {
  3424. node.appendChild(next.firstChild);
  3425. }
  3426. }
  3427. domUtils.remove(next);
  3428. }
  3429. }
  3430. !ignorePre && merge("previousSibling", "firstChild", node);
  3431. !ignoreNext && merge("nextSibling", "lastChild", node);
  3432. },
  3433. /**
  3434. * 设置节点node及其子节点不会被选中
  3435. * @method unSelectable
  3436. * @param { Element } node 需要执行操作的dom元素
  3437. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3438. * @example
  3439. * ```javascript
  3440. * UE.dom.domUtils.unSelectable( document.body );
  3441. * ```
  3442. */
  3443. unSelectable: (ie && browser.ie9below) || browser.opera
  3444. ? function(node) {
  3445. //for ie9
  3446. node.onselectstart = function() {
  3447. return false;
  3448. };
  3449. node.onclick = node.onkeyup = node.onkeydown = function() {
  3450. return false;
  3451. };
  3452. node.unselectable = "on";
  3453. node.setAttribute("unselectable", "on");
  3454. for (var i = 0, ci; (ci = node.all[i++]); ) {
  3455. switch (ci.tagName.toLowerCase()) {
  3456. case "iframe":
  3457. case "textarea":
  3458. case "input":
  3459. case "select":
  3460. break;
  3461. default:
  3462. ci.unselectable = "on";
  3463. node.setAttribute("unselectable", "on");
  3464. }
  3465. }
  3466. }
  3467. : function(node) {
  3468. node.style.MozUserSelect = node.style.webkitUserSelect = node.style.msUserSelect = node.style.KhtmlUserSelect =
  3469. "none";
  3470. },
  3471. /**
  3472. * 删除节点node上的指定属性名称的属性
  3473. * @method removeAttributes
  3474. * @param { Node } node 需要删除属性的节点对象
  3475. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3476. * @example
  3477. * ```html
  3478. * <div id="wrap">
  3479. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3480. * </div>
  3481. *
  3482. * <script>
  3483. *
  3484. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3485. *
  3486. * //output: <span style="font-size:14px;">xxxxx</span>
  3487. * console.log( document.getElementById("wrap").innerHTML );
  3488. *
  3489. * </script>
  3490. * ```
  3491. */
  3492. /**
  3493. * 删除节点node上的指定属性名称的属性
  3494. * @method removeAttributes
  3495. * @param { Node } node 需要删除属性的节点对象
  3496. * @param { Array } attrNames 需要删除的属性名数组
  3497. * @example
  3498. * ```html
  3499. * <div id="wrap">
  3500. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3501. * </div>
  3502. *
  3503. * <script>
  3504. *
  3505. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3506. *
  3507. * //output: <span style="font-size:14px;">xxxxx</span>
  3508. * console.log( document.getElementById("wrap").innerHTML );
  3509. *
  3510. * </script>
  3511. * ```
  3512. */
  3513. removeAttributes: function(node, attrNames) {
  3514. attrNames = utils.isArray(attrNames)
  3515. ? attrNames
  3516. : utils.trim(attrNames).replace(/[ ]{2,}/g, " ").split(" ");
  3517. for (var i = 0, ci; (ci = attrNames[i++]); ) {
  3518. ci = attrFix[ci] || ci;
  3519. switch (ci) {
  3520. case "className":
  3521. node[ci] = "";
  3522. break;
  3523. case "style":
  3524. node.style.cssText = "";
  3525. var val = node.getAttributeNode("style");
  3526. !browser.ie && val && node.removeAttributeNode(val);
  3527. }
  3528. node.removeAttribute(ci);
  3529. }
  3530. },
  3531. /**
  3532. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3533. * @method createElement
  3534. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3535. * @param { String } tagName 需要创建的元素的标签名
  3536. * @param { Object } attrs 新创建的元素的属性key-value集合
  3537. * @return { Element } 新创建的元素对象
  3538. * @example
  3539. * ```javascript
  3540. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3541. * id: 'test'
  3542. * } );
  3543. *
  3544. * //output: DIV
  3545. * console.log( ele.tagName );
  3546. *
  3547. * //output: test
  3548. * console.log( ele.id );
  3549. *
  3550. * ```
  3551. */
  3552. createElement: function(doc, tag, attrs) {
  3553. return domUtils.setAttributes(doc.createElement(tag), attrs);
  3554. },
  3555. /**
  3556. * 为节点node添加属性attrs,attrs为属性键值对
  3557. * @method setAttributes
  3558. * @param { Element } node 需要设置属性的元素对象
  3559. * @param { Object } attrs 需要设置的属性名-值对
  3560. * @return { Element } 设置属性的元素对象
  3561. * @example
  3562. * ```html
  3563. * <span id="test"></span>
  3564. *
  3565. * <script>
  3566. *
  3567. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3568. * id: 'demo'
  3569. * } );
  3570. *
  3571. * //output: demo
  3572. * console.log( testNode.id );
  3573. *
  3574. * </script>
  3575. *
  3576. */
  3577. setAttributes: function(node, attrs) {
  3578. for (var attr in attrs) {
  3579. if (attrs.hasOwnProperty(attr)) {
  3580. var value = attrs[attr];
  3581. switch (attr) {
  3582. case "class":
  3583. //ie下要这样赋值,setAttribute不起作用
  3584. node.className = value;
  3585. break;
  3586. case "style":
  3587. node.style.cssText = node.style.cssText + ";" + value;
  3588. break;
  3589. case "innerHTML":
  3590. node[attr] = value;
  3591. break;
  3592. case "value":
  3593. node.value = value;
  3594. break;
  3595. default:
  3596. node.setAttribute(attrFix[attr] || attr, value);
  3597. }
  3598. }
  3599. }
  3600. return node;
  3601. },
  3602. /**
  3603. * 获取元素element经过计算后的样式值
  3604. * @method getComputedStyle
  3605. * @param { Element } element 需要获取样式的元素对象
  3606. * @param { String } styleName 需要获取的样式名
  3607. * @return { String } 获取到的样式值
  3608. * @example
  3609. * ```html
  3610. * <style type="text/css">
  3611. * #test {
  3612. * font-size: 15px;
  3613. * }
  3614. * </style>
  3615. *
  3616. * <span id="test"></span>
  3617. *
  3618. * <script>
  3619. * //output: 15px
  3620. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3621. * </script>
  3622. * ```
  3623. */
  3624. getComputedStyle: function(element, styleName) {
  3625. //一下的属性单独处理
  3626. var pros = "width height top left";
  3627. if (pros.indexOf(styleName) > -1) {
  3628. return (
  3629. element[
  3630. "offset" +
  3631. styleName.replace(/^\w/, function(s) {
  3632. return s.toUpperCase();
  3633. })
  3634. ] + "px"
  3635. );
  3636. }
  3637. //忽略文本节点
  3638. if (element.nodeType == 3) {
  3639. element = element.parentNode;
  3640. }
  3641. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3642. if (
  3643. browser.ie &&
  3644. browser.version < 9 &&
  3645. styleName == "font-size" &&
  3646. !element.style.fontSize &&
  3647. !dtd.$empty[element.tagName] &&
  3648. !dtd.$nonChild[element.tagName]
  3649. ) {
  3650. var span = element.ownerDocument.createElement("span");
  3651. span.style.cssText = "padding:0;border:0;font-family:simsun;";
  3652. span.innerHTML = ".";
  3653. element.appendChild(span);
  3654. var result = span.offsetHeight;
  3655. element.removeChild(span);
  3656. span = null;
  3657. return result + "px";
  3658. }
  3659. try {
  3660. var value = domUtils.getStyle(element, styleName) ||
  3661. (window.getComputedStyle
  3662. ? domUtils.getWindow(element).getComputedStyle(element, "").getPropertyValue(styleName)
  3663. : (element.currentStyle || element.style)[utils.cssStyleToDomStyle(styleName)]);
  3664. } catch (e) {
  3665. return "";
  3666. }
  3667. return utils.transUnitToPx(utils.fixColor(styleName, value));
  3668. },
  3669. /**
  3670. * 删除元素element指定的className
  3671. * @method removeClasses
  3672. * @param { Element } ele 需要删除class的元素节点
  3673. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3674. * @example
  3675. * ```html
  3676. * <span id="test" class="test1 test2 test3">xxx</span>
  3677. *
  3678. * <script>
  3679. *
  3680. * var testNode = document.getElementById( "test" );
  3681. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3682. *
  3683. * //output: test3
  3684. * console.log( testNode.className );
  3685. *
  3686. * </script>
  3687. * ```
  3688. */
  3689. /**
  3690. * 删除元素element指定的className
  3691. * @method removeClasses
  3692. * @param { Element } ele 需要删除class的元素节点
  3693. * @param { Array } classNames 需要删除的className数组
  3694. * @example
  3695. * ```html
  3696. * <span id="test" class="test1 test2 test3">xxx</span>
  3697. *
  3698. * <script>
  3699. *
  3700. * var testNode = document.getElementById( "test" );
  3701. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3702. *
  3703. * //output: test3
  3704. * console.log( testNode.className );
  3705. *
  3706. * </script>
  3707. * ```
  3708. */
  3709. removeClasses: function(elm, classNames) {
  3710. classNames = utils.isArray(classNames)
  3711. ? classNames
  3712. : utils.trim(classNames).replace(/[ ]{2,}/g, " ").split(" ");
  3713. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3714. cls = cls.replace(new RegExp("\\b" + ci + "\\b"), "");
  3715. }
  3716. cls = utils.trim(cls).replace(/[ ]{2,}/g, " ");
  3717. if (cls) {
  3718. elm.className = cls;
  3719. } else {
  3720. domUtils.removeAttributes(elm, ["class"]);
  3721. }
  3722. },
  3723. /**
  3724. * 给元素element添加className
  3725. * @method addClass
  3726. * @param { Node } ele 需要增加className的元素
  3727. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3728. * @remind 相同的类名不会被重复添加
  3729. * @example
  3730. * ```html
  3731. * <span id="test" class="cls1 cls2"></span>
  3732. *
  3733. * <script>
  3734. * var testNode = document.getElementById("test");
  3735. *
  3736. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3737. *
  3738. * //output: cl1 cls2 cls3 cls4
  3739. * console.log( testNode.className );
  3740. *
  3741. * <script>
  3742. * ```
  3743. */
  3744. /**
  3745. * 给元素element添加className
  3746. * @method addClass
  3747. * @param { Node } ele 需要增加className的元素
  3748. * @param { Array } classNames 需要添加的className的数组
  3749. * @remind 相同的类名不会被重复添加
  3750. * @example
  3751. * ```html
  3752. * <span id="test" class="cls1 cls2"></span>
  3753. *
  3754. * <script>
  3755. * var testNode = document.getElementById("test");
  3756. *
  3757. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3758. *
  3759. * //output: cl1 cls2 cls3 cls4
  3760. * console.log( testNode.className );
  3761. *
  3762. * <script>
  3763. * ```
  3764. */
  3765. addClass: function(elm, classNames) {
  3766. if (!elm) return;
  3767. classNames = utils.trim(classNames).replace(/[ ]{2,}/g, " ").split(" ");
  3768. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3769. if (!new RegExp("\\b" + ci + "\\b").test(cls)) {
  3770. cls += " " + ci;
  3771. }
  3772. }
  3773. elm.className = utils.trim(cls);
  3774. },
  3775. /**
  3776. * 判断元素element是否包含给定的样式类名className
  3777. * @method hasClass
  3778. * @param { Node } ele 需要检测的元素
  3779. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3780. * @return { Boolean } 元素是否包含所有给定的className
  3781. * @example
  3782. * ```html
  3783. * <span id="test1" class="cls1 cls2"></span>
  3784. *
  3785. * <script>
  3786. * var test1 = document.getElementById("test1");
  3787. *
  3788. * //output: false
  3789. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3790. *
  3791. * //output: true
  3792. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3793. * </script>
  3794. * ```
  3795. */
  3796. /**
  3797. * 判断元素element是否包含给定的样式类名className
  3798. * @method hasClass
  3799. * @param { Node } ele 需要检测的元素
  3800. * @param { Array } classNames 需要检测的className数组
  3801. * @return { Boolean } 元素是否包含所有给定的className
  3802. * @example
  3803. * ```html
  3804. * <span id="test1" class="cls1 cls2"></span>
  3805. *
  3806. * <script>
  3807. * var test1 = document.getElementById("test1");
  3808. *
  3809. * //output: false
  3810. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3811. *
  3812. * //output: true
  3813. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3814. * </script>
  3815. * ```
  3816. */
  3817. hasClass: function(element, className) {
  3818. if (utils.isRegExp(className)) {
  3819. return className.test(element.className);
  3820. }
  3821. className = utils.trim(className).replace(/[ ]{2,}/g, " ").split(" ");
  3822. for (var i = 0, ci, cls = element.className; (ci = className[i++]); ) {
  3823. if (!new RegExp("\\b" + ci + "\\b", "i").test(cls)) {
  3824. return false;
  3825. }
  3826. }
  3827. return i - 1 == className.length;
  3828. },
  3829. /**
  3830. * 阻止事件默认行为
  3831. * @method preventDefault
  3832. * @param { Event } evt 需要阻止默认行为的事件对象
  3833. * @example
  3834. * ```javascript
  3835. * UE.dom.domUtils.preventDefault( evt );
  3836. * ```
  3837. */
  3838. preventDefault: function(evt) {
  3839. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  3840. },
  3841. /**
  3842. * 删除元素element指定的样式
  3843. * @method removeStyle
  3844. * @param { Element } element 需要删除样式的元素
  3845. * @param { String } styleName 需要删除的样式名
  3846. * @example
  3847. * ```html
  3848. * <span id="test" style="color: red; background: blue;"></span>
  3849. *
  3850. * <script>
  3851. *
  3852. * var testNode = document.getElementById("test");
  3853. *
  3854. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3855. *
  3856. * //output: background: blue;
  3857. * console.log( testNode.style.cssText );
  3858. *
  3859. * </script>
  3860. * ```
  3861. */
  3862. removeStyle: function(element, name) {
  3863. if (browser.ie) {
  3864. //针对color先单独处理一下
  3865. if (name == "color") {
  3866. name = "(^|;)" + name;
  3867. }
  3868. element.style.cssText = element.style.cssText.replace(
  3869. new RegExp(name + "[^:]*:[^;]+;?", "ig"),
  3870. ""
  3871. );
  3872. } else {
  3873. if (element.style.removeProperty) {
  3874. element.style.removeProperty(name);
  3875. } else {
  3876. element.style.removeAttribute(utils.cssStyleToDomStyle(name));
  3877. }
  3878. }
  3879. if (!element.style.cssText) {
  3880. domUtils.removeAttributes(element, ["style"]);
  3881. }
  3882. },
  3883. /**
  3884. * 获取元素element的style属性的指定值
  3885. * @method getStyle
  3886. * @param { Element } element 需要获取属性值的元素
  3887. * @param { String } styleName 需要获取的style的名称
  3888. * @warning 该方法仅获取元素style属性中所标明的值
  3889. * @return { String } 该元素包含指定的style属性值
  3890. * @example
  3891. * ```html
  3892. * <div id="test" style="color: red;"></div>
  3893. *
  3894. * <script>
  3895. *
  3896. * var testNode = document.getElementById( "test" );
  3897. *
  3898. * //output: red
  3899. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3900. *
  3901. * //output: ""
  3902. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3903. *
  3904. * </script>
  3905. * ```
  3906. */
  3907. getStyle: function(element, name) {
  3908. var value = element.style[utils.cssStyleToDomStyle(name)];
  3909. return utils.fixColor(name, value);
  3910. },
  3911. /**
  3912. * 为元素element设置样式属性值
  3913. * @method setStyle
  3914. * @param { Element } element 需要设置样式的元素
  3915. * @param { String } styleName 样式名
  3916. * @param { String } styleValue 样式值
  3917. * @example
  3918. * ```html
  3919. * <div id="test"></div>
  3920. *
  3921. * <script>
  3922. *
  3923. * var testNode = document.getElementById( "test" );
  3924. *
  3925. * //output: ""
  3926. * console.log( testNode.style.color );
  3927. *
  3928. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3929. * //output: "red"
  3930. * console.log( testNode.style.color );
  3931. *
  3932. * </script>
  3933. * ```
  3934. */
  3935. setStyle: function(element, name, value) {
  3936. element.style[utils.cssStyleToDomStyle(name)] = value;
  3937. if (!utils.trim(element.style.cssText)) {
  3938. this.removeAttributes(element, "style");
  3939. }
  3940. },
  3941. /**
  3942. * 为元素element设置多个样式属性值
  3943. * @method setStyles
  3944. * @param { Element } element 需要设置样式的元素
  3945. * @param { Object } styles 样式名值对
  3946. * @example
  3947. * ```html
  3948. * <div id="test"></div>
  3949. *
  3950. * <script>
  3951. *
  3952. * var testNode = document.getElementById( "test" );
  3953. *
  3954. * //output: ""
  3955. * console.log( testNode.style.color );
  3956. *
  3957. * UE.dom.domUtils.setStyles( testNode, {
  3958. * 'color': 'red'
  3959. * } );
  3960. * //output: "red"
  3961. * console.log( testNode.style.color );
  3962. *
  3963. * </script>
  3964. * ```
  3965. */
  3966. setStyles: function(element, styles) {
  3967. for (var name in styles) {
  3968. if (styles.hasOwnProperty(name)) {
  3969. domUtils.setStyle(element, name, styles[name]);
  3970. }
  3971. }
  3972. },
  3973. /**
  3974. * 删除_moz_dirty属性
  3975. * @private
  3976. * @method removeDirtyAttr
  3977. */
  3978. removeDirtyAttr: function(node) {
  3979. for (
  3980. var i = 0, ci, nodes = node.getElementsByTagName("*");
  3981. (ci = nodes[i++]);
  3982. ) {
  3983. ci.removeAttribute("_moz_dirty");
  3984. }
  3985. node.removeAttribute("_moz_dirty");
  3986. },
  3987. /**
  3988. * 获取子节点的数量
  3989. * @method getChildCount
  3990. * @param { Element } node 需要检测的元素
  3991. * @return { Number } 给定的node元素的子节点数量
  3992. * @example
  3993. * ```html
  3994. * <div id="test">
  3995. * <span></span>
  3996. * </div>
  3997. *
  3998. * <script>
  3999. *
  4000. * //output: 3
  4001. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  4002. *
  4003. * </script>
  4004. * ```
  4005. */
  4006. /**
  4007. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  4008. * @method getChildCount
  4009. * @param { Element } node 需要检测的元素
  4010. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  4011. * @return { Number } 符合过滤条件的node元素的子节点数量
  4012. * @example
  4013. * ```html
  4014. * <div id="test">
  4015. * <span></span>
  4016. * </div>
  4017. *
  4018. * <script>
  4019. *
  4020. * //output: 1
  4021. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  4022. *
  4023. * return node.nodeType === 1;
  4024. *
  4025. * } ) );
  4026. *
  4027. * </script>
  4028. * ```
  4029. */
  4030. getChildCount: function(node, fn) {
  4031. var count = 0,
  4032. first = node.firstChild;
  4033. fn =
  4034. fn ||
  4035. function() {
  4036. return 1;
  4037. };
  4038. while (first) {
  4039. if (fn(first)) {
  4040. count++;
  4041. }
  4042. first = first.nextSibling;
  4043. }
  4044. return count;
  4045. },
  4046. /**
  4047. * 判断给定节点是否为空节点
  4048. * @method isEmptyNode
  4049. * @param { Node } node 需要检测的节点对象
  4050. * @return { Boolean } 节点是否为空
  4051. * @example
  4052. * ```javascript
  4053. * UE.dom.domUtils.isEmptyNode( document.body );
  4054. * ```
  4055. */
  4056. isEmptyNode: function(node) {
  4057. return (
  4058. !node.firstChild ||
  4059. domUtils.getChildCount(node, function(node) {
  4060. return (
  4061. !domUtils.isBr(node) &&
  4062. !domUtils.isBookmarkNode(node) &&
  4063. !domUtils.isWhitespace(node)
  4064. );
  4065. }) == 0
  4066. );
  4067. },
  4068. clearSelectedArr: function(nodes) {
  4069. var node;
  4070. while ((node = nodes.pop())) {
  4071. domUtils.removeAttributes(node, ["class"]);
  4072. }
  4073. },
  4074. /**
  4075. * 将显示区域滚动到指定节点的位置
  4076. * @method scrollToView
  4077. * @param {Node} node 节点
  4078. * @param {window} win window对象
  4079. * @param {Number} offsetTop 距离上方的偏移量
  4080. */
  4081. scrollToView: function(node, win, offsetTop) {
  4082. var getViewPaneSize = function() {
  4083. var doc = win.document,
  4084. mode = doc.compatMode == "CSS1Compat";
  4085. return {
  4086. width:
  4087. (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) || 0,
  4088. height:
  4089. (mode ? doc.documentElement.clientHeight : doc.body.clientHeight) || 0
  4090. };
  4091. },
  4092. getScrollPosition = function(win) {
  4093. if ("pageXOffset" in win) {
  4094. return {
  4095. x: win.pageXOffset || 0,
  4096. y: win.pageYOffset || 0
  4097. };
  4098. } else {
  4099. var doc = win.document;
  4100. return {
  4101. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4102. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0
  4103. };
  4104. }
  4105. };
  4106. var winHeight = getViewPaneSize().height,
  4107. offset = winHeight * -1 + offsetTop;
  4108. offset += node.offsetHeight || 0;
  4109. var elementPosition = domUtils.getXY(node);
  4110. offset += elementPosition.y;
  4111. var currentScroll = getScrollPosition(win).y;
  4112. // offset += 50;
  4113. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4114. win.scrollTo(0, offset + (offset < 0 ? -20 : 20));
  4115. }
  4116. },
  4117. /**
  4118. * 判断给定节点是否为br
  4119. * @method isBr
  4120. * @param { Node } node 需要判断的节点对象
  4121. * @return { Boolean } 给定的节点是否是br节点
  4122. */
  4123. isBr: function(node) {
  4124. return node.nodeType == 1 && node.tagName == "BR";
  4125. },
  4126. /**
  4127. * 判断给定的节点是否是一个“填充”节点
  4128. * @private
  4129. * @method isFillChar
  4130. * @param { Node } node 需要判断的节点
  4131. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4132. * @returns { Boolean } 节点是否是填充节点
  4133. */
  4134. isFillChar: function(node, isInStart) {
  4135. if (node.nodeType != 3) return false;
  4136. var text = node.nodeValue;
  4137. if (isInStart) {
  4138. return new RegExp("^" + domUtils.fillChar).test(text);
  4139. }
  4140. return !text.replace(new RegExp(domUtils.fillChar, "g"), "").length;
  4141. },
  4142. isStartInblock: function(range) {
  4143. var tmpRange = range.cloneRange(),
  4144. flag = 0,
  4145. start = tmpRange.startContainer,
  4146. tmp;
  4147. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4148. start = start.childNodes[tmpRange.startOffset];
  4149. var pre = start.previousSibling;
  4150. while (pre && domUtils.isFillChar(pre)) {
  4151. start = pre;
  4152. pre = pre.previousSibling;
  4153. }
  4154. }
  4155. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4156. tmpRange.setStartBefore(start);
  4157. start = tmpRange.startContainer;
  4158. }
  4159. while (start && domUtils.isFillChar(start)) {
  4160. tmp = start;
  4161. start = start.previousSibling;
  4162. }
  4163. if (tmp) {
  4164. tmpRange.setStartBefore(tmp);
  4165. start = tmpRange.startContainer;
  4166. }
  4167. if (
  4168. start.nodeType == 1 &&
  4169. domUtils.isEmptyNode(start) &&
  4170. tmpRange.startOffset == 1
  4171. ) {
  4172. tmpRange.setStart(start, 0).collapse(true);
  4173. }
  4174. while (!tmpRange.startOffset) {
  4175. start = tmpRange.startContainer;
  4176. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4177. flag = 1;
  4178. break;
  4179. }
  4180. var pre = tmpRange.startContainer.previousSibling,
  4181. tmpNode;
  4182. if (!pre) {
  4183. tmpRange.setStartBefore(tmpRange.startContainer);
  4184. } else {
  4185. while (pre && domUtils.isFillChar(pre)) {
  4186. tmpNode = pre;
  4187. pre = pre.previousSibling;
  4188. }
  4189. if (tmpNode) {
  4190. tmpRange.setStartBefore(tmpNode);
  4191. } else {
  4192. tmpRange.setStartBefore(tmpRange.startContainer);
  4193. }
  4194. }
  4195. }
  4196. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0;
  4197. },
  4198. /**
  4199. * 判断给定的元素是否是一个空元素
  4200. * @method isEmptyBlock
  4201. * @param { Element } node 需要判断的元素
  4202. * @return { Boolean } 是否是空元素
  4203. * @example
  4204. * ```html
  4205. * <div id="test"></div>
  4206. *
  4207. * <script>
  4208. * //output: true
  4209. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4210. * </script>
  4211. * ```
  4212. */
  4213. /**
  4214. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4215. * @method isEmptyBlock
  4216. * @param { Element } node 需要判断的元素
  4217. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4218. * @return { Boolean } 是否是空元素
  4219. */
  4220. isEmptyBlock: function(node, reg) {
  4221. if (node.nodeType != 1) return 0;
  4222. reg = reg || new RegExp("[ \xa0\t\r\n" + domUtils.fillChar + "]", "g");
  4223. if (
  4224. node[browser.ie ? "innerText" : "textContent"].replace(reg, "").length > 0
  4225. ) {
  4226. return 0;
  4227. }
  4228. for (var n in dtd.$isNotEmpty) {
  4229. if (node.getElementsByTagName(n).length) {
  4230. return 0;
  4231. }
  4232. }
  4233. return 1;
  4234. },
  4235. /**
  4236. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4237. * @method setViewportOffset
  4238. * @param { Element } element 需要设置偏移量的元素
  4239. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4240. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4241. * offset.top的距离
  4242. * @example
  4243. * ```html
  4244. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4245. *
  4246. * <script>
  4247. *
  4248. * var testNode = document.getElementById("test");
  4249. *
  4250. * UE.dom.domUtils.setViewportOffset( testNode, {
  4251. * left: 200,
  4252. * top: 50
  4253. * } );
  4254. *
  4255. * //output: top: 300px; left: 100px; position: absolute;
  4256. * console.log( testNode.style.cssText );
  4257. *
  4258. * </script>
  4259. * ```
  4260. */
  4261. setViewportOffset: function(element, offset) {
  4262. var left = parseInt(element.style.left) | 0;
  4263. var top = parseInt(element.style.top) | 0;
  4264. var rect = element.getBoundingClientRect();
  4265. var offsetLeft = offset.left - rect.left;
  4266. var offsetTop = offset.top - rect.top;
  4267. if (offsetLeft) {
  4268. element.style.left = left + offsetLeft + "px";
  4269. }
  4270. if (offsetTop) {
  4271. element.style.top = top + offsetTop + "px";
  4272. }
  4273. },
  4274. /**
  4275. * 用“填充字符”填充节点
  4276. * @method fillNode
  4277. * @private
  4278. * @param { DomDocument } doc 填充的节点所在的docment对象
  4279. * @param { Node } node 需要填充的节点对象
  4280. * @example
  4281. * ```html
  4282. * <div id="test"></div>
  4283. *
  4284. * <script>
  4285. * var testNode = document.getElementById("test");
  4286. *
  4287. * //output: 0
  4288. * console.log( testNode.childNodes.length );
  4289. *
  4290. * UE.dom.domUtils.fillNode( document, testNode );
  4291. *
  4292. * //output: 1
  4293. * console.log( testNode.childNodes.length );
  4294. *
  4295. * </script>
  4296. * ```
  4297. */
  4298. fillNode: function(doc, node) {
  4299. var tmpNode = browser.ie
  4300. ? doc.createTextNode(domUtils.fillChar)
  4301. : doc.createElement("br");
  4302. node.innerHTML = "";
  4303. node.appendChild(tmpNode);
  4304. },
  4305. /**
  4306. * 把节点src的所有子节点追加到另一个节点tag上去
  4307. * @method moveChild
  4308. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4309. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4310. * @example
  4311. * ```html
  4312. * <div id="test1">
  4313. * <span></span>
  4314. * </div>
  4315. * <div id="test2">
  4316. * <div></div>
  4317. * </div>
  4318. *
  4319. * <script>
  4320. *
  4321. * var test1 = document.getElementById("test1"),
  4322. * test2 = document.getElementById("test2");
  4323. *
  4324. * UE.dom.domUtils.moveChild( test1, test2 );
  4325. *
  4326. * //output: ""(空字符串)
  4327. * console.log( test1.innerHTML );
  4328. *
  4329. * //output: "<div></div><span></span>"
  4330. * console.log( test2.innerHTML );
  4331. *
  4332. * </script>
  4333. * ```
  4334. */
  4335. /**
  4336. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4337. * @method moveChild
  4338. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4339. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4340. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4341. * @example
  4342. * ```html
  4343. * <div id="test1">
  4344. * <span></span>
  4345. * </div>
  4346. * <div id="test2">
  4347. * <div></div>
  4348. * </div>
  4349. *
  4350. * <script>
  4351. *
  4352. * var test1 = document.getElementById("test1"),
  4353. * test2 = document.getElementById("test2");
  4354. *
  4355. * UE.dom.domUtils.moveChild( test1, test2, true );
  4356. *
  4357. * //output: ""(空字符串)
  4358. * console.log( test1.innerHTML );
  4359. *
  4360. * //output: "<span></span><div></div>"
  4361. * console.log( test2.innerHTML );
  4362. *
  4363. * </script>
  4364. * ```
  4365. */
  4366. moveChild: function(src, tag, dir) {
  4367. while (src.firstChild) {
  4368. if (dir && tag.firstChild) {
  4369. tag.insertBefore(src.lastChild, tag.firstChild);
  4370. } else {
  4371. tag.appendChild(src.firstChild);
  4372. }
  4373. }
  4374. },
  4375. /**
  4376. * 判断节点的标签上是否不存在任何属性
  4377. * @method hasNoAttributes
  4378. * @private
  4379. * @param { Node } node 需要检测的节点对象
  4380. * @return { Boolean } 节点是否不包含任何属性
  4381. * @example
  4382. * ```html
  4383. * <div id="test"><span>xxxx</span></div>
  4384. *
  4385. * <script>
  4386. *
  4387. * //output: false
  4388. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4389. *
  4390. * //output: true
  4391. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4392. *
  4393. * </script>
  4394. * ```
  4395. */
  4396. hasNoAttributes: function(node) {
  4397. return browser.ie
  4398. ? /^<\w+\s*?>/.test(node.outerHTML)
  4399. : node.attributes.length == 0;
  4400. },
  4401. /**
  4402. * 检测节点是否是UEditor所使用的辅助节点
  4403. * @method isCustomeNode
  4404. * @private
  4405. * @param { Node } node 需要检测的节点
  4406. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4407. * @return { Boolean } 给定的节点是否是一个辅助节点
  4408. */
  4409. isCustomeNode: function(node) {
  4410. return node.nodeType == 1 && node.getAttribute("_ue_custom_node_");
  4411. },
  4412. /**
  4413. * 检测节点的标签是否是给定的标签
  4414. * @method isTagNode
  4415. * @param { Node } node 需要检测的节点对象
  4416. * @param { String } tagName 标签
  4417. * @return { Boolean } 节点的标签是否是给定的标签
  4418. * @example
  4419. * ```html
  4420. * <div id="test"></div>
  4421. *
  4422. * <script>
  4423. *
  4424. * //output: true
  4425. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4426. *
  4427. * </script>
  4428. * ```
  4429. */
  4430. isTagNode: function(node, tagNames) {
  4431. return (
  4432. node.nodeType == 1 &&
  4433. new RegExp("\\b" + node.tagName + "\\b", "i").test(tagNames)
  4434. );
  4435. },
  4436. /**
  4437. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4438. * @method filterNodeList
  4439. * @param { Array } nodeList 需要过滤的节点数组
  4440. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4441. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4442. * @example
  4443. * ```javascript
  4444. * var divNodes = document.getElementsByTagName("div");
  4445. * divNodes = [].slice.call( divNodes, 0 );
  4446. *
  4447. * //output: null
  4448. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4449. * return node.tagName.toLowerCase() !== 'div';
  4450. * } ) );
  4451. * ```
  4452. */
  4453. /**
  4454. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4455. * @method filterNodeList
  4456. * @param { Array } nodeList 需要过滤的节点数组
  4457. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4458. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4459. * @example
  4460. * ```javascript
  4461. * var divNodes = document.getElementsByTagName("div");
  4462. * divNodes = [].slice.call( divNodes, 0 );
  4463. *
  4464. * //output: null
  4465. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4466. * ```
  4467. */
  4468. /**
  4469. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4470. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4471. * @method filterNodeList
  4472. * @param { Array } nodeList 需要过滤的节点数组
  4473. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4474. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4475. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4476. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4477. * @example
  4478. * ```javascript
  4479. * var divNodes = document.getElementsByTagName("div");
  4480. * divNodes = [].slice.call( divNodes, 0 );
  4481. *
  4482. * //output: 3(假定有3个div)
  4483. * console.log( divNodes.length );
  4484. *
  4485. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4486. * return node.tagName.toLowerCase() === 'div';
  4487. * }, true );
  4488. *
  4489. * //output: 3
  4490. * console.log( nodes.length );
  4491. *
  4492. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4493. * return node.tagName.toLowerCase() === 'div';
  4494. * }, false );
  4495. *
  4496. * //output: div
  4497. * console.log( node.nodeName );
  4498. * ```
  4499. */
  4500. filterNodeList: function(nodelist, filter, forAll) {
  4501. var results = [];
  4502. if (!utils.isFunction(filter)) {
  4503. var str = filter;
  4504. filter = function(n) {
  4505. return (
  4506. utils.indexOf(
  4507. utils.isArray(str) ? str : str.split(" "),
  4508. n.tagName.toLowerCase()
  4509. ) != -1
  4510. );
  4511. };
  4512. }
  4513. utils.each(nodelist, function(n) {
  4514. filter(n) && results.push(n);
  4515. });
  4516. return results.length == 0
  4517. ? null
  4518. : results.length == 1 || !forAll ? results[0] : results;
  4519. },
  4520. /**
  4521. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4522. * @method isInNodeEndBoundary
  4523. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4524. * @param node 需要检测的节点对象
  4525. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4526. */
  4527. isInNodeEndBoundary: function(rng, node) {
  4528. var start = rng.startContainer;
  4529. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4530. return 0;
  4531. }
  4532. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4533. return 0;
  4534. }
  4535. while (start !== node) {
  4536. if (start.nextSibling) {
  4537. return 0;
  4538. }
  4539. start = start.parentNode;
  4540. }
  4541. return 1;
  4542. },
  4543. isBoundaryNode: function(node, dir) {
  4544. var tmp;
  4545. while (!domUtils.isBody(node)) {
  4546. tmp = node;
  4547. node = node.parentNode;
  4548. if (tmp !== node[dir]) {
  4549. return false;
  4550. }
  4551. }
  4552. return true;
  4553. },
  4554. fillHtml: browser.ie11below ? "&nbsp;" : "<br/>"
  4555. });
  4556. var fillCharReg = new RegExp(domUtils.fillChar, "g");
  4557. // core/Range.js
  4558. /**
  4559. * Range封装
  4560. * @file
  4561. * @module UE.dom
  4562. * @class Range
  4563. * @since 1.2.6.1
  4564. */
  4565. /**
  4566. * dom操作封装
  4567. * @unfile
  4568. * @module UE.dom
  4569. */
  4570. /**
  4571. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4572. * @unfile
  4573. * @module UE.dom
  4574. * @class Range
  4575. */
  4576. ;(function() {
  4577. var guid = 0,
  4578. fillChar = domUtils.fillChar,
  4579. fillData;
  4580. /**
  4581. * 更新range的collapse状态
  4582. * @param {Range} range range对象
  4583. */
  4584. function updateCollapse(range) {
  4585. range.collapsed =
  4586. range.startContainer &&
  4587. range.endContainer &&
  4588. range.startContainer === range.endContainer &&
  4589. range.startOffset == range.endOffset;
  4590. }
  4591. function selectOneNode(rng) {
  4592. return (
  4593. !rng.collapsed &&
  4594. rng.startContainer.nodeType == 1 &&
  4595. rng.startContainer === rng.endContainer &&
  4596. rng.endOffset - rng.startOffset == 1
  4597. );
  4598. }
  4599. function setEndPoint(toStart, node, offset, range) {
  4600. //如果node是自闭合标签要处理
  4601. if (
  4602. node.nodeType == 1 &&
  4603. (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])
  4604. ) {
  4605. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  4606. node = node.parentNode;
  4607. }
  4608. if (toStart) {
  4609. range.startContainer = node;
  4610. range.startOffset = offset;
  4611. if (!range.endContainer) {
  4612. range.collapse(true);
  4613. }
  4614. } else {
  4615. range.endContainer = node;
  4616. range.endOffset = offset;
  4617. if (!range.startContainer) {
  4618. range.collapse(false);
  4619. }
  4620. }
  4621. updateCollapse(range);
  4622. return range;
  4623. }
  4624. function execContentsAction(range, action) {
  4625. //调整边界
  4626. //range.includeBookmark();
  4627. var start = range.startContainer,
  4628. end = range.endContainer,
  4629. startOffset = range.startOffset,
  4630. endOffset = range.endOffset,
  4631. doc = range.document,
  4632. frag = doc.createDocumentFragment(),
  4633. tmpStart,
  4634. tmpEnd;
  4635. if (start.nodeType == 1) {
  4636. start =
  4637. start.childNodes[startOffset] ||
  4638. (tmpStart = start.appendChild(doc.createTextNode("")));
  4639. }
  4640. if (end.nodeType == 1) {
  4641. end =
  4642. end.childNodes[endOffset] ||
  4643. (tmpEnd = end.appendChild(doc.createTextNode("")));
  4644. }
  4645. if (start === end && start.nodeType == 3) {
  4646. frag.appendChild(
  4647. doc.createTextNode(
  4648. start.substringData(startOffset, endOffset - startOffset)
  4649. )
  4650. );
  4651. //is not clone
  4652. if (action) {
  4653. start.deleteData(startOffset, endOffset - startOffset);
  4654. range.collapse(true);
  4655. }
  4656. return frag;
  4657. }
  4658. var current,
  4659. currentLevel,
  4660. clone = frag,
  4661. startParents = domUtils.findParents(start, true),
  4662. endParents = domUtils.findParents(end, true);
  4663. for (var i = 0; startParents[i] == endParents[i]; ) {
  4664. i++;
  4665. }
  4666. for (var j = i, si; (si = startParents[j]); j++) {
  4667. current = si.nextSibling;
  4668. if (si == start) {
  4669. if (!tmpStart) {
  4670. if (range.startContainer.nodeType == 3) {
  4671. clone.appendChild(
  4672. doc.createTextNode(start.nodeValue.slice(startOffset))
  4673. );
  4674. //is not clone
  4675. if (action) {
  4676. start.deleteData(
  4677. startOffset,
  4678. start.nodeValue.length - startOffset
  4679. );
  4680. }
  4681. } else {
  4682. clone.appendChild(!action ? start.cloneNode(true) : start);
  4683. }
  4684. }
  4685. } else {
  4686. currentLevel = si.cloneNode(false);
  4687. clone.appendChild(currentLevel);
  4688. }
  4689. while (current) {
  4690. if (current === end || current === endParents[j]) {
  4691. break;
  4692. }
  4693. si = current.nextSibling;
  4694. clone.appendChild(!action ? current.cloneNode(true) : current);
  4695. current = si;
  4696. }
  4697. clone = currentLevel;
  4698. }
  4699. clone = frag;
  4700. if (!startParents[i]) {
  4701. clone.appendChild(startParents[i - 1].cloneNode(false));
  4702. clone = clone.firstChild;
  4703. }
  4704. for (var j = i, ei; (ei = endParents[j]); j++) {
  4705. current = ei.previousSibling;
  4706. if (ei == end) {
  4707. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4708. clone.appendChild(
  4709. doc.createTextNode(end.substringData(0, endOffset))
  4710. );
  4711. //is not clone
  4712. if (action) {
  4713. end.deleteData(0, endOffset);
  4714. }
  4715. }
  4716. } else {
  4717. currentLevel = ei.cloneNode(false);
  4718. clone.appendChild(currentLevel);
  4719. }
  4720. //如果两端同级,右边第一次已经被开始做了
  4721. if (j != i || !startParents[i]) {
  4722. while (current) {
  4723. if (current === start) {
  4724. break;
  4725. }
  4726. ei = current.previousSibling;
  4727. clone.insertBefore(
  4728. !action ? current.cloneNode(true) : current,
  4729. clone.firstChild
  4730. );
  4731. current = ei;
  4732. }
  4733. }
  4734. clone = currentLevel;
  4735. }
  4736. if (action) {
  4737. range
  4738. .setStartBefore(
  4739. !endParents[i]
  4740. ? endParents[i - 1]
  4741. : !startParents[i] ? startParents[i - 1] : endParents[i]
  4742. )
  4743. .collapse(true);
  4744. }
  4745. tmpStart && domUtils.remove(tmpStart);
  4746. tmpEnd && domUtils.remove(tmpEnd);
  4747. return frag;
  4748. }
  4749. /**
  4750. * 创建一个跟document绑定的空的Range实例
  4751. * @constructor
  4752. * @param { Document } document 新建的选区所属的文档对象
  4753. */
  4754. /**
  4755. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4756. */
  4757. /**
  4758. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4759. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4760. */
  4761. /**
  4762. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4763. */
  4764. /**
  4765. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4766. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4767. */
  4768. /**
  4769. * @property { Boolean } collapsed 当前Range是否闭合
  4770. * @default true
  4771. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4772. */
  4773. /**
  4774. * @property { Document } document 当前Range所属的Document对象
  4775. * @remind 不同range的的document属性可以是不同的
  4776. */
  4777. var Range = (dom.Range = function(document) {
  4778. var me = this;
  4779. me.startContainer = me.startOffset = me.endContainer = me.endOffset = null;
  4780. me.document = document;
  4781. me.collapsed = true;
  4782. });
  4783. /**
  4784. * 删除fillData
  4785. * @param doc
  4786. * @param excludeNode
  4787. */
  4788. function removeFillData(doc, excludeNode) {
  4789. try {
  4790. if (fillData && domUtils.inDoc(fillData, doc)) {
  4791. if (!fillData.nodeValue.replace(fillCharReg, "").length) {
  4792. var tmpNode = fillData.parentNode;
  4793. domUtils.remove(fillData);
  4794. while (
  4795. tmpNode &&
  4796. domUtils.isEmptyInlineElement(tmpNode) &&
  4797. //safari的contains有bug
  4798. (browser.safari
  4799. ? !(
  4800. domUtils.getPosition(tmpNode, excludeNode) &
  4801. domUtils.POSITION_CONTAINS
  4802. )
  4803. : !tmpNode.contains(excludeNode))
  4804. ) {
  4805. fillData = tmpNode.parentNode;
  4806. domUtils.remove(tmpNode);
  4807. tmpNode = fillData;
  4808. }
  4809. } else {
  4810. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, "");
  4811. }
  4812. }
  4813. } catch (e) {}
  4814. }
  4815. /**
  4816. * @param node
  4817. * @param dir
  4818. */
  4819. function mergeSibling(node, dir) {
  4820. var tmpNode;
  4821. node = node[dir];
  4822. while (node && domUtils.isFillChar(node)) {
  4823. tmpNode = node[dir];
  4824. domUtils.remove(node);
  4825. node = tmpNode;
  4826. }
  4827. }
  4828. Range.prototype = {
  4829. /**
  4830. * 克隆选区的内容到一个DocumentFragment里
  4831. * @method cloneContents
  4832. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4833. * @example
  4834. * ```html
  4835. * <body>
  4836. * <!-- 中括号表示选区 -->
  4837. * <b>x<i>x[x</i>xx]x</b>
  4838. *
  4839. * <script>
  4840. * //range是已选中的选区
  4841. * var fragment = range.cloneContents(),
  4842. * node = document.createElement("div");
  4843. *
  4844. * node.appendChild( fragment );
  4845. *
  4846. * //output: <i>x</i>xx
  4847. * console.log( node.innerHTML );
  4848. *
  4849. * </script>
  4850. * </body>
  4851. * ```
  4852. */
  4853. cloneContents: function() {
  4854. return this.collapsed ? null : execContentsAction(this, 0);
  4855. },
  4856. /**
  4857. * 删除当前选区范围中的所有内容
  4858. * @method deleteContents
  4859. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4860. * @return { UE.dom.Range } 当前操作的Range对象
  4861. * @example
  4862. * ```html
  4863. * <body>
  4864. * <!-- 中括号表示选区 -->
  4865. * <b>x<i>x[x</i>xx]x</b>
  4866. *
  4867. * <script>
  4868. * //range是已选中的选区
  4869. * range.deleteContents();
  4870. *
  4871. * //竖线表示闭合后的选区位置
  4872. * //output: <b>x<i>x</i>|x</b>
  4873. * console.log( document.body.innerHTML );
  4874. *
  4875. * //此时, range的各项属性为
  4876. * //output: B
  4877. * console.log( range.startContainer.tagName );
  4878. * //output: 2
  4879. * console.log( range.startOffset );
  4880. * //output: B
  4881. * console.log( range.endContainer.tagName );
  4882. * //output: 2
  4883. * console.log( range.endOffset );
  4884. * //output: true
  4885. * console.log( range.collapsed );
  4886. *
  4887. * </script>
  4888. * </body>
  4889. * ```
  4890. */
  4891. deleteContents: function() {
  4892. var txt;
  4893. if (!this.collapsed) {
  4894. execContentsAction(this, 1);
  4895. }
  4896. if (browser.webkit) {
  4897. txt = this.startContainer;
  4898. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4899. this.setStartBefore(txt).collapse(true);
  4900. domUtils.remove(txt);
  4901. }
  4902. }
  4903. return this;
  4904. },
  4905. /**
  4906. * 将当前选区的内容提取到一个DocumentFragment里
  4907. * @method extractContents
  4908. * @remind 执行该操作后, 选区将变成闭合状态
  4909. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4910. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4911. * @example
  4912. * ```html
  4913. * <body>
  4914. * <!-- 中括号表示选区 -->
  4915. * <b>x<i>x[x</i>xx]x</b>
  4916. *
  4917. * <script>
  4918. * //range是已选中的选区
  4919. * var fragment = range.extractContents(),
  4920. * node = document.createElement( "div" );
  4921. *
  4922. * node.appendChild( fragment );
  4923. *
  4924. * //竖线表示闭合后的选区位置
  4925. *
  4926. * //output: <b>x<i>x</i>|x</b>
  4927. * console.log( document.body.innerHTML );
  4928. * //output: <i>x</i>xx
  4929. * console.log( node.innerHTML );
  4930. *
  4931. * //此时, range的各项属性为
  4932. * //output: B
  4933. * console.log( range.startContainer.tagName );
  4934. * //output: 2
  4935. * console.log( range.startOffset );
  4936. * //output: B
  4937. * console.log( range.endContainer.tagName );
  4938. * //output: 2
  4939. * console.log( range.endOffset );
  4940. * //output: true
  4941. * console.log( range.collapsed );
  4942. *
  4943. * </script>
  4944. * </body>
  4945. */
  4946. extractContents: function() {
  4947. return this.collapsed ? null : execContentsAction(this, 2);
  4948. },
  4949. /**
  4950. * 设置Range的开始容器节点和偏移量
  4951. * @method setStart
  4952. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  4953. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  4954. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  4955. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  4956. * 中的索引
  4957. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  4958. * @param { int } offset 选区的开始位置偏移量
  4959. * @return { UE.dom.Range } 当前range对象
  4960. * @example
  4961. * ```html
  4962. * <!-- 选区 -->
  4963. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  4964. *
  4965. * <script>
  4966. *
  4967. * //执行操作
  4968. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  4969. *
  4970. * //此时, 选区变成了
  4971. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  4972. *
  4973. * </script>
  4974. * ```
  4975. * @example
  4976. * ```html
  4977. * <!-- 选区 -->
  4978. * <b>xxx<img>[xx]x</b>
  4979. *
  4980. * <script>
  4981. *
  4982. * //执行操作
  4983. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  4984. *
  4985. * //此时, 选区变成了
  4986. * //<b>xxx[<img>xx]x</b>
  4987. *
  4988. * </script>
  4989. * ```
  4990. */
  4991. setStart: function(node, offset) {
  4992. return setEndPoint(true, node, offset, this);
  4993. },
  4994. /**
  4995. * 设置Range的结束容器和偏移量
  4996. * @method setEnd
  4997. * @param { Node } node 作为当前选区结束边界容器的节点对象
  4998. * @param { int } offset 结束边界的偏移量
  4999. * @see UE.dom.Range:setStart(Node,int)
  5000. * @return { UE.dom.Range } 当前range对象
  5001. */
  5002. setEnd: function(node, offset) {
  5003. return setEndPoint(false, node, offset, this);
  5004. },
  5005. /**
  5006. * 将Range开始位置设置到node节点之后
  5007. * @method setStartAfter
  5008. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  5009. * @param { Node } node 选区的开始边界将紧接着该节点之后
  5010. * @return { UE.dom.Range } 当前range对象
  5011. * @example
  5012. * ```html
  5013. * <!-- 选区示例 -->
  5014. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  5015. *
  5016. * <script>
  5017. *
  5018. * //执行操作
  5019. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  5020. *
  5021. * //结果选区
  5022. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  5023. *
  5024. * </script>
  5025. * ```
  5026. */
  5027. setStartAfter: function(node) {
  5028. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  5029. },
  5030. /**
  5031. * 将Range开始位置设置到node节点之前
  5032. * @method setStartBefore
  5033. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  5034. * @param { Node } node 新的选区开始位置在该节点之前
  5035. * @see UE.dom.Range:setStartAfter(Node)
  5036. * @return { UE.dom.Range } 当前range对象
  5037. */
  5038. setStartBefore: function(node) {
  5039. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  5040. },
  5041. /**
  5042. * 将Range结束位置设置到node节点之后
  5043. * @method setEndAfter
  5044. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  5045. * @param { Node } node 目标节点
  5046. * @see UE.dom.Range:setStartAfter(Node)
  5047. * @return { UE.dom.Range } 当前range对象
  5048. * @example
  5049. * ```html
  5050. * <!-- 选区示例 -->
  5051. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  5052. *
  5053. * <script>
  5054. *
  5055. * //执行操作
  5056. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  5057. *
  5058. * //结果选区
  5059. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  5060. *
  5061. * </script>
  5062. * ```
  5063. */
  5064. setEndAfter: function(node) {
  5065. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  5066. },
  5067. /**
  5068. * 将Range结束位置设置到node节点之前
  5069. * @method setEndBefore
  5070. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  5071. * @param { Node } node 目标节点
  5072. * @see UE.dom.Range:setEndAfter(Node)
  5073. * @return { UE.dom.Range } 当前range对象
  5074. */
  5075. setEndBefore: function(node) {
  5076. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  5077. },
  5078. /**
  5079. * 设置Range的开始位置到node节点内的第一个子节点之前
  5080. * @method setStartAtFirst
  5081. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  5082. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5083. * @param { Node } node 目标节点
  5084. * @see UE.dom.Range:setStartBefore(Node)
  5085. * @return { UE.dom.Range } 当前range对象
  5086. * @example
  5087. * ```html
  5088. * <!-- 选区示例 -->
  5089. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5090. *
  5091. * <script>
  5092. *
  5093. * //执行操作
  5094. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  5095. *
  5096. * //结果选区
  5097. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  5098. *
  5099. * </script>
  5100. * ```
  5101. */
  5102. setStartAtFirst: function(node) {
  5103. return this.setStart(node, 0);
  5104. },
  5105. /**
  5106. * 设置Range的开始位置到node节点内的最后一个节点之后
  5107. * @method setStartAtLast
  5108. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  5109. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5110. * @param { Node } node 目标节点
  5111. * @see UE.dom.Range:setStartAtFirst(Node)
  5112. * @return { UE.dom.Range } 当前range对象
  5113. */
  5114. setStartAtLast: function(node) {
  5115. return this.setStart(
  5116. node,
  5117. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5118. );
  5119. },
  5120. /**
  5121. * 设置Range的结束位置到node节点内的第一个节点之前
  5122. * @method setEndAtFirst
  5123. * @param { Node } node 目标节点
  5124. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  5125. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5126. * @see UE.dom.Range:setStartAtFirst(Node)
  5127. * @return { UE.dom.Range } 当前range对象
  5128. */
  5129. setEndAtFirst: function(node) {
  5130. return this.setEnd(node, 0);
  5131. },
  5132. /**
  5133. * 设置Range的结束位置到node节点内的最后一个节点之后
  5134. * @method setEndAtLast
  5135. * @param { Node } node 目标节点
  5136. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  5137. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5138. * @see UE.dom.Range:setStartAtFirst(Node)
  5139. * @return { UE.dom.Range } 当前range对象
  5140. */
  5141. setEndAtLast: function(node) {
  5142. return this.setEnd(
  5143. node,
  5144. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5145. );
  5146. },
  5147. /**
  5148. * 选中给定节点
  5149. * @method selectNode
  5150. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5151. * 而endOffset为startOffset+1
  5152. * @param { Node } node 需要选中的节点
  5153. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5154. * @example
  5155. * ```html
  5156. * <!-- 选区示例 -->
  5157. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5158. *
  5159. * <script>
  5160. *
  5161. * //执行操作
  5162. * range.selectNode( document.getElementsByTagName("i")[0] );
  5163. *
  5164. * //结果选区
  5165. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5166. *
  5167. * </script>
  5168. * ```
  5169. */
  5170. selectNode: function(node) {
  5171. return this.setStartBefore(node).setEndAfter(node);
  5172. },
  5173. /**
  5174. * 选中给定节点内部的所有节点
  5175. * @method selectNodeContents
  5176. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5177. * 而endOffset是该节点的子节点数。
  5178. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5179. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5180. * @example
  5181. * ```html
  5182. * <!-- 选区示例 -->
  5183. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5184. *
  5185. * <script>
  5186. *
  5187. * //执行操作
  5188. * range.selectNode( document.getElementsByTagName("b")[0] );
  5189. *
  5190. * //结果选区
  5191. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5192. *
  5193. * </script>
  5194. * ```
  5195. */
  5196. selectNodeContents: function(node) {
  5197. return this.setStart(node, 0).setEndAtLast(node);
  5198. },
  5199. /**
  5200. * clone当前Range对象
  5201. * @method cloneRange
  5202. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5203. * @return { UE.dom.Range } 当前range对象的一个副本
  5204. */
  5205. cloneRange: function() {
  5206. var me = this;
  5207. return new Range(me.document)
  5208. .setStart(me.startContainer, me.startOffset)
  5209. .setEnd(me.endContainer, me.endOffset);
  5210. },
  5211. /**
  5212. * 向当前选区的结束处闭合选区
  5213. * @method collapse
  5214. * @return { UE.dom.Range } 当前range对象
  5215. * @example
  5216. * ```html
  5217. * <!-- 选区示例 -->
  5218. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5219. *
  5220. * <script>
  5221. *
  5222. * //执行操作
  5223. * range.collapse();
  5224. *
  5225. * //结果选区
  5226. * //“|”表示选区已闭合
  5227. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5228. *
  5229. * </script>
  5230. * ```
  5231. */
  5232. /**
  5233. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5234. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5235. * @method collapse
  5236. * @param { Boolean } toStart 是否向选区开始处闭合
  5237. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5238. * @see UE.dom.Range:collapse()
  5239. * @example
  5240. * ```html
  5241. * <!-- 选区示例 -->
  5242. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5243. *
  5244. * <script>
  5245. *
  5246. * //执行操作
  5247. * range.collapse( true );
  5248. *
  5249. * //结果选区
  5250. * //“|”表示选区已闭合
  5251. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5252. *
  5253. * </script>
  5254. * ```
  5255. */
  5256. collapse: function(toStart) {
  5257. var me = this;
  5258. if (toStart) {
  5259. me.endContainer = me.startContainer;
  5260. me.endOffset = me.startOffset;
  5261. } else {
  5262. me.startContainer = me.endContainer;
  5263. me.startOffset = me.endOffset;
  5264. }
  5265. me.collapsed = true;
  5266. return me;
  5267. },
  5268. /**
  5269. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5270. * @method shrinkBoundary
  5271. * @return { UE.dom.Range } 当前range对象
  5272. * @example
  5273. * ```html
  5274. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5275. * ```
  5276. *
  5277. * @example
  5278. * ```html
  5279. * <!-- 选区示例 -->
  5280. * <b>x[xx</b><i>]xxx</i>
  5281. *
  5282. * <script>
  5283. *
  5284. * //执行收缩
  5285. * range.shrinkBoundary();
  5286. *
  5287. * //结果选区
  5288. * //<b>x[xx]</b><i>xxx</i>
  5289. * </script>
  5290. * ```
  5291. *
  5292. * @example
  5293. * ```html
  5294. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5295. * ```
  5296. */
  5297. /**
  5298. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5299. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5300. * @method shrinkBoundary
  5301. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5302. * @return { UE.dom.Range } 当前range对象
  5303. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5304. */
  5305. shrinkBoundary: function(ignoreEnd) {
  5306. var me = this,
  5307. child,
  5308. collapsed = me.collapsed;
  5309. function check(node) {
  5310. return (
  5311. node.nodeType == 1 &&
  5312. !domUtils.isBookmarkNode(node) &&
  5313. !dtd.$empty[node.tagName] &&
  5314. !dtd.$nonChild[node.tagName]
  5315. );
  5316. }
  5317. while (
  5318. me.startContainer.nodeType == 1 && //是element
  5319. (child = me.startContainer.childNodes[me.startOffset]) && //子节点也是element
  5320. check(child)
  5321. ) {
  5322. me.setStart(child, 0);
  5323. }
  5324. if (collapsed) {
  5325. return me.collapse(true);
  5326. }
  5327. if (!ignoreEnd) {
  5328. while (
  5329. me.endContainer.nodeType == 1 && //是element
  5330. me.endOffset > 0 && //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5331. (child = me.endContainer.childNodes[me.endOffset - 1]) && //子节点也是element
  5332. check(child)
  5333. ) {
  5334. me.setEnd(child, child.childNodes.length);
  5335. }
  5336. }
  5337. return me;
  5338. },
  5339. /**
  5340. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5341. * @method getCommonAncestor
  5342. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5343. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5344. * @example
  5345. * ```html
  5346. * //选区示例
  5347. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5348. * <script>
  5349. *
  5350. * var node = range.getCommonAncestor();
  5351. *
  5352. * //公共祖先节点是: b节点
  5353. * //输出: B
  5354. * console.log(node.tagName);
  5355. *
  5356. * </script>
  5357. * ```
  5358. */
  5359. /**
  5360. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5361. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5362. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5363. * @method getCommonAncestor
  5364. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5365. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5366. * @see UE.dom.Range:getCommonAncestor()
  5367. * @example
  5368. * ```html
  5369. * <body>
  5370. *
  5371. * <!-- 选区示例 -->
  5372. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5373. *
  5374. * <script>
  5375. *
  5376. * var node = range.getCommonAncestor( false );
  5377. *
  5378. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5379. * //output: B
  5380. * console.log( node.tagName );
  5381. *
  5382. * </script>
  5383. *
  5384. * </body>
  5385. * ```
  5386. */
  5387. /**
  5388. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5389. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5390. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5391. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5392. * @method getCommonAncestor
  5393. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5394. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5395. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5396. * @see UE.dom.Range:getCommonAncestor()
  5397. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5398. * @example
  5399. * ```html
  5400. * <body>
  5401. *
  5402. * <!-- 选区示例 -->
  5403. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5404. *
  5405. * <script>
  5406. *
  5407. * var node = range.getCommonAncestor( true, false );
  5408. *
  5409. * //output: SPAN
  5410. * console.log( node.tagName );
  5411. *
  5412. * </script>
  5413. *
  5414. * </body>
  5415. * ```
  5416. */
  5417. getCommonAncestor: function(includeSelf, ignoreTextNode) {
  5418. var me = this,
  5419. start = me.startContainer,
  5420. end = me.endContainer;
  5421. if (start === end) {
  5422. if (includeSelf && selectOneNode(this)) {
  5423. start = start.childNodes[me.startOffset];
  5424. if (start.nodeType == 1) return start;
  5425. }
  5426. //只有在上来就相等的情况下才会出现是文本的情况
  5427. return ignoreTextNode && start.nodeType == 3 ? start.parentNode : start;
  5428. }
  5429. return domUtils.getCommonAncestor(start, end);
  5430. },
  5431. /**
  5432. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5433. * @method trimBoundary
  5434. * @remind 该操作有可能会引起文本节点被切开
  5435. * @return { UE.dom.Range } 当前range对象
  5436. * @example
  5437. * ```html
  5438. *
  5439. * //选区示例
  5440. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5441. *
  5442. * <script>
  5443. * //未调整前, 选区的开始容器和结束都是文本节点
  5444. * //执行调整
  5445. * range.trimBoundary();
  5446. *
  5447. * //调整之后, 容器节点变成了i节点
  5448. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5449. * </script>
  5450. * ```
  5451. */
  5452. /**
  5453. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5454. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5455. * @method trimBoundary
  5456. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5457. * @return { UE.dom.Range } 当前range对象
  5458. * @example
  5459. * ```html
  5460. *
  5461. * //选区示例
  5462. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5463. *
  5464. * <script>
  5465. * //未调整前, 选区的开始容器和结束都是文本节点
  5466. * //执行调整
  5467. * range.trimBoundary( true );
  5468. *
  5469. * //调整之后, 开始容器节点变成了i节点
  5470. * //但是, 结束容器没有发生变化
  5471. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5472. * </script>
  5473. * ```
  5474. */
  5475. trimBoundary: function(ignoreEnd) {
  5476. this.txtToElmBoundary();
  5477. var start = this.startContainer,
  5478. offset = this.startOffset,
  5479. collapsed = this.collapsed,
  5480. end = this.endContainer;
  5481. if (start.nodeType == 3) {
  5482. if (offset == 0) {
  5483. this.setStartBefore(start);
  5484. } else {
  5485. if (offset >= start.nodeValue.length) {
  5486. this.setStartAfter(start);
  5487. } else {
  5488. var textNode = domUtils.split(start, offset);
  5489. //跟新结束边界
  5490. if (start === end) {
  5491. this.setEnd(textNode, this.endOffset - offset);
  5492. } else if (start.parentNode === end) {
  5493. this.endOffset += 1;
  5494. }
  5495. this.setStartBefore(textNode);
  5496. }
  5497. }
  5498. if (collapsed) {
  5499. return this.collapse(true);
  5500. }
  5501. }
  5502. if (!ignoreEnd) {
  5503. offset = this.endOffset;
  5504. end = this.endContainer;
  5505. if (end.nodeType == 3) {
  5506. if (offset == 0) {
  5507. this.setEndBefore(end);
  5508. } else {
  5509. offset < end.nodeValue.length && domUtils.split(end, offset);
  5510. this.setEndAfter(end);
  5511. }
  5512. }
  5513. }
  5514. return this;
  5515. },
  5516. /**
  5517. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5518. * @method txtToElmBoundary
  5519. * @remind 该操作不会修改dom节点
  5520. * @return { UE.dom.Range } 当前range对象
  5521. */
  5522. /**
  5523. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5524. * ignoreCollapsed 的值决定是否执行该调整
  5525. * @method txtToElmBoundary
  5526. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5527. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5528. * @return { UE.dom.Range } 当前range对象
  5529. */
  5530. txtToElmBoundary: function(ignoreCollapsed) {
  5531. function adjust(r, c) {
  5532. var container = r[c + "Container"],
  5533. offset = r[c + "Offset"];
  5534. if (container.nodeType == 3) {
  5535. if (!offset) {
  5536. r[
  5537. "set" +
  5538. c.replace(/(\w)/, function(a) {
  5539. return a.toUpperCase();
  5540. }) +
  5541. "Before"
  5542. ](container);
  5543. } else if (offset >= container.nodeValue.length) {
  5544. r[
  5545. "set" +
  5546. c.replace(/(\w)/, function(a) {
  5547. return a.toUpperCase();
  5548. }) +
  5549. "After"
  5550. ](container);
  5551. }
  5552. }
  5553. }
  5554. if (ignoreCollapsed || !this.collapsed) {
  5555. adjust(this, "start");
  5556. adjust(this, "end");
  5557. }
  5558. return this;
  5559. },
  5560. /**
  5561. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5562. * @method insertNode
  5563. * @param { Node } node 需要插入的节点
  5564. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5565. * @return { UE.dom.Range } 当前range对象
  5566. */
  5567. insertNode: function(node) {
  5568. var first = node,
  5569. length = 1;
  5570. if (node.nodeType == 11) {
  5571. first = node.firstChild;
  5572. length = node.childNodes.length;
  5573. }
  5574. this.trimBoundary(true);
  5575. var start = this.startContainer,
  5576. offset = this.startOffset;
  5577. var nextNode = start.childNodes[offset];
  5578. if (nextNode) {
  5579. start.insertBefore(node, nextNode);
  5580. } else {
  5581. start.appendChild(node);
  5582. }
  5583. if (first.parentNode === this.endContainer) {
  5584. this.endOffset = this.endOffset + length;
  5585. }
  5586. return this.setStartBefore(first);
  5587. },
  5588. /**
  5589. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5590. * @method setCursor
  5591. * @return { UE.dom.Range } 当前range对象
  5592. * @see UE.dom.Range:collapse()
  5593. */
  5594. /**
  5595. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5596. * @method setCursor
  5597. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5598. * 反之,则向开始容器方向闭合
  5599. * @return { UE.dom.Range } 当前range对象
  5600. * @see UE.dom.Range:collapse(Boolean)
  5601. */
  5602. setCursor: function(toEnd, noFillData) {
  5603. return this.collapse(!toEnd).select(noFillData);
  5604. },
  5605. /**
  5606. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5607. * @method createBookmark
  5608. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5609. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5610. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5611. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5612. * 返回的记录的类型为ID, 反之则为引用
  5613. */
  5614. createBookmark: function(serialize, same) {
  5615. var endNode,
  5616. startNode = this.document.createElement("span");
  5617. startNode.style.cssText = "display:none;line-height:0px;";
  5618. startNode.appendChild(this.document.createTextNode("\u200D"));
  5619. startNode.id = "_baidu_bookmark_start_" + (same ? "" : guid++);
  5620. if (!this.collapsed) {
  5621. endNode = startNode.cloneNode(true);
  5622. endNode.id = "_baidu_bookmark_end_" + (same ? "" : guid++);
  5623. }
  5624. this.insertNode(startNode);
  5625. if (endNode) {
  5626. this.collapse().insertNode(endNode).setEndBefore(endNode);
  5627. }
  5628. this.setStartAfter(startNode);
  5629. return {
  5630. start: serialize ? startNode.id : startNode,
  5631. end: endNode ? (serialize ? endNode.id : endNode) : null,
  5632. id: serialize
  5633. };
  5634. },
  5635. /**
  5636. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5637. * @method moveToBookmark
  5638. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5639. * @return { UE.dom.Range } 当前range对象
  5640. * @see UE.dom.Range:createBookmark(Boolean)
  5641. */
  5642. moveToBookmark: function(bookmark) {
  5643. var start = bookmark.id
  5644. ? this.document.getElementById(bookmark.start)
  5645. : bookmark.start,
  5646. end = bookmark.end && bookmark.id
  5647. ? this.document.getElementById(bookmark.end)
  5648. : bookmark.end;
  5649. this.setStartBefore(start);
  5650. domUtils.remove(start);
  5651. if (end) {
  5652. this.setEndBefore(end);
  5653. domUtils.remove(end);
  5654. } else {
  5655. this.collapse(true);
  5656. }
  5657. return this;
  5658. },
  5659. /**
  5660. * 调整range的边界,使其"放大"到最近的父节点
  5661. * @method enlarge
  5662. * @remind 会引起选区的变化
  5663. * @return { UE.dom.Range } 当前range对象
  5664. */
  5665. /**
  5666. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5667. * 要求扩大之后的父节点是block节点
  5668. * @method enlarge
  5669. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5670. * @return { UE.dom.Range } 当前range对象
  5671. */
  5672. enlarge: function(toBlock, stopFn) {
  5673. var isBody = domUtils.isBody,
  5674. pre,
  5675. node,
  5676. tmp = this.document.createTextNode("");
  5677. if (toBlock) {
  5678. node = this.startContainer;
  5679. if (node.nodeType == 1) {
  5680. if (node.childNodes[this.startOffset]) {
  5681. pre = node = node.childNodes[this.startOffset];
  5682. } else {
  5683. node.appendChild(tmp);
  5684. pre = node = tmp;
  5685. }
  5686. } else {
  5687. pre = node;
  5688. }
  5689. while (1) {
  5690. if (domUtils.isBlockElm(node)) {
  5691. node = pre;
  5692. while ((pre = node.previousSibling) && !domUtils.isBlockElm(pre)) {
  5693. node = pre;
  5694. }
  5695. this.setStartBefore(node);
  5696. break;
  5697. }
  5698. pre = node;
  5699. node = node.parentNode;
  5700. }
  5701. node = this.endContainer;
  5702. if (node.nodeType == 1) {
  5703. if ((pre = node.childNodes[this.endOffset])) {
  5704. node.insertBefore(tmp, pre);
  5705. } else {
  5706. node.appendChild(tmp);
  5707. }
  5708. pre = node = tmp;
  5709. } else {
  5710. pre = node;
  5711. }
  5712. while (1) {
  5713. if (domUtils.isBlockElm(node)) {
  5714. node = pre;
  5715. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5716. node = pre;
  5717. }
  5718. this.setEndAfter(node);
  5719. break;
  5720. }
  5721. pre = node;
  5722. node = node.parentNode;
  5723. }
  5724. if (tmp.parentNode === this.endContainer) {
  5725. this.endOffset--;
  5726. }
  5727. domUtils.remove(tmp);
  5728. }
  5729. // 扩展边界到最大
  5730. if (!this.collapsed) {
  5731. while (this.startOffset == 0) {
  5732. if (stopFn && stopFn(this.startContainer)) {
  5733. break;
  5734. }
  5735. if (isBody(this.startContainer)) {
  5736. break;
  5737. }
  5738. this.setStartBefore(this.startContainer);
  5739. }
  5740. while (
  5741. this.endOffset ==
  5742. (this.endContainer.nodeType == 1
  5743. ? this.endContainer.childNodes.length
  5744. : this.endContainer.nodeValue.length)
  5745. ) {
  5746. if (stopFn && stopFn(this.endContainer)) {
  5747. break;
  5748. }
  5749. if (isBody(this.endContainer)) {
  5750. break;
  5751. }
  5752. this.setEndAfter(this.endContainer);
  5753. }
  5754. }
  5755. return this;
  5756. },
  5757. enlargeToBlockElm: function(ignoreEnd) {
  5758. while (!domUtils.isBlockElm(this.startContainer)) {
  5759. this.setStartBefore(this.startContainer);
  5760. }
  5761. if (!ignoreEnd) {
  5762. while (!domUtils.isBlockElm(this.endContainer)) {
  5763. this.setEndAfter(this.endContainer);
  5764. }
  5765. }
  5766. return this;
  5767. },
  5768. /**
  5769. * 调整Range的边界,使其"缩小"到最合适的位置
  5770. * @method adjustmentBoundary
  5771. * @return { UE.dom.Range } 当前range对象
  5772. * @see UE.dom.Range:shrinkBoundary()
  5773. */
  5774. adjustmentBoundary: function() {
  5775. if (!this.collapsed) {
  5776. while (
  5777. !domUtils.isBody(this.startContainer) &&
  5778. this.startOffset ==
  5779. this.startContainer[
  5780. this.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5781. ].length &&
  5782. this.startContainer[
  5783. this.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5784. ].length
  5785. ) {
  5786. this.setStartAfter(this.startContainer);
  5787. }
  5788. while (
  5789. !domUtils.isBody(this.endContainer) &&
  5790. !this.endOffset &&
  5791. this.endContainer[
  5792. this.endContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5793. ].length
  5794. ) {
  5795. this.setEndBefore(this.endContainer);
  5796. }
  5797. }
  5798. return this;
  5799. },
  5800. /**
  5801. * 给range选区中的内容添加给定的inline标签
  5802. * @method applyInlineStyle
  5803. * @param { String } tagName 需要添加的标签名
  5804. * @example
  5805. * ```html
  5806. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5807. * ```
  5808. */
  5809. /**
  5810. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5811. * @method applyInlineStyle
  5812. * @param { String } tagName 需要添加的标签名
  5813. * @param { Object } attrs 跟随新添加的标签的属性
  5814. * @return { UE.dom.Range } 当前选区
  5815. * @example
  5816. * ```html
  5817. * <p>xxxx[xxxx]x</p>
  5818. *
  5819. * ==>
  5820. *
  5821. * <!-- 执行操作 -->
  5822. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5823. *
  5824. * ==>
  5825. *
  5826. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5827. * ```
  5828. */
  5829. applyInlineStyle: function(tagName, attrs, list) {
  5830. if (this.collapsed) return this;
  5831. this.trimBoundary()
  5832. .enlarge(false, function(node) {
  5833. return node.nodeType == 1 && domUtils.isBlockElm(node);
  5834. })
  5835. .adjustmentBoundary();
  5836. var bookmark = this.createBookmark(),
  5837. end = bookmark.end,
  5838. filterFn = function(node) {
  5839. return node.nodeType == 1
  5840. ? node.tagName.toLowerCase() != "br"
  5841. : !domUtils.isWhitespace(node);
  5842. },
  5843. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5844. node,
  5845. pre,
  5846. range = this.cloneRange();
  5847. while (
  5848. current &&
  5849. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  5850. ) {
  5851. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5852. range.setStartBefore(current);
  5853. node = current;
  5854. while (
  5855. node &&
  5856. (node.nodeType == 3 || dtd[tagName][node.tagName]) &&
  5857. node !== end
  5858. ) {
  5859. pre = node;
  5860. node = domUtils.getNextDomNode(
  5861. node,
  5862. node.nodeType == 1,
  5863. null,
  5864. function(parent) {
  5865. return dtd[tagName][parent.tagName];
  5866. }
  5867. );
  5868. }
  5869. var frag = range.setEndAfter(pre).extractContents(),
  5870. elm;
  5871. if (list && list.length > 0) {
  5872. var level, top;
  5873. top = level = list[0].cloneNode(false);
  5874. for (var i = 1, ci; (ci = list[i++]); ) {
  5875. level.appendChild(ci.cloneNode(false));
  5876. level = level.firstChild;
  5877. }
  5878. elm = level;
  5879. } else {
  5880. elm = range.document.createElement(tagName);
  5881. }
  5882. if (attrs) {
  5883. domUtils.setAttributes(elm, attrs);
  5884. }
  5885. elm.appendChild(frag);
  5886. //针对嵌套span的全局样式指定,做容错处理
  5887. if (elm.tagName == "SPAN" && attrs && attrs.style) {
  5888. utils.each(elm.getElementsByTagName("span"), function(s) {
  5889. s.style.cssText = s.style.cssText + ";" + attrs.style;
  5890. });
  5891. }
  5892. range.insertNode(list ? top : elm);
  5893. //处理下滑线在a上的情况
  5894. var aNode;
  5895. if (
  5896. tagName == "span" &&
  5897. attrs.style &&
  5898. /text\-decoration/.test(attrs.style) &&
  5899. (aNode = domUtils.findParentByTagName(elm, "a", true))
  5900. ) {
  5901. domUtils.setAttributes(aNode, attrs);
  5902. domUtils.remove(elm, true);
  5903. elm = aNode;
  5904. } else {
  5905. domUtils.mergeSibling(elm);
  5906. domUtils.clearEmptySibling(elm);
  5907. }
  5908. //去除子节点相同的
  5909. domUtils.mergeChild(elm, attrs);
  5910. current = domUtils.getNextDomNode(elm, false, filterFn);
  5911. domUtils.mergeToParent(elm);
  5912. if (node === end) {
  5913. break;
  5914. }
  5915. } else {
  5916. current = domUtils.getNextDomNode(current, true, filterFn);
  5917. }
  5918. }
  5919. return this.moveToBookmark(bookmark);
  5920. },
  5921. /**
  5922. * 移除当前选区内指定的inline标签,但保留其中的内容
  5923. * @method removeInlineStyle
  5924. * @param { String } tagName 需要移除的标签名
  5925. * @return { UE.dom.Range } 当前的range对象
  5926. * @example
  5927. * ```html
  5928. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5929. * ```
  5930. */
  5931. /**
  5932. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5933. * @method removeInlineStyle
  5934. * @param { Array } tagNameArr 需要移除的标签名的数组
  5935. * @return { UE.dom.Range } 当前的range对象
  5936. * @see UE.dom.Range:removeInlineStyle(String)
  5937. */
  5938. removeInlineStyle: function(tagNames) {
  5939. if (this.collapsed) return this;
  5940. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames];
  5941. this.shrinkBoundary().adjustmentBoundary();
  5942. var start = this.startContainer,
  5943. end = this.endContainer;
  5944. while (1) {
  5945. if (start.nodeType == 1) {
  5946. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  5947. break;
  5948. }
  5949. if (start.tagName.toLowerCase() == "body") {
  5950. start = null;
  5951. break;
  5952. }
  5953. }
  5954. start = start.parentNode;
  5955. }
  5956. while (1) {
  5957. if (end.nodeType == 1) {
  5958. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  5959. break;
  5960. }
  5961. if (end.tagName.toLowerCase() == "body") {
  5962. end = null;
  5963. break;
  5964. }
  5965. }
  5966. end = end.parentNode;
  5967. }
  5968. var bookmark = this.createBookmark(),
  5969. frag,
  5970. tmpRange;
  5971. if (start) {
  5972. tmpRange = this.cloneRange()
  5973. .setEndBefore(bookmark.start)
  5974. .setStartBefore(start);
  5975. frag = tmpRange.extractContents();
  5976. tmpRange.insertNode(frag);
  5977. domUtils.clearEmptySibling(start, true);
  5978. start.parentNode.insertBefore(bookmark.start, start);
  5979. }
  5980. if (end) {
  5981. tmpRange = this.cloneRange()
  5982. .setStartAfter(bookmark.end)
  5983. .setEndAfter(end);
  5984. frag = tmpRange.extractContents();
  5985. tmpRange.insertNode(frag);
  5986. domUtils.clearEmptySibling(end, false, true);
  5987. end.parentNode.insertBefore(bookmark.end, end.nextSibling);
  5988. }
  5989. var current = domUtils.getNextDomNode(bookmark.start, false, function(
  5990. node
  5991. ) {
  5992. return node.nodeType == 1;
  5993. }),
  5994. next;
  5995. while (current && current !== bookmark.end) {
  5996. next = domUtils.getNextDomNode(current, true, function(node) {
  5997. return node.nodeType == 1;
  5998. });
  5999. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  6000. domUtils.remove(current, true);
  6001. }
  6002. current = next;
  6003. }
  6004. return this.moveToBookmark(bookmark);
  6005. },
  6006. /**
  6007. * 获取当前选中的自闭合的节点
  6008. * @method getClosedNode
  6009. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  6010. */
  6011. getClosedNode: function() {
  6012. var node;
  6013. if (!this.collapsed) {
  6014. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  6015. if (selectOneNode(range)) {
  6016. var child = range.startContainer.childNodes[range.startOffset];
  6017. if (
  6018. child &&
  6019. child.nodeType == 1 &&
  6020. (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])
  6021. ) {
  6022. node = child;
  6023. }
  6024. }
  6025. }
  6026. return node;
  6027. },
  6028. /**
  6029. * 在页面上高亮range所表示的选区
  6030. * @method select
  6031. * @return { UE.dom.Range } 返回当前Range对象
  6032. */
  6033. //这里不区分ie9以上,trace:3824
  6034. select: browser.ie
  6035. ? function(noFillData, textRange) {
  6036. var nativeRange;
  6037. if (!this.collapsed) this.shrinkBoundary();
  6038. var node = this.getClosedNode();
  6039. if (node && !textRange) {
  6040. try {
  6041. nativeRange = this.document.body.createControlRange();
  6042. nativeRange.addElement(node);
  6043. nativeRange.select();
  6044. } catch (e) {}
  6045. return this;
  6046. }
  6047. var bookmark = this.createBookmark(),
  6048. start = bookmark.start,
  6049. end;
  6050. nativeRange = this.document.body.createTextRange();
  6051. nativeRange.moveToElementText(start);
  6052. nativeRange.moveStart("character", 1);
  6053. if (!this.collapsed) {
  6054. var nativeRangeEnd = this.document.body.createTextRange();
  6055. end = bookmark.end;
  6056. nativeRangeEnd.moveToElementText(end);
  6057. nativeRange.setEndPoint("EndToEnd", nativeRangeEnd);
  6058. } else {
  6059. if (!noFillData && this.startContainer.nodeType != 3) {
  6060. //使用<span>|x<span>固定住光标
  6061. var tmpText = this.document.createTextNode(fillChar),
  6062. tmp = this.document.createElement("span");
  6063. tmp.appendChild(this.document.createTextNode(fillChar));
  6064. start.parentNode.insertBefore(tmp, start);
  6065. start.parentNode.insertBefore(tmpText, start);
  6066. //当点b,i,u时,不能清除i上边的b
  6067. removeFillData(this.document, tmpText);
  6068. fillData = tmpText;
  6069. mergeSibling(tmp, "previousSibling");
  6070. mergeSibling(start, "nextSibling");
  6071. nativeRange.moveStart("character", -1);
  6072. nativeRange.collapse(true);
  6073. }
  6074. }
  6075. this.moveToBookmark(bookmark);
  6076. tmp && domUtils.remove(tmp);
  6077. //IE在隐藏状态下不支持range操作,catch一下
  6078. try {
  6079. nativeRange.select();
  6080. } catch (e) {}
  6081. return this;
  6082. }
  6083. : function(notInsertFillData) {
  6084. function checkOffset(rng) {
  6085. function check(node, offset, dir) {
  6086. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  6087. rng[dir + "Offset"] = node.nodeValue.length;
  6088. }
  6089. }
  6090. check(rng.startContainer, rng.startOffset, "start");
  6091. check(rng.endContainer, rng.endOffset, "end");
  6092. }
  6093. var win = domUtils.getWindow(this.document),
  6094. sel = win.getSelection(),
  6095. txtNode;
  6096. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  6097. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  6098. browser.gecko ? this.document.body.focus() : win.focus();
  6099. if (sel) {
  6100. sel.removeAllRanges();
  6101. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  6102. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  6103. if (this.collapsed && !notInsertFillData) {
  6104. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  6105. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  6106. // var tmp = this.document.createTextNode('');
  6107. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  6108. // }
  6109. //
  6110. //处理光标落在文本节点的情况
  6111. //处理以下的情况
  6112. //<b>|xxxx</b>
  6113. //<b>xxxx</b>|xxxx
  6114. //xxxx<b>|</b>
  6115. var start = this.startContainer,
  6116. child = start;
  6117. if (start.nodeType == 1) {
  6118. child = start.childNodes[this.startOffset];
  6119. }
  6120. if (
  6121. !(start.nodeType == 3 && this.startOffset) &&
  6122. (child
  6123. ? !child.previousSibling ||
  6124. child.previousSibling.nodeType != 3
  6125. : !start.lastChild || start.lastChild.nodeType != 3)
  6126. ) {
  6127. txtNode = this.document.createTextNode(fillChar);
  6128. //跟着前边走
  6129. this.insertNode(txtNode);
  6130. removeFillData(this.document, txtNode);
  6131. mergeSibling(txtNode, "previousSibling");
  6132. mergeSibling(txtNode, "nextSibling");
  6133. fillData = txtNode;
  6134. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  6135. }
  6136. }
  6137. var nativeRange = this.document.createRange();
  6138. if (
  6139. this.collapsed &&
  6140. browser.opera &&
  6141. this.startContainer.nodeType == 1
  6142. ) {
  6143. var child = this.startContainer.childNodes[this.startOffset];
  6144. if (!child) {
  6145. //往前靠拢
  6146. child = this.startContainer.lastChild;
  6147. if (child && domUtils.isBr(child)) {
  6148. this.setStartBefore(child).collapse(true);
  6149. }
  6150. } else {
  6151. //向后靠拢
  6152. while (child && domUtils.isBlockElm(child)) {
  6153. if (child.nodeType == 1 && child.childNodes[0]) {
  6154. child = child.childNodes[0];
  6155. } else {
  6156. break;
  6157. }
  6158. }
  6159. child && this.setStartBefore(child).collapse(true);
  6160. }
  6161. }
  6162. //是createAddress最后一位算的不准,现在这里进行微调
  6163. checkOffset(this);
  6164. nativeRange.setStart(this.startContainer, this.startOffset);
  6165. nativeRange.setEnd(this.endContainer, this.endOffset);
  6166. sel.addRange(nativeRange);
  6167. }
  6168. return this;
  6169. },
  6170. /**
  6171. * 滚动到当前range开始的位置
  6172. * @method scrollToView
  6173. * @param { Window } win 当前range对象所属的window对象
  6174. * @return { UE.dom.Range } 当前Range对象
  6175. */
  6176. /**
  6177. * 滚动到距离当前range开始位置 offset 的位置处
  6178. * @method scrollToView
  6179. * @param { Window } win 当前range对象所属的window对象
  6180. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  6181. * @return { UE.dom.Range } 当前Range对象
  6182. */
  6183. scrollToView: function(win, offset) {
  6184. win = win ? window : domUtils.getWindow(this.document);
  6185. var me = this,
  6186. span = me.document.createElement("span");
  6187. //trace:717
  6188. span.innerHTML = "&nbsp;";
  6189. me.cloneRange().insertNode(span);
  6190. domUtils.scrollToView(span, win, offset);
  6191. domUtils.remove(span);
  6192. return me;
  6193. },
  6194. /**
  6195. * 判断当前选区内容是否占位符
  6196. * @private
  6197. * @method inFillChar
  6198. * @return { Boolean } 如果是占位符返回true,否则返回false
  6199. */
  6200. inFillChar: function() {
  6201. var start = this.startContainer;
  6202. if (
  6203. this.collapsed &&
  6204. start.nodeType == 3 &&
  6205. start.nodeValue.replace(new RegExp("^" + domUtils.fillChar), "")
  6206. .length +
  6207. 1 ==
  6208. start.nodeValue.length
  6209. ) {
  6210. return true;
  6211. }
  6212. return false;
  6213. },
  6214. /**
  6215. * 保存
  6216. * @method createAddress
  6217. * @private
  6218. * @return { Boolean } 返回开始和结束的位置
  6219. * @example
  6220. * ```html
  6221. * <body>
  6222. * <p>
  6223. * aaaa
  6224. * <em>
  6225. * <!-- 选区开始 -->
  6226. * bbbb
  6227. * <!-- 选区结束 -->
  6228. * </em>
  6229. * </p>
  6230. *
  6231. * <script>
  6232. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6233. * console.log( range.createAddress() );
  6234. * </script>
  6235. * </body>
  6236. * ```
  6237. */
  6238. createAddress: function(ignoreEnd, ignoreTxt) {
  6239. var addr = {},
  6240. me = this;
  6241. function getAddress(isStart) {
  6242. var node = isStart ? me.startContainer : me.endContainer;
  6243. var parents = domUtils.findParents(node, true, function(node) {
  6244. return !domUtils.isBody(node);
  6245. }),
  6246. addrs = [];
  6247. for (var i = 0, ci; (ci = parents[i++]); ) {
  6248. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt));
  6249. }
  6250. var firstIndex = 0;
  6251. if (ignoreTxt) {
  6252. if (node.nodeType == 3) {
  6253. var tmpNode = node.previousSibling;
  6254. while (tmpNode && tmpNode.nodeType == 3) {
  6255. firstIndex += tmpNode.nodeValue.replace(fillCharReg, "").length;
  6256. tmpNode = tmpNode.previousSibling;
  6257. }
  6258. firstIndex += isStart ? me.startOffset : me.endOffset; // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6259. } else {
  6260. node = node.childNodes[isStart ? me.startOffset : me.endOffset];
  6261. if (node) {
  6262. firstIndex = domUtils.getNodeIndex(node, ignoreTxt);
  6263. } else {
  6264. node = isStart ? me.startContainer : me.endContainer;
  6265. var first = node.firstChild;
  6266. while (first) {
  6267. if (domUtils.isFillChar(first)) {
  6268. first = first.nextSibling;
  6269. continue;
  6270. }
  6271. firstIndex++;
  6272. if (first.nodeType == 3) {
  6273. while (first && first.nodeType == 3) {
  6274. first = first.nextSibling;
  6275. }
  6276. } else {
  6277. first = first.nextSibling;
  6278. }
  6279. }
  6280. }
  6281. }
  6282. } else {
  6283. firstIndex = isStart
  6284. ? domUtils.isFillChar(node) ? 0 : me.startOffset
  6285. : me.endOffset;
  6286. }
  6287. if (firstIndex < 0) {
  6288. firstIndex = 0;
  6289. }
  6290. addrs.push(firstIndex);
  6291. return addrs;
  6292. }
  6293. addr.startAddress = getAddress(true);
  6294. if (!ignoreEnd) {
  6295. addr.endAddress = me.collapsed
  6296. ? [].concat(addr.startAddress)
  6297. : getAddress();
  6298. }
  6299. return addr;
  6300. },
  6301. /**
  6302. * 保存
  6303. * @method createAddress
  6304. * @private
  6305. * @return { Boolean } 返回开始和结束的位置
  6306. * @example
  6307. * ```html
  6308. * <body>
  6309. * <p>
  6310. * aaaa
  6311. * <em>
  6312. * <!-- 选区开始 -->
  6313. * bbbb
  6314. * <!-- 选区结束 -->
  6315. * </em>
  6316. * </p>
  6317. *
  6318. * <script>
  6319. * var range = editor.selection.getRange();
  6320. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6321. * range.select();
  6322. * //output: 'bbbb'
  6323. * console.log(editor.selection.getText());
  6324. * </script>
  6325. * </body>
  6326. * ```
  6327. */
  6328. moveToAddress: function(addr, ignoreEnd) {
  6329. var me = this;
  6330. function getNode(address, isStart) {
  6331. var tmpNode = me.document.body,
  6332. parentNode,
  6333. offset;
  6334. for (var i = 0, ci, l = address.length; i < l; i++) {
  6335. ci = address[i];
  6336. parentNode = tmpNode;
  6337. tmpNode = tmpNode.childNodes[ci];
  6338. if (!tmpNode) {
  6339. offset = ci;
  6340. break;
  6341. }
  6342. }
  6343. if (isStart) {
  6344. if (tmpNode) {
  6345. me.setStartBefore(tmpNode);
  6346. } else {
  6347. me.setStart(parentNode, offset);
  6348. }
  6349. } else {
  6350. if (tmpNode) {
  6351. me.setEndBefore(tmpNode);
  6352. } else {
  6353. me.setEnd(parentNode, offset);
  6354. }
  6355. }
  6356. }
  6357. getNode(addr.startAddress, true);
  6358. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  6359. return me;
  6360. },
  6361. /**
  6362. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6363. * @method equals
  6364. * @param { UE.dom.Range } 需要判断的Range对象
  6365. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6366. */
  6367. equals: function(rng) {
  6368. for (var p in this) {
  6369. if (this.hasOwnProperty(p)) {
  6370. if (this[p] !== rng[p]) return false;
  6371. }
  6372. }
  6373. return true;
  6374. },
  6375. /**
  6376. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6377. * 作为其参数。
  6378. * @method traversal
  6379. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6380. * @return { UE.dom.Range } 当前range对象
  6381. * @example
  6382. * ```html
  6383. *
  6384. * <body>
  6385. *
  6386. * <!-- 选区开始 -->
  6387. * <span></span>
  6388. * <a></a>
  6389. * <!-- 选区结束 -->
  6390. * </body>
  6391. *
  6392. * <script>
  6393. *
  6394. * //output: <span></span><a></a>
  6395. * console.log( range.cloneContents() );
  6396. *
  6397. * range.traversal( function ( node ) {
  6398. *
  6399. * if ( node.nodeType === 1 ) {
  6400. * node.className = "test";
  6401. * }
  6402. *
  6403. * } );
  6404. *
  6405. * //output: <span class="test"></span><a class="test"></a>
  6406. * console.log( range.cloneContents() );
  6407. *
  6408. * </script>
  6409. * ```
  6410. */
  6411. /**
  6412. * 遍历range内的节点。
  6413. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6414. * 作为其参数。
  6415. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6416. * 发doFn函数的执行
  6417. * @method traversal
  6418. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6419. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6420. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6421. * 会触发doFn。
  6422. * @return { UE.dom.Range } 当前range对象
  6423. * @see UE.dom.Range:traversal(Function)
  6424. * @example
  6425. * ```html
  6426. *
  6427. * <body>
  6428. *
  6429. * <!-- 选区开始 -->
  6430. * <span></span>
  6431. * <a></a>
  6432. * <!-- 选区结束 -->
  6433. * </body>
  6434. *
  6435. * <script>
  6436. *
  6437. * //output: <span></span><a></a>
  6438. * console.log( range.cloneContents() );
  6439. *
  6440. * range.traversal( function ( node ) {
  6441. *
  6442. * node.className = "test";
  6443. *
  6444. * }, function ( node ) {
  6445. * return node.nodeType === 1;
  6446. * } );
  6447. *
  6448. * //output: <span class="test"></span><a class="test"></a>
  6449. * console.log( range.cloneContents() );
  6450. *
  6451. * </script>
  6452. * ```
  6453. */
  6454. traversal: function(doFn, filterFn) {
  6455. if (this.collapsed) return this;
  6456. var bookmark = this.createBookmark(),
  6457. end = bookmark.end,
  6458. current = domUtils.getNextDomNode(bookmark.start, false, filterFn);
  6459. while (
  6460. current &&
  6461. current !== end &&
  6462. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  6463. ) {
  6464. var tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  6465. doFn(current);
  6466. current = tmpNode;
  6467. }
  6468. return this.moveToBookmark(bookmark);
  6469. }
  6470. };
  6471. })();
  6472. // core/Selection.js
  6473. /**
  6474. * 选集
  6475. * @file
  6476. * @module UE.dom
  6477. * @class Selection
  6478. * @since 1.2.6.1
  6479. */
  6480. /**
  6481. * 选区集合
  6482. * @unfile
  6483. * @module UE.dom
  6484. * @class Selection
  6485. */
  6486. ;(function() {
  6487. function getBoundaryInformation(range, start) {
  6488. var getIndex = domUtils.getNodeIndex;
  6489. range = range.duplicate();
  6490. range.collapse(start);
  6491. var parent = range.parentElement();
  6492. //如果节点里没有子节点,直接退出
  6493. if (!parent.hasChildNodes()) {
  6494. return { container: parent, offset: 0 };
  6495. }
  6496. var siblings = parent.children,
  6497. child,
  6498. testRange = range.duplicate(),
  6499. startIndex = 0,
  6500. endIndex = siblings.length - 1,
  6501. index = -1,
  6502. distance;
  6503. while (startIndex <= endIndex) {
  6504. index = Math.floor((startIndex + endIndex) / 2);
  6505. child = siblings[index];
  6506. testRange.moveToElementText(child);
  6507. var position = testRange.compareEndPoints("StartToStart", range);
  6508. if (position > 0) {
  6509. endIndex = index - 1;
  6510. } else if (position < 0) {
  6511. startIndex = index + 1;
  6512. } else {
  6513. //trace:1043
  6514. return { container: parent, offset: getIndex(child) };
  6515. }
  6516. }
  6517. if (index == -1) {
  6518. testRange.moveToElementText(parent);
  6519. testRange.setEndPoint("StartToStart", range);
  6520. distance = testRange.text.replace(/(\r\n|\r)/g, "\n").length;
  6521. siblings = parent.childNodes;
  6522. if (!distance) {
  6523. child = siblings[siblings.length - 1];
  6524. return { container: child, offset: child.nodeValue.length };
  6525. }
  6526. var i = siblings.length;
  6527. while (distance > 0) {
  6528. distance -= siblings[--i].nodeValue.length;
  6529. }
  6530. return { container: siblings[i], offset: -distance };
  6531. }
  6532. testRange.collapse(position > 0);
  6533. testRange.setEndPoint(position > 0 ? "StartToStart" : "EndToStart", range);
  6534. distance = testRange.text.replace(/(\r\n|\r)/g, "\n").length;
  6535. if (!distance) {
  6536. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName]
  6537. ? {
  6538. container: parent,
  6539. offset: getIndex(child) + (position > 0 ? 0 : 1)
  6540. }
  6541. : {
  6542. container: child,
  6543. offset: position > 0 ? 0 : child.childNodes.length
  6544. };
  6545. }
  6546. while (distance > 0) {
  6547. try {
  6548. var pre = child;
  6549. child = child[position > 0 ? "previousSibling" : "nextSibling"];
  6550. distance -= child.nodeValue.length;
  6551. } catch (e) {
  6552. return { container: parent, offset: getIndex(pre) };
  6553. }
  6554. }
  6555. return {
  6556. container: child,
  6557. offset: position > 0 ? -distance : child.nodeValue.length + distance
  6558. };
  6559. }
  6560. /**
  6561. * 将ieRange转换为Range对象
  6562. * @param {Range} ieRange ieRange对象
  6563. * @param {Range} range Range对象
  6564. * @return {Range} range 返回转换后的Range对象
  6565. */
  6566. function transformIERangeToRange(ieRange, range) {
  6567. if (ieRange.item) {
  6568. range.selectNode(ieRange.item(0));
  6569. } else {
  6570. var bi = getBoundaryInformation(ieRange, true);
  6571. range.setStart(bi.container, bi.offset);
  6572. if (ieRange.compareEndPoints("StartToEnd", ieRange) != 0) {
  6573. bi = getBoundaryInformation(ieRange, false);
  6574. range.setEnd(bi.container, bi.offset);
  6575. }
  6576. }
  6577. return range;
  6578. }
  6579. /**
  6580. * 获得ieRange
  6581. * @param {Selection} sel Selection对象
  6582. * @return {ieRange} 得到ieRange
  6583. */
  6584. function _getIERange(sel) {
  6585. var ieRange;
  6586. //ie下有可能报错
  6587. try {
  6588. ieRange = sel.getNative().createRange();
  6589. } catch (e) {
  6590. return null;
  6591. }
  6592. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement();
  6593. if ((el.ownerDocument || el) === sel.document) {
  6594. return ieRange;
  6595. }
  6596. return null;
  6597. }
  6598. var Selection = (dom.Selection = function(doc) {
  6599. var me = this,
  6600. iframe;
  6601. me.document = doc;
  6602. if (browser.ie9below) {
  6603. iframe = domUtils.getWindow(doc).frameElement;
  6604. domUtils.on(iframe, "beforedeactivate", function() {
  6605. me._bakIERange = me.getIERange();
  6606. });
  6607. domUtils.on(iframe, "activate", function() {
  6608. try {
  6609. if (!_getIERange(me) && me._bakIERange) {
  6610. me._bakIERange.select();
  6611. }
  6612. } catch (ex) {}
  6613. me._bakIERange = null;
  6614. });
  6615. }
  6616. iframe = doc = null;
  6617. });
  6618. Selection.prototype = {
  6619. rangeInBody: function(rng, txtRange) {
  6620. var node = browser.ie9below || txtRange
  6621. ? rng.item ? rng.item() : rng.parentElement()
  6622. : rng.startContainer;
  6623. return node === this.document.body || domUtils.inDoc(node, this.document);
  6624. },
  6625. /**
  6626. * 获取原生seleciton对象
  6627. * @method getNative
  6628. * @return { Object } 获得selection对象
  6629. * @example
  6630. * ```javascript
  6631. * editor.selection.getNative();
  6632. * ```
  6633. */
  6634. getNative: function() {
  6635. var doc = this.document;
  6636. try {
  6637. return !doc
  6638. ? null
  6639. : browser.ie9below
  6640. ? doc.selection
  6641. : domUtils.getWindow(doc).getSelection();
  6642. } catch (e) {
  6643. return null;
  6644. }
  6645. },
  6646. /**
  6647. * 获得ieRange
  6648. * @method getIERange
  6649. * @return { Object } 返回ie原生的Range
  6650. * @example
  6651. * ```javascript
  6652. * editor.selection.getIERange();
  6653. * ```
  6654. */
  6655. getIERange: function() {
  6656. var ieRange = _getIERange(this);
  6657. if (!ieRange) {
  6658. if (this._bakIERange) {
  6659. return this._bakIERange;
  6660. }
  6661. }
  6662. return ieRange;
  6663. },
  6664. /**
  6665. * 缓存当前选区的range和选区的开始节点
  6666. * @method cache
  6667. */
  6668. cache: function() {
  6669. this.clear();
  6670. this._cachedRange = this.getRange();
  6671. this._cachedStartElement = this.getStart();
  6672. this._cachedStartElementPath = this.getStartElementPath();
  6673. },
  6674. /**
  6675. * 获取选区开始位置的父节点到body
  6676. * @method getStartElementPath
  6677. * @return { Array } 返回父节点集合
  6678. * @example
  6679. * ```javascript
  6680. * editor.selection.getStartElementPath();
  6681. * ```
  6682. */
  6683. getStartElementPath: function() {
  6684. if (this._cachedStartElementPath) {
  6685. return this._cachedStartElementPath;
  6686. }
  6687. var start = this.getStart();
  6688. if (start) {
  6689. return domUtils.findParents(start, true, null, true);
  6690. }
  6691. return [];
  6692. },
  6693. /**
  6694. * 清空缓存
  6695. * @method clear
  6696. */
  6697. clear: function() {
  6698. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  6699. },
  6700. /**
  6701. * 编辑器是否得到了选区
  6702. * @method isFocus
  6703. */
  6704. isFocus: function() {
  6705. try {
  6706. if (browser.ie9below) {
  6707. var nativeRange = _getIERange(this);
  6708. return !!(nativeRange && this.rangeInBody(nativeRange));
  6709. } else {
  6710. return !!this.getNative().rangeCount;
  6711. }
  6712. } catch (e) {
  6713. return false;
  6714. }
  6715. },
  6716. /**
  6717. * 获取选区对应的Range
  6718. * @method getRange
  6719. * @return { Object } 得到Range对象
  6720. * @example
  6721. * ```javascript
  6722. * editor.selection.getRange();
  6723. * ```
  6724. */
  6725. getRange: function() {
  6726. var me = this;
  6727. function optimze(range) {
  6728. var child = me.document.body.firstChild,
  6729. collapsed = range.collapsed;
  6730. while (child && child.firstChild) {
  6731. range.setStart(child, 0);
  6732. child = child.firstChild;
  6733. }
  6734. if (!range.startContainer) {
  6735. range.setStart(me.document.body, 0);
  6736. }
  6737. if (collapsed) {
  6738. range.collapse(true);
  6739. }
  6740. }
  6741. if (me._cachedRange != null) {
  6742. return this._cachedRange;
  6743. }
  6744. var range = new baidu.editor.dom.Range(me.document);
  6745. if (browser.ie9below) {
  6746. var nativeRange = me.getIERange();
  6747. if (nativeRange) {
  6748. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6749. try {
  6750. transformIERangeToRange(nativeRange, range);
  6751. } catch (e) {
  6752. optimze(range);
  6753. }
  6754. } else {
  6755. optimze(range);
  6756. }
  6757. } else {
  6758. var sel = me.getNative();
  6759. if (sel && sel.rangeCount) {
  6760. var firstRange = sel.getRangeAt(0);
  6761. var lastRange = sel.getRangeAt(sel.rangeCount - 1);
  6762. range
  6763. .setStart(firstRange.startContainer, firstRange.startOffset)
  6764. .setEnd(lastRange.endContainer, lastRange.endOffset);
  6765. if (
  6766. range.collapsed &&
  6767. domUtils.isBody(range.startContainer) &&
  6768. !range.startOffset
  6769. ) {
  6770. optimze(range);
  6771. }
  6772. } else {
  6773. //trace:1734 有可能已经不在dom树上了,标识的节点
  6774. if (
  6775. this._bakRange &&
  6776. domUtils.inDoc(this._bakRange.startContainer, this.document)
  6777. ) {
  6778. return this._bakRange;
  6779. }
  6780. optimze(range);
  6781. }
  6782. }
  6783. return (this._bakRange = range);
  6784. },
  6785. /**
  6786. * 获取开始元素,用于状态反射
  6787. * @method getStart
  6788. * @return { Element } 获得开始元素
  6789. * @example
  6790. * ```javascript
  6791. * editor.selection.getStart();
  6792. * ```
  6793. */
  6794. getStart: function() {
  6795. if (this._cachedStartElement) {
  6796. return this._cachedStartElement;
  6797. }
  6798. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6799. tmpRange,
  6800. start,
  6801. tmp,
  6802. parent;
  6803. if (browser.ie9below) {
  6804. if (!range) {
  6805. //todo 给第一个值可能会有问题
  6806. return this.document.body.firstChild;
  6807. }
  6808. //control元素
  6809. if (range.item) {
  6810. return range.item(0);
  6811. }
  6812. tmpRange = range.duplicate();
  6813. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6814. tmpRange.text.length > 0 && tmpRange.moveStart("character", 1);
  6815. tmpRange.collapse(1);
  6816. start = tmpRange.parentElement();
  6817. parent = tmp = range.parentElement();
  6818. while ((tmp = tmp.parentNode)) {
  6819. if (tmp == start) {
  6820. start = parent;
  6821. break;
  6822. }
  6823. }
  6824. } else {
  6825. range.shrinkBoundary();
  6826. start = range.startContainer;
  6827. if (start.nodeType == 1 && start.hasChildNodes()) {
  6828. start =
  6829. start.childNodes[
  6830. Math.min(start.childNodes.length - 1, range.startOffset)
  6831. ];
  6832. }
  6833. if (start.nodeType == 3) {
  6834. return start.parentNode;
  6835. }
  6836. }
  6837. return start;
  6838. },
  6839. /**
  6840. * 得到选区中的文本
  6841. * @method getText
  6842. * @return { String } 选区中包含的文本
  6843. * @example
  6844. * ```javascript
  6845. * editor.selection.getText();
  6846. * ```
  6847. */
  6848. getText: function() {
  6849. var nativeSel, nativeRange;
  6850. if (this.isFocus() && (nativeSel = this.getNative())) {
  6851. nativeRange = browser.ie9below
  6852. ? nativeSel.createRange()
  6853. : nativeSel.getRangeAt(0);
  6854. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  6855. }
  6856. return "";
  6857. },
  6858. /**
  6859. * 清除选区
  6860. * @method clearRange
  6861. * @example
  6862. * ```javascript
  6863. * editor.selection.clearRange();
  6864. * ```
  6865. */
  6866. clearRange: function() {
  6867. this.getNative()[browser.ie9below ? "empty" : "removeAllRanges"]();
  6868. }
  6869. };
  6870. })();
  6871. // core/Editor.js
  6872. /**
  6873. * 编辑器主类,包含编辑器提供的大部分公用接口
  6874. * @file
  6875. * @module UE
  6876. * @class Editor
  6877. * @since 1.2.6.1
  6878. */
  6879. /**
  6880. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6881. * @unfile
  6882. * @module UE
  6883. */
  6884. /**
  6885. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6886. * @unfile
  6887. * @module UE
  6888. * @class Editor
  6889. */
  6890. ;(function() {
  6891. var uid = 0,
  6892. _selectionChangeTimer;
  6893. /**
  6894. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6895. * @private
  6896. * @method setValue
  6897. * @param { UE.Editor } editor 编辑器事例
  6898. */
  6899. function setValue(form, editor) {
  6900. var textarea;
  6901. if (editor.options.textarea) {
  6902. if (utils.isString(editor.options.textarea)) {
  6903. for (
  6904. var i = 0, ti, tis = domUtils.getElementsByTagName(form, "textarea");
  6905. (ti = tis[i++]);
  6906. ) {
  6907. if (ti.id == "ueditor_textarea_" + editor.options.textarea) {
  6908. textarea = ti;
  6909. break;
  6910. }
  6911. }
  6912. } else {
  6913. textarea = editor.textarea;
  6914. }
  6915. }
  6916. if (!textarea) {
  6917. form.appendChild(
  6918. (textarea = domUtils.createElement(document, "textarea", {
  6919. name: editor.options.textarea,
  6920. id: "ueditor_textarea_" + editor.options.textarea,
  6921. style: "display:none"
  6922. }))
  6923. );
  6924. //不要产生多个textarea
  6925. editor.textarea = textarea;
  6926. }
  6927. !textarea.getAttribute("name") &&
  6928. textarea.setAttribute("name", editor.options.textarea);
  6929. textarea.value = editor.hasContents()
  6930. ? editor.options.allHtmlEnabled
  6931. ? editor.getAllHtml()
  6932. : editor.getContent(null, null, true)
  6933. : "";
  6934. }
  6935. function loadPlugins(me) {
  6936. //初始化插件
  6937. for (var pi in UE.plugins) {
  6938. UE.plugins[pi].call(me);
  6939. }
  6940. }
  6941. function checkCurLang(I18N) {
  6942. for (var lang in I18N) {
  6943. return lang;
  6944. }
  6945. }
  6946. function langReadied(me) {
  6947. me.langIsReady = true;
  6948. me.fireEvent("langReady");
  6949. }
  6950. /**
  6951. * 编辑器准备就绪后会触发该事件
  6952. * @module UE
  6953. * @class Editor
  6954. * @event ready
  6955. * @remind render方法执行完成之后,会触发该事件
  6956. * @remind
  6957. * @example
  6958. * ```javascript
  6959. * editor.addListener( 'ready', function( editor ) {
  6960. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  6961. * } );
  6962. * ```
  6963. */
  6964. /**
  6965. * 执行destroy方法,会触发该事件
  6966. * @module UE
  6967. * @class Editor
  6968. * @event destroy
  6969. * @see UE.Editor:destroy()
  6970. */
  6971. /**
  6972. * 执行reset方法,会触发该事件
  6973. * @module UE
  6974. * @class Editor
  6975. * @event reset
  6976. * @see UE.Editor:reset()
  6977. */
  6978. /**
  6979. * 执行focus方法,会触发该事件
  6980. * @module UE
  6981. * @class Editor
  6982. * @event focus
  6983. * @see UE.Editor:focus(Boolean)
  6984. */
  6985. /**
  6986. * 语言加载完成会触发该事件
  6987. * @module UE
  6988. * @class Editor
  6989. * @event langReady
  6990. */
  6991. /**
  6992. * 运行命令之后会触发该命令
  6993. * @module UE
  6994. * @class Editor
  6995. * @event beforeExecCommand
  6996. */
  6997. /**
  6998. * 运行命令之后会触发该命令
  6999. * @module UE
  7000. * @class Editor
  7001. * @event afterExecCommand
  7002. */
  7003. /**
  7004. * 运行命令之前会触发该命令
  7005. * @module UE
  7006. * @class Editor
  7007. * @event firstBeforeExecCommand
  7008. */
  7009. /**
  7010. * 在getContent方法执行之前会触发该事件
  7011. * @module UE
  7012. * @class Editor
  7013. * @event beforeGetContent
  7014. * @see UE.Editor:getContent()
  7015. */
  7016. /**
  7017. * 在getContent方法执行之后会触发该事件
  7018. * @module UE
  7019. * @class Editor
  7020. * @event afterGetContent
  7021. * @see UE.Editor:getContent()
  7022. */
  7023. /**
  7024. * 在getAllHtml方法执行时会触发该事件
  7025. * @module UE
  7026. * @class Editor
  7027. * @event getAllHtml
  7028. * @see UE.Editor:getAllHtml()
  7029. */
  7030. /**
  7031. * 在setContent方法执行之前会触发该事件
  7032. * @module UE
  7033. * @class Editor
  7034. * @event beforeSetContent
  7035. * @see UE.Editor:setContent(String)
  7036. */
  7037. /**
  7038. * 在setContent方法执行之后会触发该事件
  7039. * @module UE
  7040. * @class Editor
  7041. * @event afterSetContent
  7042. * @see UE.Editor:setContent(String)
  7043. */
  7044. /**
  7045. * 每当编辑器内部选区发生改变时,将触发该事件
  7046. * @event selectionchange
  7047. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  7048. * @example
  7049. * ```javascript
  7050. * editor.addListener( 'selectionchange', function( editor ) {
  7051. * console.log('选区发生改变');
  7052. * }
  7053. */
  7054. /**
  7055. * 在所有selectionchange的监听函数执行之前,会触发该事件
  7056. * @module UE
  7057. * @class Editor
  7058. * @event beforeSelectionChange
  7059. * @see UE.Editor:selectionchange
  7060. */
  7061. /**
  7062. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  7063. * @module UE
  7064. * @class Editor
  7065. * @event afterSelectionChange
  7066. * @see UE.Editor:selectionchange
  7067. */
  7068. /**
  7069. * 编辑器内容发生改变时会触发该事件
  7070. * @module UE
  7071. * @class Editor
  7072. * @event contentChange
  7073. */
  7074. /**
  7075. * 以默认参数构建一个编辑器实例
  7076. * @constructor
  7077. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7078. * @example
  7079. * ```javascript
  7080. * var editor = new UE.Editor();
  7081. * editor.execCommand('blod');
  7082. * ```
  7083. * @see UE.Config
  7084. */
  7085. /**
  7086. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  7087. * @constructor
  7088. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7089. * @param { Object } setting 创建编辑器的参数
  7090. * @example
  7091. * ```javascript
  7092. * var editor = new UE.Editor();
  7093. * editor.execCommand('blod');
  7094. * ```
  7095. * @see UE.Config
  7096. */
  7097. var Editor = (UE.Editor = function(options) {
  7098. var me = this;
  7099. me.uid = uid++;
  7100. EventBase.call(me);
  7101. me.commands = {};
  7102. me.options = utils.extend(utils.clone(options || {}), UEDITOR_CONFIG, true);
  7103. me.shortcutkeys = {};
  7104. me.inputRules = [];
  7105. me.outputRules = [];
  7106. //设置默认的常用属性
  7107. me.setOpt(Editor.defaultOptions(me));
  7108. /* 尝试异步加载后台配置 */
  7109. //me.loadServerConfig();
  7110. if (!utils.isEmptyObject(UE.I18N)) {
  7111. //修改默认的语言类型
  7112. me.options.lang = checkCurLang(UE.I18N);
  7113. UE.plugin.load(me);
  7114. langReadied(me);
  7115. } else {
  7116. utils.loadFile(
  7117. document,
  7118. {
  7119. src:
  7120. me.options.langPath +
  7121. me.options.lang +
  7122. "/" +
  7123. me.options.lang +
  7124. ".js",
  7125. tag: "script",
  7126. type: "text/javascript",
  7127. defer: "defer"
  7128. },
  7129. function() {
  7130. UE.plugin.load(me);
  7131. langReadied(me);
  7132. }
  7133. );
  7134. }
  7135. UE.instants["ueditorInstant" + me.uid] = me;
  7136. });
  7137. Editor.prototype = {
  7138. registerCommand: function(name, obj) {
  7139. this.commands[name] = obj;
  7140. },
  7141. /**
  7142. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  7143. * @method ready
  7144. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  7145. * 立即触发该回调。
  7146. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  7147. * @example
  7148. * ```javascript
  7149. * editor.ready( function( editor ) {
  7150. * editor.setContent('初始化完毕');
  7151. * } );
  7152. * ```
  7153. * @see UE.Editor.event:ready
  7154. */
  7155. ready: function(fn) {
  7156. var me = this;
  7157. if (fn) {
  7158. me.isReady ? fn.apply(me) : me.addListener("ready", fn);
  7159. }
  7160. },
  7161. /**
  7162. * 该方法用于设置placeholder
  7163. * @method setPlaceholder
  7164. * @param { String } placeholder 编辑器的placeholder文案
  7165. * @example
  7166. * ```javascript
  7167. * editor.setPlaceholder('请输入内容');
  7168. * ```
  7169. */
  7170. setPlaceholder: function(){
  7171. function contentChange(){
  7172. var localHtml = this.getPlainTxt();
  7173. if(!localHtml.trim()){
  7174. UE.dom.domUtils.addClass( this.body, 'empty' );
  7175. }else{
  7176. UE.dom.domUtils.removeClasses( this.body, 'empty' );
  7177. }
  7178. }
  7179. return function(placeholder){
  7180. var _editor = this;
  7181. _editor.ready(function () {
  7182. contentChange.call(_editor);
  7183. _editor.body.setAttribute('placeholder', placeholder);
  7184. });
  7185. _editor.removeListener('keyup contentchange', contentChange);
  7186. _editor.addListener('keyup contentchange', contentChange);
  7187. }
  7188. }(),
  7189. /**
  7190. * 该方法是提供给插件里面使用,设置配置项默认值
  7191. * @method setOpt
  7192. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7193. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7194. * @param { String } key 编辑器的可接受的选项名称
  7195. * @param { * } val 该选项可接受的值
  7196. * @example
  7197. * ```javascript
  7198. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  7199. * ```
  7200. */
  7201. /**
  7202. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  7203. * @method setOpt
  7204. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7205. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7206. * @param { Object } options 将要设置的选项的键值对对象
  7207. * @example
  7208. * ```javascript
  7209. * editor.setOpt( {
  7210. * 'initContent': '欢迎使用编辑器'
  7211. * } );
  7212. * ```
  7213. */
  7214. setOpt: function(key, val) {
  7215. var obj = {};
  7216. if (utils.isString(key)) {
  7217. obj[key] = val;
  7218. } else {
  7219. obj = key;
  7220. }
  7221. utils.extend(this.options, obj, true);
  7222. },
  7223. getOpt: function(key) {
  7224. return this.options[key];
  7225. },
  7226. /**
  7227. * 销毁编辑器实例,使用textarea代替
  7228. * @method destroy
  7229. * @example
  7230. * ```javascript
  7231. * editor.destroy();
  7232. * ```
  7233. */
  7234. destroy: function() {
  7235. var me = this;
  7236. me.fireEvent("destroy");
  7237. var container = me.container.parentNode;
  7238. var textarea = me.textarea;
  7239. if (!textarea) {
  7240. textarea = document.createElement("textarea");
  7241. container.parentNode.insertBefore(textarea, container);
  7242. } else {
  7243. textarea.style.display = "";
  7244. }
  7245. textarea.style.width = me.iframe.offsetWidth + "px";
  7246. textarea.style.height = me.iframe.offsetHeight + "px";
  7247. textarea.value = me.getContent();
  7248. textarea.id = me.key;
  7249. container.innerHTML = "";
  7250. domUtils.remove(container);
  7251. var key = me.key;
  7252. //trace:2004
  7253. for (var p in me) {
  7254. if (me.hasOwnProperty(p)) {
  7255. delete this[p];
  7256. }
  7257. }
  7258. UE.delEditor(key);
  7259. },
  7260. /**
  7261. * 渲染编辑器的DOM到指定容器
  7262. * @method render
  7263. * @param { String } containerId 指定一个容器ID
  7264. * @remind 执行该方法,会触发ready事件
  7265. * @warning 必须且只能调用一次
  7266. */
  7267. /**
  7268. * 渲染编辑器的DOM到指定容器
  7269. * @method render
  7270. * @param { Element } containerDom 直接指定容器对象
  7271. * @remind 执行该方法,会触发ready事件
  7272. * @warning 必须且只能调用一次
  7273. */
  7274. render: function(container) {
  7275. var me = this,
  7276. options = me.options,
  7277. getStyleValue = function(attr) {
  7278. return parseInt(domUtils.getComputedStyle(container, attr));
  7279. };
  7280. if (utils.isString(container)) {
  7281. container = document.getElementById(container);
  7282. }
  7283. if (container) {
  7284. if (options.initialFrameWidth) {
  7285. options.minFrameWidth = options.initialFrameWidth;
  7286. } else {
  7287. options.minFrameWidth = options.initialFrameWidth =
  7288. container.offsetWidth;
  7289. }
  7290. if (options.initialFrameHeight) {
  7291. options.minFrameHeight = options.initialFrameHeight;
  7292. } else {
  7293. options.initialFrameHeight = options.minFrameHeight =
  7294. container.offsetHeight;
  7295. }
  7296. container.style.width = /%$/.test(options.initialFrameWidth)
  7297. ? "100%"
  7298. : options.initialFrameWidth -
  7299. getStyleValue("padding-left") -
  7300. getStyleValue("padding-right") +
  7301. "px";
  7302. container.style.height = /%$/.test(options.initialFrameHeight)
  7303. ? "100%"
  7304. : options.initialFrameHeight -
  7305. getStyleValue("padding-top") -
  7306. getStyleValue("padding-bottom") +
  7307. "px";
  7308. container.style.zIndex = options.zIndex;
  7309. var html =
  7310. (ie && browser.version < 9 ? "" : "<!DOCTYPE html>") +
  7311. "<html xmlns='http://www.w3.org/1999/xhtml' class='view' >" +
  7312. "<head>" +
  7313. "<style type='text/css'>" +
  7314. //设置四周的留边
  7315. ".view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n" +
  7316. //设置默认字体和字号
  7317. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7318. "body{margin:8px;font-family:sans-serif;font-size:16px;}" +
  7319. //设置placeholder
  7320. "body.empty:before{content:attr(placeholder);position:absolute;color:#999;}"+
  7321. //设置段落间距
  7322. "p{margin:5px 0;}</style>" +
  7323. (options.iframeCssUrl
  7324. ? "<link rel='stylesheet' type='text/css' href='" +
  7325. utils.unhtml(options.iframeCssUrl) +
  7326. "'/>"
  7327. : "") +
  7328. (options.initialStyle
  7329. ? "<style>" + options.initialStyle + "</style>"
  7330. : "") +
  7331. "</head>" +
  7332. "<body class='view' ></body>" +
  7333. "<script type='text/javascript' " +
  7334. (ie ? "defer='defer'" : "") +
  7335. " id='_initialScript'>" +
  7336. "setTimeout(function(){editor = window.parent.UE.instants['ueditorInstant" +
  7337. me.uid +
  7338. "'];editor._setup(document);},0);" +
  7339. "var _tmpScript = document.getElementById('_initialScript');_tmpScript.parentNode.removeChild(_tmpScript);" +
  7340. "</script>" +
  7341. (options.iframeJsUrl
  7342. ? "<script type='text/javascript' src='" +
  7343. utils.unhtml(options.iframeJsUrl) +
  7344. "'></script>"
  7345. : "") +
  7346. "</html>";
  7347. container.appendChild(
  7348. domUtils.createElement(document, "iframe", {
  7349. id: "ueditor_" + me.uid,
  7350. width: "100%",
  7351. height: "100%",
  7352. frameborder: "0",
  7353. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7354. // scrolling :'no',
  7355. src:
  7356. "javascript:void(function(){document.open();" +
  7357. (options.customDomain && document.domain != location.hostname
  7358. ? 'document.domain="' + document.domain + '";'
  7359. : "") +
  7360. 'document.write("' +
  7361. html +
  7362. '");document.close();}())'
  7363. })
  7364. );
  7365. container.style.overflow = "hidden";
  7366. //解决如果是给定的百分比,会导致高度算不对的问题
  7367. setTimeout(function() {
  7368. if (/%$/.test(options.initialFrameWidth)) {
  7369. options.minFrameWidth = options.initialFrameWidth =
  7370. container.offsetWidth;
  7371. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7372. // container.style.width = options.initialFrameWidth + 'px';
  7373. }
  7374. if (/%$/.test(options.initialFrameHeight)) {
  7375. options.minFrameHeight = options.initialFrameHeight =
  7376. container.offsetHeight;
  7377. container.style.height = options.initialFrameHeight + "px";
  7378. }
  7379. });
  7380. }
  7381. },
  7382. /**
  7383. * 编辑器初始化
  7384. * @method _setup
  7385. * @private
  7386. * @param { Element } doc 编辑器Iframe中的文档对象
  7387. */
  7388. _setup: function(doc) {
  7389. var me = this,
  7390. options = me.options;
  7391. if (ie) {
  7392. doc.body.disabled = true;
  7393. doc.body.contentEditable = true;
  7394. doc.body.disabled = false;
  7395. } else {
  7396. doc.body.contentEditable = true;
  7397. }
  7398. doc.body.spellcheck = false;
  7399. me.document = doc;
  7400. me.window = doc.defaultView || doc.parentWindow;
  7401. me.iframe = me.window.frameElement;
  7402. me.body = doc.body;
  7403. me.selection = new dom.Selection(doc);
  7404. //gecko初始化就能得到range,无法判断isFocus了
  7405. var geckoSel;
  7406. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7407. geckoSel.removeAllRanges();
  7408. }
  7409. this._initEvents();
  7410. //为form提交提供一个隐藏的textarea
  7411. for (
  7412. var form = this.iframe.parentNode;
  7413. !domUtils.isBody(form);
  7414. form = form.parentNode
  7415. ) {
  7416. if (form.tagName == "FORM") {
  7417. me.form = form;
  7418. if (me.options.autoSyncData) {
  7419. domUtils.on(me.window, "blur", function() {
  7420. setValue(form, me);
  7421. });
  7422. } else {
  7423. domUtils.on(form, "submit", function() {
  7424. setValue(this, me);
  7425. });
  7426. }
  7427. break;
  7428. }
  7429. }
  7430. if (options.initialContent) {
  7431. if (options.autoClearinitialContent) {
  7432. var oldExecCommand = me.execCommand;
  7433. me.execCommand = function() {
  7434. me.fireEvent("firstBeforeExecCommand");
  7435. return oldExecCommand.apply(me, arguments);
  7436. };
  7437. this._setDefaultContent(options.initialContent);
  7438. } else this.setContent(options.initialContent, false, true);
  7439. }
  7440. //编辑器不能为空内容
  7441. if (domUtils.isEmptyNode(me.body)) {
  7442. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  7443. }
  7444. //如果要求focus, 就把光标定位到内容开始
  7445. if (options.focus) {
  7446. setTimeout(function() {
  7447. me.focus(me.options.focusInEnd);
  7448. //如果自动清除开着,就不需要做selectionchange;
  7449. !me.options.autoClearinitialContent && me._selectionChange();
  7450. }, 0);
  7451. }
  7452. if (!me.container) {
  7453. me.container = this.iframe.parentNode;
  7454. }
  7455. if (options.fullscreen && me.ui) {
  7456. me.ui.setFullScreen(true);
  7457. }
  7458. try {
  7459. me.document.execCommand("2D-position", false, false);
  7460. } catch (e) {}
  7461. try {
  7462. me.document.execCommand("enableInlineTableEditing", false, false);
  7463. } catch (e) {}
  7464. try {
  7465. me.document.execCommand("enableObjectResizing", false, false);
  7466. } catch (e) {}
  7467. //挂接快捷键
  7468. me._bindshortcutKeys();
  7469. me.isReady = 1;
  7470. me.fireEvent("ready");
  7471. options.onready && options.onready.call(me);
  7472. if (!browser.ie9below) {
  7473. domUtils.on(me.window, ["blur", "focus"], function(e) {
  7474. //chrome下会出现alt+tab切换时,导致选区位置不对
  7475. if (e.type == "blur") {
  7476. me._bakRange = me.selection.getRange();
  7477. try {
  7478. me._bakNativeRange = me.selection.getNative().getRangeAt(0);
  7479. me.selection.getNative().removeAllRanges();
  7480. } catch (e) {
  7481. me._bakNativeRange = null;
  7482. }
  7483. } else {
  7484. try {
  7485. me._bakRange && me._bakRange.select();
  7486. } catch (e) {}
  7487. }
  7488. });
  7489. }
  7490. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7491. if (browser.gecko && browser.version <= 10902) {
  7492. //修复ff3.6初始化进来,不能点击获得焦点
  7493. me.body.contentEditable = false;
  7494. setTimeout(function() {
  7495. me.body.contentEditable = true;
  7496. }, 100);
  7497. setInterval(function() {
  7498. me.body.style.height = me.iframe.offsetHeight - 20 + "px";
  7499. }, 100);
  7500. }
  7501. !options.isShow && me.setHide();
  7502. options.readonly && me.setDisabled();
  7503. },
  7504. /**
  7505. * 同步数据到编辑器所在的form
  7506. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7507. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7508. * @method sync
  7509. * @example
  7510. * ```javascript
  7511. * editor.sync();
  7512. * form.sumbit(); //form变量已经指向了form元素
  7513. * ```
  7514. */
  7515. /**
  7516. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7517. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7518. * @method sync
  7519. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7520. */
  7521. sync: function(formId) {
  7522. var me = this,
  7523. form = formId
  7524. ? document.getElementById(formId)
  7525. : domUtils.findParent(
  7526. me.iframe.parentNode,
  7527. function(node) {
  7528. return node.tagName == "FORM";
  7529. },
  7530. true
  7531. );
  7532. form && setValue(form, me);
  7533. },
  7534. /**
  7535. * 设置编辑器高度
  7536. * @method setHeight
  7537. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7538. * @param { Number } number 设置的高度值,纯数值,不带单位
  7539. * @example
  7540. * ```javascript
  7541. * editor.setHeight(number);
  7542. * ```
  7543. */
  7544. setHeight: function(height, notSetHeight) {
  7545. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7546. this.iframe.parentNode.style.height = height + "px";
  7547. }
  7548. !notSetHeight &&
  7549. (this.options.minFrameHeight = this.options.initialFrameHeight = height);
  7550. this.body.style.height = height + "px";
  7551. !notSetHeight && this.trigger("setHeight");
  7552. },
  7553. /**
  7554. * 为编辑器的编辑命令提供快捷键
  7555. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7556. * @method addshortcutkey
  7557. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7558. * @example
  7559. * ```javascript
  7560. * editor.addshortcutkey({
  7561. * "Bold" : "ctrl+66",//^B
  7562. * "Italic" : "ctrl+73", //^I
  7563. * });
  7564. * ```
  7565. */
  7566. /**
  7567. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7568. * @method addshortcutkey
  7569. * @param { String } cmd 触发快捷键时,响应的命令
  7570. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7571. * @example
  7572. * ```javascript
  7573. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7574. * ```
  7575. */
  7576. addshortcutkey: function(cmd, keys) {
  7577. var obj = {};
  7578. if (keys) {
  7579. obj[cmd] = keys;
  7580. } else {
  7581. obj = cmd;
  7582. }
  7583. utils.extend(this.shortcutkeys, obj);
  7584. },
  7585. /**
  7586. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7587. * @method _bindshortcutKeys
  7588. * @private
  7589. */
  7590. _bindshortcutKeys: function() {
  7591. var me = this,
  7592. shortcutkeys = this.shortcutkeys;
  7593. me.addListener("keydown", function(type, e) {
  7594. var keyCode = e.keyCode || e.which;
  7595. for (var i in shortcutkeys) {
  7596. var tmp = shortcutkeys[i].split(",");
  7597. for (var t = 0, ti; (ti = tmp[t++]); ) {
  7598. ti = ti.split(":");
  7599. var key = ti[0],
  7600. param = ti[1];
  7601. if (
  7602. /^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) ||
  7603. /^(\d+)$/.test(key)
  7604. ) {
  7605. if (
  7606. ((RegExp.$1 == "ctrl" ? e.ctrlKey || e.metaKey : 0) &&
  7607. (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1) &&
  7608. keyCode == RegExp.$3) ||
  7609. keyCode == RegExp.$1
  7610. ) {
  7611. if (me.queryCommandState(i, param) != -1)
  7612. me.execCommand(i, param);
  7613. domUtils.preventDefault(e);
  7614. }
  7615. }
  7616. }
  7617. }
  7618. });
  7619. },
  7620. /**
  7621. * 获取编辑器的内容
  7622. * @method getContent
  7623. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7624. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7625. * @example
  7626. * ```javascript
  7627. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7628. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7629. * ```
  7630. */
  7631. /**
  7632. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7633. * @method getContent
  7634. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7635. * 代表当前编辑器的内容是否空,
  7636. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7637. * 经过内置过滤规则处理后的内容。
  7638. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7639. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7640. * @return { String } 编辑器的内容字符串
  7641. * @example
  7642. * ```javascript
  7643. * // editor 是一个编辑器的实例
  7644. * var content = editor.getContent( function ( editor ) {
  7645. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7646. * } );
  7647. * ```
  7648. */
  7649. getContent: function(cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7650. var me = this;
  7651. if (cmd && utils.isFunction(cmd)) {
  7652. fn = cmd;
  7653. cmd = "";
  7654. }
  7655. if (fn ? !fn() : !this.hasContents()) {
  7656. return "";
  7657. }
  7658. me.fireEvent("beforegetcontent");
  7659. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank);
  7660. me.filterOutputRule(root);
  7661. me.fireEvent("aftergetcontent", cmd, root);
  7662. return root.toHtml(formatter);
  7663. },
  7664. /**
  7665. * 取得完整的html代码,可以直接显示成完整的html文档
  7666. * @method getAllHtml
  7667. * @return { String } 编辑器的内容html文档字符串
  7668. * @eaxmple
  7669. * ```javascript
  7670. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7671. * ```
  7672. */
  7673. getAllHtml: function() {
  7674. var me = this,
  7675. headHtml = [],
  7676. html = "";
  7677. me.fireEvent("getAllHtml", headHtml);
  7678. if (browser.ie && browser.version > 8) {
  7679. var headHtmlForIE9 = "";
  7680. utils.each(me.document.styleSheets, function(si) {
  7681. headHtmlForIE9 += si.href
  7682. ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />'
  7683. : "<style>" + si.cssText + "</style>";
  7684. });
  7685. utils.each(me.document.getElementsByTagName("script"), function(si) {
  7686. headHtmlForIE9 += si.outerHTML;
  7687. });
  7688. }
  7689. return (
  7690. "<html><head>" +
  7691. (me.options.charset
  7692. ? '<meta http-equiv="Content-Type" content="text/html; charset=' +
  7693. me.options.charset +
  7694. '"/>'
  7695. : "") +
  7696. (headHtmlForIE9 ||
  7697. me.document.getElementsByTagName("head")[0].innerHTML) +
  7698. headHtml.join("\n") +
  7699. "</head>" +
  7700. "<body " +
  7701. (ie && browser.version < 9 ? 'class="view"' : "") +
  7702. ">" +
  7703. me.getContent(null, null, true) +
  7704. "</body></html>"
  7705. );
  7706. },
  7707. /**
  7708. * 得到编辑器的纯文本内容,但会保留段落格式
  7709. * @method getPlainTxt
  7710. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7711. * @example
  7712. * ```javascript
  7713. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7714. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7715. * ```
  7716. */
  7717. getPlainTxt: function() {
  7718. var reg = new RegExp(domUtils.fillChar, "g"),
  7719. html = this.body.innerHTML.replace(/[\n\r]/g, ""); //ie要先去了\n在处理
  7720. html = html
  7721. .replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, "\n")
  7722. .replace(/<br\/?>/gi, "\n")
  7723. .replace(/<[^>/]+>/g, "")
  7724. .replace(/(\n)?<\/([^>]+)>/g, function(a, b, c) {
  7725. return dtd.$block[c] ? "\n" : b ? b : "";
  7726. });
  7727. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7728. return html
  7729. .replace(reg, "")
  7730. .replace(/\u00a0/g, " ")
  7731. .replace(/&nbsp;/g, " ");
  7732. },
  7733. /**
  7734. * 获取编辑器中的纯文本内容,没有段落格式
  7735. * @method getContentTxt
  7736. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7737. * @example
  7738. * ```javascript
  7739. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7740. * console.log(editor.getPlainTxt()); //输出:"12
  7741. * ```
  7742. */
  7743. getContentTxt: function() {
  7744. var reg = new RegExp(domUtils.fillChar, "g");
  7745. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7746. return this.body[browser.ie ? "innerText" : "textContent"]
  7747. .replace(reg, "")
  7748. .replace(/\u00a0/g, " ");
  7749. },
  7750. /**
  7751. * 设置编辑器的内容,可修改编辑器当前的html内容
  7752. * @method setContent
  7753. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7754. * @warning 该方法会触发selectionchange事件
  7755. * @param { String } html 要插入的html内容
  7756. * @example
  7757. * ```javascript
  7758. * editor.getContent('<p>test</p>');
  7759. * ```
  7760. */
  7761. /**
  7762. * 设置编辑器的内容,可修改编辑器当前的html内容
  7763. * @method setContent
  7764. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7765. * @warning 该方法会触发selectionchange事件
  7766. * @param { String } html 要插入的html内容
  7767. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7768. * @example
  7769. * ```javascript
  7770. * //假设设置前的编辑器内容是 <p>old text</p>
  7771. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7772. * ```
  7773. */
  7774. setContent: function(html, isAppendTo, notFireSelectionchange) {
  7775. var me = this;
  7776. me.fireEvent("beforesetcontent", html);
  7777. var root = UE.htmlparser(html);
  7778. me.filterInputRule(root);
  7779. html = root.toHtml();
  7780. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : "") + html;
  7781. function isCdataDiv(node) {
  7782. return node.tagName == "DIV" && node.getAttribute("cdata_tag");
  7783. }
  7784. //给文本或者inline节点套p标签
  7785. if (me.options.enterTag == "p") {
  7786. var child = this.body.firstChild,
  7787. tmpNode;
  7788. if (
  7789. !child ||
  7790. (child.nodeType == 1 &&
  7791. (dtd.$cdata[child.tagName] ||
  7792. isCdataDiv(child) ||
  7793. domUtils.isCustomeNode(child)) &&
  7794. child === this.body.lastChild)
  7795. ) {
  7796. this.body.innerHTML =
  7797. "<p>" +
  7798. (browser.ie ? "&nbsp;" : "<br/>") +
  7799. "</p>" +
  7800. this.body.innerHTML;
  7801. } else {
  7802. var p = me.document.createElement("p");
  7803. while (child) {
  7804. while (
  7805. child &&
  7806. (child.nodeType == 3 ||
  7807. (child.nodeType == 1 &&
  7808. dtd.p[child.tagName] &&
  7809. !dtd.$cdata[child.tagName]))
  7810. ) {
  7811. tmpNode = child.nextSibling;
  7812. p.appendChild(child);
  7813. child = tmpNode;
  7814. }
  7815. if (p.firstChild) {
  7816. if (!child) {
  7817. me.body.appendChild(p);
  7818. break;
  7819. } else {
  7820. child.parentNode.insertBefore(p, child);
  7821. p = me.document.createElement("p");
  7822. }
  7823. }
  7824. child = child.nextSibling;
  7825. }
  7826. }
  7827. }
  7828. me.fireEvent("aftersetcontent");
  7829. me.fireEvent("contentchange");
  7830. !notFireSelectionchange && me._selectionChange();
  7831. //清除保存的选区
  7832. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  7833. //trace:1742 setContent后gecko能得到焦点问题
  7834. var geckoSel;
  7835. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7836. geckoSel.removeAllRanges();
  7837. }
  7838. if (me.options.autoSyncData) {
  7839. me.form && setValue(me.form, me);
  7840. }
  7841. },
  7842. /**
  7843. * 让编辑器获得焦点,默认focus到编辑器头部
  7844. * @method focus
  7845. * @example
  7846. * ```javascript
  7847. * editor.focus()
  7848. * ```
  7849. */
  7850. /**
  7851. * 让编辑器获得焦点,toEnd确定focus位置
  7852. * @method focus
  7853. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7854. * @example
  7855. * ```javascript
  7856. * editor.focus(true)
  7857. * ```
  7858. */
  7859. focus: function(toEnd) {
  7860. try {
  7861. var me = this,
  7862. rng = me.selection.getRange();
  7863. if (toEnd) {
  7864. var node = me.body.lastChild;
  7865. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7866. if (domUtils.isEmptyBlock(node)) {
  7867. rng.setStartAtFirst(node);
  7868. } else {
  7869. rng.setStartAtLast(node);
  7870. }
  7871. rng.collapse(true);
  7872. }
  7873. rng.setCursor(true);
  7874. } else {
  7875. if (
  7876. !rng.collapsed &&
  7877. domUtils.isBody(rng.startContainer) &&
  7878. rng.startOffset == 0
  7879. ) {
  7880. var node = me.body.firstChild;
  7881. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7882. rng.setStartAtFirst(node).collapse(true);
  7883. }
  7884. }
  7885. rng.select(true);
  7886. }
  7887. this.fireEvent("focus selectionchange");
  7888. } catch (e) {}
  7889. },
  7890. isFocus: function() {
  7891. return this.selection.isFocus();
  7892. },
  7893. blur: function() {
  7894. var sel = this.selection.getNative();
  7895. if (sel.empty && browser.ie) {
  7896. var nativeRng = document.body.createTextRange();
  7897. nativeRng.moveToElementText(document.body);
  7898. nativeRng.collapse(true);
  7899. nativeRng.select();
  7900. sel.empty();
  7901. } else {
  7902. sel.removeAllRanges();
  7903. }
  7904. //this.fireEvent('blur selectionchange');
  7905. },
  7906. /**
  7907. * 初始化UE事件及部分事件代理
  7908. * @method _initEvents
  7909. * @private
  7910. */
  7911. _initEvents: function() {
  7912. var me = this,
  7913. doc = me.document,
  7914. win = me.window;
  7915. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  7916. domUtils.on(
  7917. doc,
  7918. [
  7919. "click",
  7920. "contextmenu",
  7921. "mousedown",
  7922. "keydown",
  7923. "keyup",
  7924. "keypress",
  7925. "mouseup",
  7926. "mouseover",
  7927. "mouseout",
  7928. "selectstart"
  7929. ],
  7930. me._proxyDomEvent
  7931. );
  7932. domUtils.on(win, ["focus", "blur"], me._proxyDomEvent);
  7933. domUtils.on(me.body, "drop", function(e) {
  7934. //阻止ff下默认的弹出新页面打开图片
  7935. if (browser.gecko && e.stopPropagation) {
  7936. e.stopPropagation();
  7937. }
  7938. me.fireEvent("contentchange");
  7939. });
  7940. domUtils.on(doc, ["mouseup", "keydown"], function(evt) {
  7941. //特殊键不触发selectionchange
  7942. if (
  7943. evt.type == "keydown" &&
  7944. (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)
  7945. ) {
  7946. return;
  7947. }
  7948. if (evt.button == 2) return;
  7949. me._selectionChange(250, evt);
  7950. });
  7951. },
  7952. /**
  7953. * 触发事件代理
  7954. * @method _proxyDomEvent
  7955. * @private
  7956. * @return { * } fireEvent的返回值
  7957. * @see UE.EventBase:fireEvent(String)
  7958. */
  7959. _proxyDomEvent: function(evt) {
  7960. if (
  7961. this.fireEvent("before" + evt.type.replace(/^on/, "").toLowerCase()) ===
  7962. false
  7963. ) {
  7964. return false;
  7965. }
  7966. if (this.fireEvent(evt.type.replace(/^on/, ""), evt) === false) {
  7967. return false;
  7968. }
  7969. return this.fireEvent(
  7970. "after" + evt.type.replace(/^on/, "").toLowerCase()
  7971. );
  7972. },
  7973. /**
  7974. * 变化选区
  7975. * @method _selectionChange
  7976. * @private
  7977. */
  7978. _selectionChange: function(delay, evt) {
  7979. var me = this;
  7980. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  7981. // if ( !me.selection.isFocus() ){
  7982. // return;
  7983. // }
  7984. var hackForMouseUp = false;
  7985. var mouseX, mouseY;
  7986. if (browser.ie && browser.version < 9 && evt && evt.type == "mouseup") {
  7987. var range = this.selection.getRange();
  7988. if (!range.collapsed) {
  7989. hackForMouseUp = true;
  7990. mouseX = evt.clientX;
  7991. mouseY = evt.clientY;
  7992. }
  7993. }
  7994. clearTimeout(_selectionChangeTimer);
  7995. _selectionChangeTimer = setTimeout(function() {
  7996. if (!me.selection || !me.selection.getNative()) {
  7997. return;
  7998. }
  7999. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  8000. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  8001. var ieRange;
  8002. if (hackForMouseUp && me.selection.getNative().type == "None") {
  8003. ieRange = me.document.body.createTextRange();
  8004. try {
  8005. ieRange.moveToPoint(mouseX, mouseY);
  8006. } catch (ex) {
  8007. ieRange = null;
  8008. }
  8009. }
  8010. var bakGetIERange;
  8011. if (ieRange) {
  8012. bakGetIERange = me.selection.getIERange;
  8013. me.selection.getIERange = function() {
  8014. return ieRange;
  8015. };
  8016. }
  8017. me.selection.cache();
  8018. if (bakGetIERange) {
  8019. me.selection.getIERange = bakGetIERange;
  8020. }
  8021. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  8022. me.fireEvent("beforeselectionchange");
  8023. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  8024. me.fireEvent("selectionchange", !!evt);
  8025. me.fireEvent("afterselectionchange");
  8026. me.selection.clear();
  8027. }
  8028. }, delay || 50);
  8029. },
  8030. /**
  8031. * 执行编辑命令
  8032. * @method _callCmdFn
  8033. * @private
  8034. * @param { String } fnName 函数名称
  8035. * @param { * } args 传给命令函数的参数
  8036. * @return { * } 返回命令函数运行的返回值
  8037. */
  8038. _callCmdFn: function(fnName, args) {
  8039. var cmdName = args[0].toLowerCase(),
  8040. cmd,
  8041. cmdFn;
  8042. cmd = this.commands[cmdName] || UE.commands[cmdName];
  8043. cmdFn = cmd && cmd[fnName];
  8044. //没有querycommandstate或者没有command的都默认返回0
  8045. if ((!cmd || !cmdFn) && fnName == "queryCommandState") {
  8046. return 0;
  8047. } else if (cmdFn) {
  8048. return cmdFn.apply(this, args);
  8049. }
  8050. },
  8051. /**
  8052. * 执行编辑命令cmdName,完成富文本编辑效果
  8053. * @method execCommand
  8054. * @param { String } cmdName 需要执行的命令
  8055. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8056. * @return { * } 返回命令函数运行的返回值
  8057. * @example
  8058. * ```javascript
  8059. * editor.execCommand(cmdName);
  8060. * ```
  8061. */
  8062. execCommand: function(cmdName) {
  8063. cmdName = cmdName.toLowerCase();
  8064. var me = this;
  8065. var result;
  8066. var cmd = me.commands[cmdName] || UE.commands[cmdName];
  8067. if (!cmd || !cmd.execCommand) {
  8068. return null;
  8069. }
  8070. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  8071. me.__hasEnterExecCommand = true;
  8072. if (me.queryCommandState.apply(me, arguments) != -1) {
  8073. me.fireEvent("saveScene");
  8074. me.fireEvent.apply(
  8075. me,
  8076. ["beforeexeccommand", cmdName].concat(arguments)
  8077. );
  8078. result = this._callCmdFn("execCommand", arguments);
  8079. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  8080. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  8081. me.fireEvent.apply(
  8082. me,
  8083. ["afterexeccommand", cmdName].concat(arguments)
  8084. );
  8085. me.fireEvent("saveScene");
  8086. }
  8087. me.__hasEnterExecCommand = false;
  8088. } else {
  8089. result = this._callCmdFn("execCommand", arguments);
  8090. !me.__hasEnterExecCommand &&
  8091. !cmd.ignoreContentChange &&
  8092. !me._ignoreContentChange &&
  8093. me.fireEvent("contentchange");
  8094. }
  8095. !me.__hasEnterExecCommand &&
  8096. !cmd.ignoreContentChange &&
  8097. !me._ignoreContentChange &&
  8098. me._selectionChange();
  8099. return result;
  8100. },
  8101. /**
  8102. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  8103. * @method queryCommandState
  8104. * @param { String } cmdName 需要查询的命令名称
  8105. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8106. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  8107. * @example
  8108. * ```javascript
  8109. * editor.queryCommandState(cmdName) => (-1|0|1)
  8110. * ```
  8111. * @see COMMAND.LIST
  8112. */
  8113. queryCommandState: function(cmdName) {
  8114. return this._callCmdFn("queryCommandState", arguments);
  8115. },
  8116. /**
  8117. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  8118. * @method queryCommandValue
  8119. * @param { String } cmdName 需要查询的命令名称
  8120. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8121. * @remind 只有部分插件有此方法
  8122. * @return { * } 返回每个命令特定的当前状态值
  8123. * @grammar editor.queryCommandValue(cmdName) => {*}
  8124. * @see COMMAND.LIST
  8125. */
  8126. queryCommandValue: function(cmdName) {
  8127. return this._callCmdFn("queryCommandValue", arguments);
  8128. },
  8129. /**
  8130. * 检查编辑区域中是否有内容
  8131. * @method hasContents
  8132. * @remind 默认有文本内容,或者有以下节点都不认为是空
  8133. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  8134. * @return { Boolean } 检查有内容返回true,否则返回false
  8135. * @example
  8136. * ```javascript
  8137. * editor.hasContents()
  8138. * ```
  8139. */
  8140. /**
  8141. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  8142. * @method hasContents
  8143. * @param { Array } tags 传入数组判断时用到的节点类型
  8144. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  8145. * @example
  8146. * ```javascript
  8147. * editor.hasContents(['span']);
  8148. * ```
  8149. */
  8150. hasContents: function(tags) {
  8151. if (tags) {
  8152. for (var i = 0, ci; (ci = tags[i++]); ) {
  8153. if (this.document.getElementsByTagName(ci).length > 0) {
  8154. return true;
  8155. }
  8156. }
  8157. }
  8158. if (!domUtils.isEmptyBlock(this.body)) {
  8159. return true;
  8160. }
  8161. //随时添加,定义的特殊标签如果存在,不能认为是空
  8162. tags = ["div"];
  8163. for (i = 0; (ci = tags[i++]); ) {
  8164. var nodes = domUtils.getElementsByTagName(this.document, ci);
  8165. for (var n = 0, cn; (cn = nodes[n++]); ) {
  8166. if (domUtils.isCustomeNode(cn)) {
  8167. return true;
  8168. }
  8169. }
  8170. }
  8171. return false;
  8172. },
  8173. /**
  8174. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  8175. * @method reset
  8176. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  8177. * @example
  8178. * ```javascript
  8179. * editor.reset()
  8180. * ```
  8181. */
  8182. reset: function() {
  8183. this.fireEvent("reset");
  8184. },
  8185. /**
  8186. * 设置当前编辑区域可以编辑
  8187. * @method setEnabled
  8188. * @example
  8189. * ```javascript
  8190. * editor.setEnabled()
  8191. * ```
  8192. */
  8193. setEnabled: function() {
  8194. var me = this,
  8195. range;
  8196. if (me.body.contentEditable == "false") {
  8197. me.body.contentEditable = true;
  8198. range = me.selection.getRange();
  8199. //有可能内容丢失了
  8200. try {
  8201. range.moveToBookmark(me.lastBk);
  8202. delete me.lastBk;
  8203. } catch (e) {
  8204. range.setStartAtFirst(me.body).collapse(true);
  8205. }
  8206. range.select(true);
  8207. if (me.bkqueryCommandState) {
  8208. me.queryCommandState = me.bkqueryCommandState;
  8209. delete me.bkqueryCommandState;
  8210. }
  8211. if (me.bkqueryCommandValue) {
  8212. me.queryCommandValue = me.bkqueryCommandValue;
  8213. delete me.bkqueryCommandValue;
  8214. }
  8215. me.fireEvent("selectionchange");
  8216. }
  8217. },
  8218. enable: function() {
  8219. return this.setEnabled();
  8220. },
  8221. /** 设置当前编辑区域不可编辑
  8222. * @method setDisabled
  8223. */
  8224. /** 设置当前编辑区域不可编辑,except中的命令除外
  8225. * @method setDisabled
  8226. * @param { String } except 例外命令的字符串
  8227. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8228. * @example
  8229. * ```javascript
  8230. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  8231. * ```
  8232. */
  8233. /** 设置当前编辑区域不可编辑,except中的命令除外
  8234. * @method setDisabled
  8235. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  8236. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8237. * @example
  8238. * ```javascript
  8239. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  8240. * ```
  8241. */
  8242. setDisabled: function(except) {
  8243. var me = this;
  8244. except = except ? (utils.isArray(except) ? except : [except]) : [];
  8245. if (me.body.contentEditable == "true") {
  8246. if (!me.lastBk) {
  8247. me.lastBk = me.selection.getRange().createBookmark(true);
  8248. }
  8249. me.body.contentEditable = false;
  8250. me.bkqueryCommandState = me.queryCommandState;
  8251. me.bkqueryCommandValue = me.queryCommandValue;
  8252. me.queryCommandState = function(type) {
  8253. if (utils.indexOf(except, type) != -1) {
  8254. return me.bkqueryCommandState.apply(me, arguments);
  8255. }
  8256. return -1;
  8257. };
  8258. me.queryCommandValue = function(type) {
  8259. if (utils.indexOf(except, type) != -1) {
  8260. return me.bkqueryCommandValue.apply(me, arguments);
  8261. }
  8262. return null;
  8263. };
  8264. me.fireEvent("selectionchange");
  8265. }
  8266. },
  8267. disable: function(except) {
  8268. return this.setDisabled(except);
  8269. },
  8270. /**
  8271. * 设置默认内容
  8272. * @method _setDefaultContent
  8273. * @private
  8274. * @param { String } cont 要存入的内容
  8275. */
  8276. _setDefaultContent: (function() {
  8277. function clear() {
  8278. var me = this;
  8279. if (me.document.getElementById("initContent")) {
  8280. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  8281. me.removeListener("firstBeforeExecCommand focus", clear);
  8282. setTimeout(function() {
  8283. me.focus();
  8284. me._selectionChange();
  8285. }, 0);
  8286. }
  8287. }
  8288. return function(cont) {
  8289. var me = this;
  8290. me.body.innerHTML = '<p id="initContent">' + cont + "</p>";
  8291. me.addListener("firstBeforeExecCommand focus", clear);
  8292. };
  8293. })(),
  8294. /**
  8295. * 显示编辑器
  8296. * @method setShow
  8297. * @example
  8298. * ```javascript
  8299. * editor.setShow()
  8300. * ```
  8301. */
  8302. setShow: function() {
  8303. var me = this,
  8304. range = me.selection.getRange();
  8305. if (me.container.style.display == "none") {
  8306. //有可能内容丢失了
  8307. try {
  8308. range.moveToBookmark(me.lastBk);
  8309. delete me.lastBk;
  8310. } catch (e) {
  8311. range.setStartAtFirst(me.body).collapse(true);
  8312. }
  8313. //ie下focus实效,所以做了个延迟
  8314. setTimeout(function() {
  8315. range.select(true);
  8316. }, 100);
  8317. me.container.style.display = "";
  8318. }
  8319. },
  8320. show: function() {
  8321. return this.setShow();
  8322. },
  8323. /**
  8324. * 隐藏编辑器
  8325. * @method setHide
  8326. * @example
  8327. * ```javascript
  8328. * editor.setHide()
  8329. * ```
  8330. */
  8331. setHide: function() {
  8332. var me = this;
  8333. if (!me.lastBk) {
  8334. me.lastBk = me.selection.getRange().createBookmark(true);
  8335. }
  8336. me.container.style.display = "none";
  8337. },
  8338. hide: function() {
  8339. return this.setHide();
  8340. },
  8341. /**
  8342. * 根据指定的路径,获取对应的语言资源
  8343. * @method getLang
  8344. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  8345. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  8346. * @example
  8347. * ```javascript
  8348. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  8349. * ```
  8350. */
  8351. getLang: function(path) {
  8352. var lang = UE.I18N[this.options.lang];
  8353. if (!lang) {
  8354. throw Error("not import language file");
  8355. }
  8356. path = (path || "").split(".");
  8357. for (var i = 0, ci; (ci = path[i++]); ) {
  8358. lang = lang[ci];
  8359. if (!lang) break;
  8360. }
  8361. return lang;
  8362. },
  8363. /**
  8364. * 计算编辑器html内容字符串的长度
  8365. * @method getContentLength
  8366. * @return { Number } 返回计算的长度
  8367. * @example
  8368. * ```javascript
  8369. * //编辑器html内容<p><strong>132</strong></p>
  8370. * editor.getContentLength() //返回27
  8371. * ```
  8372. */
  8373. /**
  8374. * 计算编辑器当前纯文本内容的长度
  8375. * @method getContentLength
  8376. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  8377. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  8378. * @example
  8379. * ```javascript
  8380. * //编辑器html内容<p><strong>132</strong></p>
  8381. * editor.getContentLength() //返回3
  8382. * ```
  8383. */
  8384. getContentLength: function(ingoneHtml, tagNames) {
  8385. var count = this.getContent(false, false, true).length;
  8386. if (ingoneHtml) {
  8387. tagNames = (tagNames || []).concat(["hr", "img", "iframe"]);
  8388. count = this.getContentTxt().replace(/[\t\r\n]+/g, "").length;
  8389. for (var i = 0, ci; (ci = tagNames[i++]); ) {
  8390. count += this.document.getElementsByTagName(ci).length;
  8391. }
  8392. }
  8393. return count;
  8394. },
  8395. /**
  8396. * 注册输入过滤规则
  8397. * @method addInputRule
  8398. * @param { Function } rule 要添加的过滤规则
  8399. * @example
  8400. * ```javascript
  8401. * editor.addInputRule(function(root){
  8402. * $.each(root.getNodesByTagName('div'),function(i,node){
  8403. * node.tagName="p";
  8404. * });
  8405. * });
  8406. * ```
  8407. */
  8408. addInputRule: function(rule) {
  8409. this.inputRules.push(rule);
  8410. },
  8411. /**
  8412. * 执行注册的过滤规则
  8413. * @method filterInputRule
  8414. * @param { UE.uNode } root 要过滤的uNode节点
  8415. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  8416. * @example
  8417. * ```javascript
  8418. * editor.filterInputRule(editor.body);
  8419. * ```
  8420. * @see UE.Editor:addInputRule
  8421. */
  8422. filterInputRule: function(root) {
  8423. for (var i = 0, ci; (ci = this.inputRules[i++]); ) {
  8424. ci.call(this, root);
  8425. }
  8426. },
  8427. /**
  8428. * 注册输出过滤规则
  8429. * @method addOutputRule
  8430. * @param { Function } rule 要添加的过滤规则
  8431. * @example
  8432. * ```javascript
  8433. * editor.addOutputRule(function(root){
  8434. * $.each(root.getNodesByTagName('p'),function(i,node){
  8435. * node.tagName="div";
  8436. * });
  8437. * });
  8438. * ```
  8439. */
  8440. addOutputRule: function(rule) {
  8441. this.outputRules.push(rule);
  8442. },
  8443. /**
  8444. * 根据输出过滤规则,过滤编辑器内容
  8445. * @method filterOutputRule
  8446. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8447. * @param { UE.uNode } root 要过滤的uNode节点
  8448. * @example
  8449. * ```javascript
  8450. * editor.filterOutputRule(editor.body);
  8451. * ```
  8452. * @see UE.Editor:addOutputRule
  8453. */
  8454. filterOutputRule: function(root) {
  8455. for (var i = 0, ci; (ci = this.outputRules[i++]); ) {
  8456. ci.call(this, root);
  8457. }
  8458. },
  8459. /**
  8460. * 根据action名称获取请求的路径
  8461. * @method getActionUrl
  8462. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8463. * @param { String } action action名称
  8464. * @example
  8465. * ```javascript
  8466. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8467. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8468. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8469. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8470. * ```
  8471. */
  8472. getActionUrl: function(action) {
  8473. var actionName = this.getOpt(action) || action,
  8474. imageUrl = this.getOpt("imageUrl"),
  8475. serverUrl = this.getOpt("serverUrl");
  8476. /* if (!serverUrl && imageUrl) {
  8477. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, "$1controller$2");
  8478. }
  8479. if (serverUrl) {
  8480. serverUrl =
  8481. serverUrl +
  8482. (serverUrl.indexOf("?") == -1 ? "?" : "&") +
  8483. "action=" +
  8484. (actionName || "");
  8485. return utils.formatUrl(serverUrl);
  8486. } else {
  8487. return "";
  8488. } */
  8489. if (serverUrl) {
  8490. serverUrl = serverUrl + "?";
  8491. return utils.formatUrl(serverUrl);
  8492. } else {
  8493. return "";
  8494. }
  8495. }
  8496. };
  8497. utils.inherits(Editor, EventBase);
  8498. })();
  8499. // core/Editor.defaultoptions.js
  8500. //维护编辑器一下默认的不在插件中的配置项
  8501. UE.Editor.defaultOptions = function(editor) {
  8502. var _url = editor.options.UEDITOR_HOME_URL;
  8503. return {
  8504. isShow: true,
  8505. initialContent: "",
  8506. initialStyle: "",
  8507. autoClearinitialContent: false,
  8508. iframeCssUrl: _url + "themes/iframe.css",
  8509. textarea: "editorValue",
  8510. focus: false,
  8511. focusInEnd: true,
  8512. autoClearEmptyNode: true,
  8513. fullscreen: false,
  8514. readonly: false,
  8515. zIndex: 999,
  8516. imagePopup: true,
  8517. enterTag: "p",
  8518. customDomain: false,
  8519. lang: "zh-cn",
  8520. langPath: _url + "i18n/",
  8521. theme: "default",
  8522. themePath: _url + "themes/",
  8523. allHtmlEnabled: false,
  8524. scaleEnabled: false,
  8525. tableNativeEditInFF: false,
  8526. autoSyncData: true,
  8527. fileNameFormat: "{time}{rand:6}"
  8528. };
  8529. };
  8530. // core/loadconfig.js
  8531. ;(function() {
  8532. UE.Editor.prototype.loadServerConfig = function() {
  8533. var me = this;
  8534. setTimeout(function() {
  8535. try {
  8536. me.options.imageUrl &&
  8537. me.setOpt(
  8538. "serverUrl",
  8539. me.options.imageUrl.replace(
  8540. /^(.*[\/]).+([\.].+)$/,
  8541. "$1controller$2"
  8542. )
  8543. );
  8544. var configUrl = me.getActionUrl("config"),
  8545. isJsonp = utils.isCrossDomainUrl(configUrl);
  8546. /* 发出ajax请求 */
  8547. me._serverConfigLoaded = false;
  8548. configUrl &&
  8549. UE.ajax.request(configUrl, {
  8550. method: "GET",
  8551. dataType: isJsonp ? "jsonp" : "",
  8552. onsuccess: function(r) {
  8553. try {
  8554. var config = isJsonp ? r : eval("(" + r.responseText + ")");
  8555. utils.extend(me.options, config);
  8556. me.fireEvent("serverConfigLoaded");
  8557. me._serverConfigLoaded = true;
  8558. } catch (e) {
  8559. showErrorMsg(me.getLang("loadconfigFormatError"));
  8560. }
  8561. },
  8562. onerror: function() {
  8563. showErrorMsg(me.getLang("loadconfigHttpError"));
  8564. }
  8565. });
  8566. } catch (e) {
  8567. showErrorMsg(me.getLang("loadconfigError"));
  8568. }
  8569. });
  8570. function showErrorMsg(msg) {
  8571. console && console.error(msg);
  8572. //me.fireEvent('showMessage', {
  8573. // 'title': msg,
  8574. // 'type': 'error'
  8575. //});
  8576. }
  8577. };
  8578. UE.Editor.prototype.isServerConfigLoaded = function() {
  8579. var me = this;
  8580. return me._serverConfigLoaded || false;
  8581. };
  8582. UE.Editor.prototype.afterConfigReady = function(handler) {
  8583. if (!handler || !utils.isFunction(handler)) return;
  8584. var me = this;
  8585. var readyHandler = function() {
  8586. handler.apply(me, arguments);
  8587. me.removeListener("serverConfigLoaded", readyHandler);
  8588. };
  8589. if (me.isServerConfigLoaded()) {
  8590. handler.call(me, "serverConfigLoaded");
  8591. } else {
  8592. me.addListener("serverConfigLoaded", readyHandler);
  8593. }
  8594. };
  8595. })();
  8596. // core/ajax.js
  8597. /**
  8598. * @file
  8599. * @module UE.ajax
  8600. * @since 1.2.6.1
  8601. */
  8602. /**
  8603. * 提供对ajax请求的支持
  8604. * @module UE.ajax
  8605. */
  8606. UE.ajax = (function() {
  8607. //创建一个ajaxRequest对象
  8608. var fnStr = "XMLHttpRequest()";
  8609. try {
  8610. new ActiveXObject("Msxml2.XMLHTTP");
  8611. fnStr = "ActiveXObject('Msxml2.XMLHTTP')";
  8612. } catch (e) {
  8613. try {
  8614. new ActiveXObject("Microsoft.XMLHTTP");
  8615. fnStr = "ActiveXObject('Microsoft.XMLHTTP')";
  8616. } catch (e) {}
  8617. }
  8618. var creatAjaxRequest = new Function("return new " + fnStr);
  8619. /**
  8620. * 将json参数转化成适合ajax提交的参数列表
  8621. * @param json
  8622. */
  8623. function json2str(json) {
  8624. var strArr = [];
  8625. for (var i in json) {
  8626. //忽略默认的几个参数
  8627. if (
  8628. i == "method" ||
  8629. i == "timeout" ||
  8630. i == "async" ||
  8631. i == "dataType" ||
  8632. i == "callback"
  8633. )
  8634. continue;
  8635. //忽略控制
  8636. if (json[i] == undefined || json[i] == null) continue;
  8637. //传递过来的对象和函数不在提交之列
  8638. if (
  8639. !(
  8640. (typeof json[i]).toLowerCase() == "function" ||
  8641. (typeof json[i]).toLowerCase() == "object"
  8642. )
  8643. ) {
  8644. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  8645. } else if (utils.isArray(json[i])) {
  8646. //支持传数组内容
  8647. for (var j = 0; j < json[i].length; j++) {
  8648. strArr.push(
  8649. encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j])
  8650. );
  8651. }
  8652. }
  8653. }
  8654. return strArr.join("&");
  8655. }
  8656. function doAjax(url, ajaxOptions) {
  8657. var xhr = creatAjaxRequest(),
  8658. //是否超时
  8659. timeIsOut = false,
  8660. //默认参数
  8661. defaultAjaxOptions = {
  8662. method: "POST",
  8663. timeout: 5000,
  8664. async: true,
  8665. data: {}, //需要传递对象的话只能覆盖
  8666. onsuccess: function() {},
  8667. onerror: function() {}
  8668. };
  8669. if (typeof url === "object") {
  8670. ajaxOptions = url;
  8671. url = ajaxOptions.url;
  8672. }
  8673. if (!xhr || !url) return;
  8674. var ajaxOpts = ajaxOptions
  8675. ? utils.extend(defaultAjaxOptions, ajaxOptions)
  8676. : defaultAjaxOptions;
  8677. var submitStr = json2str(ajaxOpts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8678. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8679. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8680. submitStr += (submitStr ? "&" : "") + json2str(ajaxOpts.data);
  8681. }
  8682. //超时检测
  8683. var timerID = setTimeout(function() {
  8684. if (xhr.readyState != 4) {
  8685. timeIsOut = true;
  8686. xhr.abort();
  8687. clearTimeout(timerID);
  8688. }
  8689. }, ajaxOpts.timeout);
  8690. var method = ajaxOpts.method.toUpperCase();
  8691. var str =
  8692. url +
  8693. (url.indexOf("?") == -1 ? "?" : "&") +
  8694. (method == "POST" ? "" : submitStr + "&noCache=" + +new Date());
  8695. xhr.open(method, str, ajaxOpts.async);
  8696. xhr.onreadystatechange = function() {
  8697. if (xhr.readyState == 4) {
  8698. if (!timeIsOut && xhr.status == 200) {
  8699. ajaxOpts.onsuccess(xhr);
  8700. } else {
  8701. ajaxOpts.onerror(xhr);
  8702. }
  8703. }
  8704. };
  8705. if (method == "POST") {
  8706. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  8707. xhr.send(submitStr);
  8708. } else {
  8709. xhr.send(null);
  8710. }
  8711. }
  8712. function doJsonp(url, opts) {
  8713. var successhandler = opts.onsuccess || function() {},
  8714. scr = document.createElement("SCRIPT"),
  8715. options = opts || {},
  8716. charset = options["charset"],
  8717. callbackField = options["jsonp"] || "callback",
  8718. callbackFnName,
  8719. timeOut = options["timeOut"] || 0,
  8720. timer,
  8721. reg = new RegExp("(\\?|&)" + callbackField + "=([^&]*)"),
  8722. matches;
  8723. if (utils.isFunction(successhandler)) {
  8724. callbackFnName =
  8725. "bd__editor__" + Math.floor(Math.random() * 2147483648).toString(36);
  8726. window[callbackFnName] = getCallBack(0);
  8727. } else if (utils.isString(successhandler)) {
  8728. callbackFnName = successhandler;
  8729. } else {
  8730. if ((matches = reg.exec(url))) {
  8731. callbackFnName = matches[2];
  8732. }
  8733. }
  8734. url = url.replace(reg, "\x241" + callbackField + "=" + callbackFnName);
  8735. if (url.search(reg) < 0) {
  8736. url +=
  8737. (url.indexOf("?") < 0 ? "?" : "&") +
  8738. callbackField +
  8739. "=" +
  8740. callbackFnName;
  8741. }
  8742. var queryStr = json2str(opts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8743. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8744. if (!utils.isEmptyObject(opts.data)) {
  8745. queryStr += (queryStr ? "&" : "") + json2str(opts.data);
  8746. }
  8747. if (queryStr) {
  8748. url = url.replace(/\?/, "?" + queryStr + "&");
  8749. }
  8750. scr.onerror = getCallBack(1);
  8751. if (timeOut) {
  8752. timer = setTimeout(getCallBack(1), timeOut);
  8753. }
  8754. createScriptTag(scr, url, charset);
  8755. function createScriptTag(scr, url, charset) {
  8756. scr.setAttribute("type", "text/javascript");
  8757. scr.setAttribute("defer", "defer");
  8758. charset && scr.setAttribute("charset", charset);
  8759. scr.setAttribute("src", url);
  8760. document.getElementsByTagName("head")[0].appendChild(scr);
  8761. }
  8762. function getCallBack(onTimeOut) {
  8763. return function() {
  8764. try {
  8765. if (onTimeOut) {
  8766. options.onerror && options.onerror();
  8767. } else {
  8768. try {
  8769. clearTimeout(timer);
  8770. successhandler.apply(window, arguments);
  8771. } catch (e) {}
  8772. }
  8773. } catch (exception) {
  8774. options.onerror && options.onerror.call(window, exception);
  8775. } finally {
  8776. options.oncomplete && options.oncomplete.apply(window, arguments);
  8777. scr.parentNode && scr.parentNode.removeChild(scr);
  8778. window[callbackFnName] = null;
  8779. try {
  8780. delete window[callbackFnName];
  8781. } catch (e) {}
  8782. }
  8783. };
  8784. }
  8785. }
  8786. return {
  8787. /**
  8788. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8789. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8790. * @method request
  8791. * @param { URLString } url ajax请求的url地址
  8792. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8793. * @example
  8794. * ```javascript
  8795. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8796. * UE.ajax.requeset( 'sayhello.php', {
  8797. *
  8798. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8799. * method: 'GET',
  8800. *
  8801. * //超时时间。 默认为5000, 单位是ms
  8802. * timeout: 10000,
  8803. *
  8804. * //是否是异步请求。 true为异步请求, false为同步请求
  8805. * async: true,
  8806. *
  8807. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8808. * data: {
  8809. * name: 'ueditor'
  8810. * },
  8811. *
  8812. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8813. * onsuccess: function ( xhr ) {
  8814. * console.log( xhr.responseText );
  8815. * },
  8816. *
  8817. * //请求失败或者超时后的回调。
  8818. * onerror: function ( xhr ) {
  8819. * alert( 'Ajax请求失败' );
  8820. * }
  8821. *
  8822. * } );
  8823. * ```
  8824. */
  8825. /**
  8826. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8827. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8828. * @method request
  8829. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8830. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8831. * @example
  8832. * ```javascript
  8833. *
  8834. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8835. * UE.ajax.requeset( 'sayhello.php', {
  8836. *
  8837. * //请求的地址, 该项是必须的。
  8838. * url: 'sayhello.php'
  8839. *
  8840. * } );
  8841. * ```
  8842. */
  8843. request: function(url, opts) {
  8844. if (opts && opts.dataType == "jsonp") {
  8845. doJsonp(url, opts);
  8846. } else {
  8847. doAjax(url, opts);
  8848. }
  8849. },
  8850. getJSONP: function(url, data, fn) {
  8851. var opts = {
  8852. data: data,
  8853. oncomplete: fn
  8854. };
  8855. doJsonp(url, opts);
  8856. }
  8857. };
  8858. })();
  8859. // core/filterword.js
  8860. /**
  8861. * UE过滤word的静态方法
  8862. * @file
  8863. */
  8864. /**
  8865. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8866. * @module UE
  8867. */
  8868. /**
  8869. * 根据传入html字符串过滤word
  8870. * @module UE
  8871. * @since 1.2.6.1
  8872. * @method filterWord
  8873. * @param { String } html html字符串
  8874. * @return { String } 已过滤后的结果字符串
  8875. * @example
  8876. * ```javascript
  8877. * UE.filterWord(html);
  8878. * ```
  8879. */
  8880. var filterWord = (UE.filterWord = (function() {
  8881. //是否是word过来的内容
  8882. function isWordDocument(str) {
  8883. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/gi.test(
  8884. str
  8885. );
  8886. }
  8887. //去掉小数
  8888. function transUnit(v) {
  8889. v = v.replace(/[\d.]+\w+/g, function(m) {
  8890. return utils.transUnitToPx(m);
  8891. });
  8892. return v;
  8893. }
  8894. function filterPasteWord(str) {
  8895. return (
  8896. str
  8897. .replace(/[\t\r\n]+/g, " ")
  8898. .replace(/<!--[\s\S]*?-->/gi, "")
  8899. //转换图片
  8900. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function(str) {
  8901. //opera能自己解析出image所这里直接返回空
  8902. if (browser.opera) {
  8903. return "";
  8904. }
  8905. try {
  8906. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8907. if (/Bitmap/i.test(str)) {
  8908. return "";
  8909. }
  8910. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8911. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8912. src = str.match(/src=\s*"([^"]*)"/i)[1];
  8913. return (
  8914. '<img width="' +
  8915. transUnit(width) +
  8916. '" height="' +
  8917. transUnit(height) +
  8918. '" src="' +
  8919. src +
  8920. '" />'
  8921. );
  8922. } catch (e) {
  8923. return "";
  8924. }
  8925. })
  8926. //针对wps添加的多余标签处理
  8927. .replace(/<\/?div[^>]*>/g, "")
  8928. //去掉多余的属性
  8929. .replace(/v:\w+=(["']?)[^'"]+\1/g, "")
  8930. .replace(
  8931. /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi,
  8932. ""
  8933. )
  8934. .replace(
  8935. /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi,
  8936. "<p><strong>$1</strong></p>"
  8937. )
  8938. //去掉多余的属性
  8939. .replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/gi, function(
  8940. str,
  8941. name,
  8942. marks,
  8943. val
  8944. ) {
  8945. //保留list的标示
  8946. return name == "class" && val == "MsoListParagraph" ? str : "";
  8947. })
  8948. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8949. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function(a, b, c) {
  8950. return c.replace(/[\t\r\n ]+/g, " ");
  8951. })
  8952. //处理style的问题
  8953. .replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function(
  8954. str,
  8955. tag,
  8956. tmp,
  8957. style
  8958. ) {
  8959. var n = [],
  8960. s = style
  8961. .replace(/^\s+|\s+$/, "")
  8962. .replace(/&#39;/g, "'")
  8963. .replace(/&quot;/gi, "'")
  8964. .replace(/[\d.]+(cm|pt)/g, function(str) {
  8965. return utils.transUnitToPx(str);
  8966. })
  8967. .split(/;\s*/g);
  8968. for (var i = 0, v; (v = s[i]); i++) {
  8969. var name,
  8970. value,
  8971. parts = v.split(":");
  8972. if (parts.length == 2) {
  8973. name = parts[0].toLowerCase();
  8974. value = parts[1].toLowerCase();
  8975. if (
  8976. (/^(background)\w*/.test(name) &&
  8977. value.replace(/(initial|\s)/g, "").length == 0) ||
  8978. (/^(margin)\w*/.test(name) && /^0\w+$/.test(value))
  8979. ) {
  8980. continue;
  8981. }
  8982. switch (name) {
  8983. case "mso-padding-alt":
  8984. case "mso-padding-top-alt":
  8985. case "mso-padding-right-alt":
  8986. case "mso-padding-bottom-alt":
  8987. case "mso-padding-left-alt":
  8988. case "mso-margin-alt":
  8989. case "mso-margin-top-alt":
  8990. case "mso-margin-right-alt":
  8991. case "mso-margin-bottom-alt":
  8992. case "mso-margin-left-alt":
  8993. //ie下会出现挤到一起的情况
  8994. //case "mso-table-layout-alt":
  8995. case "mso-height":
  8996. case "mso-width":
  8997. case "mso-vertical-align-alt":
  8998. //trace:1819 ff下会解析出padding在table上
  8999. if (!/<table/.test(tag))
  9000. n[i] =
  9001. name.replace(/^mso-|-alt$/g, "") + ":" + transUnit(value);
  9002. continue;
  9003. case "horiz-align":
  9004. n[i] = "text-align:" + value;
  9005. continue;
  9006. case "vert-align":
  9007. n[i] = "vertical-align:" + value;
  9008. continue;
  9009. case "font-color":
  9010. case "mso-foreground":
  9011. n[i] = "color:" + value;
  9012. continue;
  9013. case "mso-background":
  9014. case "mso-highlight":
  9015. n[i] = "background:" + value;
  9016. continue;
  9017. case "mso-default-height":
  9018. n[i] = "min-height:" + transUnit(value);
  9019. continue;
  9020. case "mso-default-width":
  9021. n[i] = "min-width:" + transUnit(value);
  9022. continue;
  9023. case "mso-padding-between-alt":
  9024. n[i] =
  9025. "border-collapse:separate;border-spacing:" +
  9026. transUnit(value);
  9027. continue;
  9028. case "text-line-through":
  9029. if (value == "single" || value == "double") {
  9030. n[i] = "text-decoration:line-through";
  9031. }
  9032. continue;
  9033. case "mso-zero-height":
  9034. if (value == "yes") {
  9035. n[i] = "display:none";
  9036. }
  9037. continue;
  9038. // case 'background':
  9039. // break;
  9040. case "margin":
  9041. if (!/[1-9]/.test(value)) {
  9042. continue;
  9043. }
  9044. }
  9045. if (
  9046. /^(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(
  9047. name
  9048. ) ||
  9049. (/text\-indent|padding|margin/.test(name) &&
  9050. /\-[\d.]+/.test(value))
  9051. ) {
  9052. continue;
  9053. }
  9054. n[i] = name + ":" + parts[1];
  9055. }
  9056. }
  9057. return (
  9058. tag +
  9059. (n.length
  9060. ? ' style="' + n.join(";").replace(/;{2,}/g, ";") + '"'
  9061. : "")
  9062. );
  9063. })
  9064. );
  9065. }
  9066. return function(html) {
  9067. return isWordDocument(html) ? filterPasteWord(html) : html;
  9068. };
  9069. })());
  9070. // core/node.js
  9071. /**
  9072. * 编辑器模拟的节点类
  9073. * @file
  9074. * @module UE
  9075. * @class uNode
  9076. * @since 1.2.6.1
  9077. */
  9078. /**
  9079. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9080. * @unfile
  9081. * @module UE
  9082. */
  9083. ;(function() {
  9084. /**
  9085. * 编辑器模拟的节点类
  9086. * @unfile
  9087. * @module UE
  9088. * @class uNode
  9089. */
  9090. /**
  9091. * 通过一个键值对,创建一个uNode对象
  9092. * @constructor
  9093. * @param { Object } attr 传入要创建的uNode的初始属性
  9094. * @example
  9095. * ```javascript
  9096. * var node = new uNode({
  9097. * type:'element',
  9098. * tagName:'span',
  9099. * attrs:{style:'font-size:14px;'}
  9100. * })
  9101. * ```
  9102. */
  9103. var uNode = (UE.uNode = function(obj) {
  9104. this.type = obj.type;
  9105. this.data = obj.data;
  9106. this.tagName = obj.tagName;
  9107. this.parentNode = obj.parentNode;
  9108. this.attrs = obj.attrs || {};
  9109. this.children = obj.children;
  9110. });
  9111. var notTransAttrs = {
  9112. href: 1,
  9113. src: 1,
  9114. _src: 1,
  9115. _href: 1,
  9116. cdata_data: 1
  9117. };
  9118. var notTransTagName = {
  9119. style: 1,
  9120. script: 1
  9121. };
  9122. var indentChar = " ",
  9123. breakChar = "\n";
  9124. function insertLine(arr, current, begin) {
  9125. arr.push(breakChar);
  9126. return current + (begin ? 1 : -1);
  9127. }
  9128. function insertIndent(arr, current) {
  9129. //插入缩进
  9130. for (var i = 0; i < current; i++) {
  9131. arr.push(indentChar);
  9132. }
  9133. }
  9134. //创建uNode的静态方法
  9135. //支持标签和html
  9136. uNode.createElement = function(html) {
  9137. if (/[<>]/.test(html)) {
  9138. return UE.htmlparser(html).children[0];
  9139. } else {
  9140. return new uNode({
  9141. type: "element",
  9142. children: [],
  9143. tagName: html
  9144. });
  9145. }
  9146. };
  9147. uNode.createText = function(data, noTrans) {
  9148. return new UE.uNode({
  9149. type: "text",
  9150. data: noTrans ? data : utils.unhtml(data || "")
  9151. });
  9152. };
  9153. function nodeToHtml(node, arr, formatter, current) {
  9154. switch (node.type) {
  9155. case "root":
  9156. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9157. //插入新行
  9158. if (
  9159. formatter &&
  9160. ci.type == "element" &&
  9161. !dtd.$inlineWithA[ci.tagName] &&
  9162. i > 1
  9163. ) {
  9164. insertLine(arr, current, true);
  9165. insertIndent(arr, current);
  9166. }
  9167. nodeToHtml(ci, arr, formatter, current);
  9168. }
  9169. break;
  9170. case "text":
  9171. isText(node, arr);
  9172. break;
  9173. case "element":
  9174. isElement(node, arr, formatter, current);
  9175. break;
  9176. case "comment":
  9177. isComment(node, arr, formatter);
  9178. }
  9179. return arr;
  9180. }
  9181. function isText(node, arr) {
  9182. if (node.parentNode.tagName == "pre") {
  9183. //源码模式下输入html标签,不能做转换处理,直接输出
  9184. arr.push(node.data);
  9185. } else {
  9186. arr.push(
  9187. notTransTagName[node.parentNode.tagName]
  9188. ? utils.html(node.data)
  9189. : node.data.replace(/[ ]{2}/g, " &nbsp;")
  9190. );
  9191. }
  9192. }
  9193. function isElement(node, arr, formatter, current) {
  9194. var attrhtml = "";
  9195. if (node.attrs) {
  9196. attrhtml = [];
  9197. var attrs = node.attrs;
  9198. for (var a in attrs) {
  9199. //这里就针对
  9200. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  9201. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  9202. //有可能做的不够
  9203. attrhtml.push(
  9204. a +
  9205. (attrs[a] !== undefined
  9206. ? '="' +
  9207. (notTransAttrs[a]
  9208. ? utils.html(attrs[a]).replace(/["]/g, function(a) {
  9209. return "&quot;";
  9210. })
  9211. : utils.unhtml(attrs[a])) +
  9212. '"'
  9213. : "")
  9214. );
  9215. }
  9216. attrhtml = attrhtml.join(" ");
  9217. }
  9218. arr.push(
  9219. "<" +
  9220. node.tagName +
  9221. (attrhtml ? " " + attrhtml : "") +
  9222. (dtd.$empty[node.tagName] ? "/" : "") +
  9223. ">"
  9224. );
  9225. //插入新行
  9226. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != "pre") {
  9227. if (node.children && node.children.length) {
  9228. current = insertLine(arr, current, true);
  9229. insertIndent(arr, current);
  9230. }
  9231. }
  9232. if (node.children && node.children.length) {
  9233. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9234. if (
  9235. formatter &&
  9236. ci.type == "element" &&
  9237. !dtd.$inlineWithA[ci.tagName] &&
  9238. i > 1
  9239. ) {
  9240. insertLine(arr, current);
  9241. insertIndent(arr, current);
  9242. }
  9243. nodeToHtml(ci, arr, formatter, current);
  9244. }
  9245. }
  9246. if (!dtd.$empty[node.tagName]) {
  9247. if (
  9248. formatter &&
  9249. !dtd.$inlineWithA[node.tagName] &&
  9250. node.tagName != "pre"
  9251. ) {
  9252. if (node.children && node.children.length) {
  9253. current = insertLine(arr, current);
  9254. insertIndent(arr, current);
  9255. }
  9256. }
  9257. arr.push("</" + node.tagName + ">");
  9258. }
  9259. }
  9260. function isComment(node, arr) {
  9261. arr.push("<!--" + node.data + "-->");
  9262. }
  9263. function getNodeById(root, id) {
  9264. var node;
  9265. if (root.type == "element" && root.getAttr("id") == id) {
  9266. return root;
  9267. }
  9268. if (root.children && root.children.length) {
  9269. for (var i = 0, ci; (ci = root.children[i++]); ) {
  9270. if ((node = getNodeById(ci, id))) {
  9271. return node;
  9272. }
  9273. }
  9274. }
  9275. }
  9276. function getNodesByTagName(node, tagName, arr) {
  9277. if (node.type == "element" && node.tagName == tagName) {
  9278. arr.push(node);
  9279. }
  9280. if (node.children && node.children.length) {
  9281. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9282. getNodesByTagName(ci, tagName, arr);
  9283. }
  9284. }
  9285. }
  9286. function nodeTraversal(root, fn) {
  9287. if (root.children && root.children.length) {
  9288. for (var i = 0, ci; (ci = root.children[i]); ) {
  9289. nodeTraversal(ci, fn);
  9290. //ci被替换的情况,这里就不再走 fn了
  9291. if (ci.parentNode) {
  9292. if (ci.children && ci.children.length) {
  9293. fn(ci);
  9294. }
  9295. if (ci.parentNode) i++;
  9296. }
  9297. }
  9298. } else {
  9299. fn(root);
  9300. }
  9301. }
  9302. uNode.prototype = {
  9303. /**
  9304. * 当前节点对象,转换成html文本
  9305. * @method toHtml
  9306. * @return { String } 返回转换后的html字符串
  9307. * @example
  9308. * ```javascript
  9309. * node.toHtml();
  9310. * ```
  9311. */
  9312. /**
  9313. * 当前节点对象,转换成html文本
  9314. * @method toHtml
  9315. * @param { Boolean } formatter 是否格式化返回值
  9316. * @return { String } 返回转换后的html字符串
  9317. * @example
  9318. * ```javascript
  9319. * node.toHtml( true );
  9320. * ```
  9321. */
  9322. toHtml: function(formatter) {
  9323. var arr = [];
  9324. nodeToHtml(this, arr, formatter, 0);
  9325. return arr.join("");
  9326. },
  9327. /**
  9328. * 获取节点的html内容
  9329. * @method innerHTML
  9330. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9331. * @return { String } 返回节点的html内容
  9332. * @example
  9333. * ```javascript
  9334. * var htmlstr = node.innerHTML();
  9335. * ```
  9336. */
  9337. /**
  9338. * 设置节点的html内容
  9339. * @method innerHTML
  9340. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9341. * @param { String } htmlstr 传入要设置的html内容
  9342. * @return { UE.uNode } 返回节点本身
  9343. * @example
  9344. * ```javascript
  9345. * node.innerHTML('<span>text</span>');
  9346. * ```
  9347. */
  9348. innerHTML: function(htmlstr) {
  9349. if (this.type != "element" || dtd.$empty[this.tagName]) {
  9350. return this;
  9351. }
  9352. if (utils.isString(htmlstr)) {
  9353. if (this.children) {
  9354. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9355. ci.parentNode = null;
  9356. }
  9357. }
  9358. this.children = [];
  9359. var tmpRoot = UE.htmlparser(htmlstr);
  9360. for (var i = 0, ci; (ci = tmpRoot.children[i++]); ) {
  9361. this.children.push(ci);
  9362. ci.parentNode = this;
  9363. }
  9364. return this;
  9365. } else {
  9366. var tmpRoot = new UE.uNode({
  9367. type: "root",
  9368. children: this.children
  9369. });
  9370. return tmpRoot.toHtml();
  9371. }
  9372. },
  9373. /**
  9374. * 获取节点的纯文本内容
  9375. * @method innerText
  9376. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9377. * @return { String } 返回节点的存文本内容
  9378. * @example
  9379. * ```javascript
  9380. * var textStr = node.innerText();
  9381. * ```
  9382. */
  9383. /**
  9384. * 设置节点的纯文本内容
  9385. * @method innerText
  9386. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9387. * @param { String } textStr 传入要设置的文本内容
  9388. * @return { UE.uNode } 返回节点本身
  9389. * @example
  9390. * ```javascript
  9391. * node.innerText('<span>text</span>');
  9392. * ```
  9393. */
  9394. innerText: function(textStr, noTrans) {
  9395. if (this.type != "element" || dtd.$empty[this.tagName]) {
  9396. return this;
  9397. }
  9398. if (textStr) {
  9399. if (this.children) {
  9400. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9401. ci.parentNode = null;
  9402. }
  9403. }
  9404. this.children = [];
  9405. this.appendChild(uNode.createText(textStr, noTrans));
  9406. return this;
  9407. } else {
  9408. return this.toHtml().replace(/<[^>]+>/g, "");
  9409. }
  9410. },
  9411. /**
  9412. * 获取当前对象的data属性
  9413. * @method getData
  9414. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  9415. * @example
  9416. * ```javascript
  9417. * node.getData();
  9418. * ```
  9419. */
  9420. getData: function() {
  9421. if (this.type == "element") return "";
  9422. return this.data;
  9423. },
  9424. /**
  9425. * 获取当前节点下的第一个子节点
  9426. * @method firstChild
  9427. * @return { UE.uNode } 返回第一个子节点
  9428. * @example
  9429. * ```javascript
  9430. * node.firstChild(); //返回第一个子节点
  9431. * ```
  9432. */
  9433. firstChild: function() {
  9434. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9435. // return this;
  9436. // }
  9437. return this.children ? this.children[0] : null;
  9438. },
  9439. /**
  9440. * 获取当前节点下的最后一个子节点
  9441. * @method lastChild
  9442. * @return { UE.uNode } 返回最后一个子节点
  9443. * @example
  9444. * ```javascript
  9445. * node.lastChild(); //返回最后一个子节点
  9446. * ```
  9447. */
  9448. lastChild: function() {
  9449. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  9450. // return this;
  9451. // }
  9452. return this.children ? this.children[this.children.length - 1] : null;
  9453. },
  9454. /**
  9455. * 获取和当前节点有相同父亲节点的前一个节点
  9456. * @method previousSibling
  9457. * @return { UE.uNode } 返回前一个节点
  9458. * @example
  9459. * ```javascript
  9460. * node.children[2].previousSibling(); //返回子节点node.children[1]
  9461. * ```
  9462. */
  9463. previousSibling: function() {
  9464. var parent = this.parentNode;
  9465. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9466. if (ci === this) {
  9467. return i == 0 ? null : parent.children[i - 1];
  9468. }
  9469. }
  9470. },
  9471. /**
  9472. * 获取和当前节点有相同父亲节点的后一个节点
  9473. * @method nextSibling
  9474. * @return { UE.uNode } 返回后一个节点,找不到返回null
  9475. * @example
  9476. * ```javascript
  9477. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  9478. * ```
  9479. */
  9480. nextSibling: function() {
  9481. var parent = this.parentNode;
  9482. for (var i = 0, ci; (ci = parent.children[i++]); ) {
  9483. if (ci === this) {
  9484. return parent.children[i];
  9485. }
  9486. }
  9487. },
  9488. /**
  9489. * 用新的节点替换当前节点
  9490. * @method replaceChild
  9491. * @param { UE.uNode } target 要替换成该节点参数
  9492. * @param { UE.uNode } source 要被替换掉的节点
  9493. * @return { UE.uNode } 返回替换之后的节点对象
  9494. * @example
  9495. * ```javascript
  9496. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  9497. * ```
  9498. */
  9499. replaceChild: function(target, source) {
  9500. if (this.children) {
  9501. if (target.parentNode) {
  9502. target.parentNode.removeChild(target);
  9503. }
  9504. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9505. if (ci === source) {
  9506. this.children.splice(i, 1, target);
  9507. source.parentNode = null;
  9508. target.parentNode = this;
  9509. return target;
  9510. }
  9511. }
  9512. }
  9513. },
  9514. /**
  9515. * 在节点的子节点列表最后位置插入一个节点
  9516. * @method appendChild
  9517. * @param { UE.uNode } node 要插入的节点
  9518. * @return { UE.uNode } 返回刚插入的子节点
  9519. * @example
  9520. * ```javascript
  9521. * node.appendChild( newNode ); //在node内插入子节点newNode
  9522. * ```
  9523. */
  9524. appendChild: function(node) {
  9525. if (
  9526. this.type == "root" ||
  9527. (this.type == "element" && !dtd.$empty[this.tagName])
  9528. ) {
  9529. if (!this.children) {
  9530. this.children = [];
  9531. }
  9532. if (node.parentNode) {
  9533. node.parentNode.removeChild(node);
  9534. }
  9535. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9536. if (ci === node) {
  9537. this.children.splice(i, 1);
  9538. break;
  9539. }
  9540. }
  9541. this.children.push(node);
  9542. node.parentNode = this;
  9543. return node;
  9544. }
  9545. },
  9546. /**
  9547. * 在传入节点的前面插入一个节点
  9548. * @method insertBefore
  9549. * @param { UE.uNode } target 要插入的节点
  9550. * @param { UE.uNode } source 在该参数节点前面插入
  9551. * @return { UE.uNode } 返回刚插入的子节点
  9552. * @example
  9553. * ```javascript
  9554. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9555. * ```
  9556. */
  9557. insertBefore: function(target, source) {
  9558. if (this.children) {
  9559. if (target.parentNode) {
  9560. target.parentNode.removeChild(target);
  9561. }
  9562. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9563. if (ci === source) {
  9564. this.children.splice(i, 0, target);
  9565. target.parentNode = this;
  9566. return target;
  9567. }
  9568. }
  9569. }
  9570. },
  9571. /**
  9572. * 在传入节点的后面插入一个节点
  9573. * @method insertAfter
  9574. * @param { UE.uNode } target 要插入的节点
  9575. * @param { UE.uNode } source 在该参数节点后面插入
  9576. * @return { UE.uNode } 返回刚插入的子节点
  9577. * @example
  9578. * ```javascript
  9579. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9580. * ```
  9581. */
  9582. insertAfter: function(target, source) {
  9583. if (this.children) {
  9584. if (target.parentNode) {
  9585. target.parentNode.removeChild(target);
  9586. }
  9587. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9588. if (ci === source) {
  9589. this.children.splice(i + 1, 0, target);
  9590. target.parentNode = this;
  9591. return target;
  9592. }
  9593. }
  9594. }
  9595. },
  9596. /**
  9597. * 从当前节点的子节点列表中,移除节点
  9598. * @method removeChild
  9599. * @param { UE.uNode } node 要移除的节点引用
  9600. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9601. * @return { * } 返回刚移除的子节点
  9602. * @example
  9603. * ```javascript
  9604. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9605. * ```
  9606. */
  9607. removeChild: function(node, keepChildren) {
  9608. if (this.children) {
  9609. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9610. if (ci === node) {
  9611. this.children.splice(i, 1);
  9612. ci.parentNode = null;
  9613. if (keepChildren && ci.children && ci.children.length) {
  9614. for (var j = 0, cj; (cj = ci.children[j]); j++) {
  9615. this.children.splice(i + j, 0, cj);
  9616. cj.parentNode = this;
  9617. }
  9618. }
  9619. return ci;
  9620. }
  9621. }
  9622. }
  9623. },
  9624. /**
  9625. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9626. * @method getAttr
  9627. * @param { String } attrName 要获取的属性名称
  9628. * @return { * } 返回attrs对象下的属性值
  9629. * @example
  9630. * ```javascript
  9631. * node.getAttr('title');
  9632. * ```
  9633. */
  9634. getAttr: function(attrName) {
  9635. return this.attrs && this.attrs[attrName.toLowerCase()];
  9636. },
  9637. /**
  9638. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9639. * @method setAttr
  9640. * @param { String } attrName 要设置的属性名称
  9641. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9642. * @return { * } 返回attrs对象下的属性值
  9643. * @example
  9644. * ```javascript
  9645. * node.setAttr('title','标题');
  9646. * ```
  9647. */
  9648. setAttr: function(attrName, attrVal) {
  9649. if (!attrName) {
  9650. delete this.attrs;
  9651. return;
  9652. }
  9653. if (!this.attrs) {
  9654. this.attrs = {};
  9655. }
  9656. if (utils.isObject(attrName)) {
  9657. for (var a in attrName) {
  9658. if (!attrName[a]) {
  9659. delete this.attrs[a];
  9660. } else {
  9661. this.attrs[a.toLowerCase()] = attrName[a];
  9662. }
  9663. }
  9664. } else {
  9665. if (!attrVal) {
  9666. delete this.attrs[attrName];
  9667. } else {
  9668. this.attrs[attrName.toLowerCase()] = attrVal;
  9669. }
  9670. }
  9671. },
  9672. /**
  9673. * 获取当前节点在父节点下的位置索引
  9674. * @method getIndex
  9675. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9676. * @example
  9677. * ```javascript
  9678. * node.getIndex();
  9679. * ```
  9680. */
  9681. getIndex: function() {
  9682. var parent = this.parentNode;
  9683. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9684. if (ci === this) {
  9685. return i;
  9686. }
  9687. }
  9688. return -1;
  9689. },
  9690. /**
  9691. * 在当前节点下,根据id查找节点
  9692. * @method getNodeById
  9693. * @param { String } id 要查找的id
  9694. * @return { UE.uNode } 返回找到的节点
  9695. * @example
  9696. * ```javascript
  9697. * node.getNodeById('textId');
  9698. * ```
  9699. */
  9700. getNodeById: function(id) {
  9701. var node;
  9702. if (this.children && this.children.length) {
  9703. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9704. if ((node = getNodeById(ci, id))) {
  9705. return node;
  9706. }
  9707. }
  9708. }
  9709. },
  9710. /**
  9711. * 在当前节点下,根据元素名称查找节点列表
  9712. * @method getNodesByTagName
  9713. * @param { String } tagNames 要查找的元素名称
  9714. * @return { Array } 返回找到的节点列表
  9715. * @example
  9716. * ```javascript
  9717. * node.getNodesByTagName('span');
  9718. * ```
  9719. */
  9720. getNodesByTagName: function(tagNames) {
  9721. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, " ").split(" ");
  9722. var arr = [],
  9723. me = this;
  9724. utils.each(tagNames, function(tagName) {
  9725. if (me.children && me.children.length) {
  9726. for (var i = 0, ci; (ci = me.children[i++]); ) {
  9727. getNodesByTagName(ci, tagName, arr);
  9728. }
  9729. }
  9730. });
  9731. return arr;
  9732. },
  9733. /**
  9734. * 根据样式名称,获取节点的样式值
  9735. * @method getStyle
  9736. * @param { String } name 要获取的样式名称
  9737. * @return { String } 返回样式值
  9738. * @example
  9739. * ```javascript
  9740. * node.getStyle('font-size');
  9741. * ```
  9742. */
  9743. getStyle: function(name) {
  9744. var cssStyle = this.getAttr("style");
  9745. if (!cssStyle) {
  9746. return "";
  9747. }
  9748. var reg = new RegExp("(^|;)\\s*" + name + ":([^;]+)", "i");
  9749. var match = cssStyle.match(reg);
  9750. if (match && match[0]) {
  9751. return match[2];
  9752. }
  9753. return "";
  9754. },
  9755. /**
  9756. * 给节点设置样式
  9757. * @method setStyle
  9758. * @param { String } name 要设置的的样式名称
  9759. * @param { String } val 要设置的的样值
  9760. * @example
  9761. * ```javascript
  9762. * node.setStyle('font-size', '12px');
  9763. * ```
  9764. */
  9765. setStyle: function(name, val) {
  9766. function exec(name, val) {
  9767. var reg = new RegExp("(^|;)\\s*" + name + ":([^;]+;?)", "gi");
  9768. cssStyle = cssStyle.replace(reg, "$1");
  9769. if (val) {
  9770. cssStyle = name + ":" + utils.unhtml(val) + ";" + cssStyle;
  9771. }
  9772. }
  9773. var cssStyle = this.getAttr("style");
  9774. if (!cssStyle) {
  9775. cssStyle = "";
  9776. }
  9777. if (utils.isObject(name)) {
  9778. for (var a in name) {
  9779. exec(a, name[a]);
  9780. }
  9781. } else {
  9782. exec(name, val);
  9783. }
  9784. this.setAttr("style", utils.trim(cssStyle));
  9785. },
  9786. /**
  9787. * 传入一个函数,递归遍历当前节点下的所有节点
  9788. * @method traversal
  9789. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9790. * @example
  9791. * ```javascript
  9792. * traversal(node, function(){
  9793. * console.log(node.type);
  9794. * });
  9795. * ```
  9796. */
  9797. traversal: function(fn) {
  9798. if (this.children && this.children.length) {
  9799. nodeTraversal(this, fn);
  9800. }
  9801. return this;
  9802. }
  9803. };
  9804. })();
  9805. // core/htmlparser.js
  9806. /**
  9807. * html字符串转换成uNode节点
  9808. * @file
  9809. * @module UE
  9810. * @since 1.2.6.1
  9811. */
  9812. /**
  9813. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9814. * @unfile
  9815. * @module UE
  9816. */
  9817. /**
  9818. * html字符串转换成uNode节点的静态方法
  9819. * @method htmlparser
  9820. * @param { String } htmlstr 要转换的html代码
  9821. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9822. * @return { uNode } 给定的html片段转换形成的uNode对象
  9823. * @example
  9824. * ```javascript
  9825. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9826. * ```
  9827. */
  9828. var htmlparser = (UE.htmlparser = function(htmlstr, ignoreBlank) {
  9829. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9830. //先去掉了,加上的原因忘了,这里先记录
  9831. //var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9832. //以上的正则表达式无法匹配:<div style="text-align:center;font-family:" font-size:14px;"=""><img src="http://hs-album.oss.aliyuncs.com/static/27/78/35/image/20161206/20161206174331_41105.gif" alt="" /><br /></div>
  9833. //修改为如下正则表达式:
  9834. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\/\s>]+)((?:\s+[\w\-:.]+(?:\s*=\s*?(?:(?:"[^"]*")|(?:'[^']*')|[^\s"'\/>]+))?)*)[\S\s]*?(\/?)>))/g,
  9835. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  9836. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9837. var allowEmptyTags = {
  9838. b: 1,
  9839. code: 1,
  9840. i: 1,
  9841. u: 1,
  9842. strike: 1,
  9843. s: 1,
  9844. tt: 1,
  9845. strong: 1,
  9846. q: 1,
  9847. samp: 1,
  9848. em: 1,
  9849. span: 1,
  9850. sub: 1,
  9851. img: 1,
  9852. sup: 1,
  9853. font: 1,
  9854. big: 1,
  9855. small: 1,
  9856. iframe: 1,
  9857. a: 1,
  9858. br: 1,
  9859. pre: 1
  9860. };
  9861. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, "g"), "");
  9862. if (!ignoreBlank) {
  9863. htmlstr = htmlstr.replace(
  9864. new RegExp(
  9865. "[\\r\\t\\n" +
  9866. (ignoreBlank ? "" : " ") +
  9867. "]*</?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n" +
  9868. (ignoreBlank ? "" : " ") +
  9869. "]*",
  9870. "g"
  9871. ),
  9872. function(a, b) {
  9873. //br暂时单独处理
  9874. if (b && allowEmptyTags[b.toLowerCase()]) {
  9875. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, "");
  9876. }
  9877. return a
  9878. .replace(new RegExp("^[\\r\\n" + (ignoreBlank ? "" : " ") + "]+"), "")
  9879. .replace(
  9880. new RegExp("[\\r\\n" + (ignoreBlank ? "" : " ") + "]+$"),
  9881. ""
  9882. );
  9883. }
  9884. );
  9885. }
  9886. var notTransAttrs = {
  9887. href: 1,
  9888. src: 1
  9889. };
  9890. var uNode = UE.uNode,
  9891. needParentNode = {
  9892. td: "tr",
  9893. tr: ["tbody", "thead", "tfoot"],
  9894. tbody: "table",
  9895. th: "tr",
  9896. thead: "table",
  9897. tfoot: "table",
  9898. caption: "table",
  9899. li: ["ul", "ol"],
  9900. dt: "dl",
  9901. dd: "dl",
  9902. option: "select"
  9903. },
  9904. needChild = {
  9905. ol: "li",
  9906. ul: "li"
  9907. };
  9908. function text(parent, data) {
  9909. if (needChild[parent.tagName]) {
  9910. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  9911. parent.appendChild(tmpNode);
  9912. tmpNode.appendChild(uNode.createText(data));
  9913. parent = tmpNode;
  9914. } else {
  9915. parent.appendChild(uNode.createText(data));
  9916. }
  9917. }
  9918. function element(parent, tagName, htmlattr) {
  9919. var needParentTag;
  9920. if ((needParentTag = needParentNode[tagName])) {
  9921. var tmpParent = parent,
  9922. hasParent;
  9923. while (tmpParent.type != "root") {
  9924. if (
  9925. utils.isArray(needParentTag)
  9926. ? utils.indexOf(needParentTag, tmpParent.tagName) != -1
  9927. : needParentTag == tmpParent.tagName
  9928. ) {
  9929. parent = tmpParent;
  9930. hasParent = true;
  9931. break;
  9932. }
  9933. tmpParent = tmpParent.parentNode;
  9934. }
  9935. if (!hasParent) {
  9936. parent = element(
  9937. parent,
  9938. utils.isArray(needParentTag) ? needParentTag[0] : needParentTag
  9939. );
  9940. }
  9941. }
  9942. //按dtd处理嵌套
  9943. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  9944. // parent = parent.parentNode;
  9945. var elm = new uNode({
  9946. parentNode: parent,
  9947. type: "element",
  9948. tagName: tagName.toLowerCase(),
  9949. //是自闭合的处理一下
  9950. children: dtd.$empty[tagName] ? null : []
  9951. });
  9952. //如果属性存在,处理属性
  9953. if (htmlattr) {
  9954. var attrs = {},
  9955. match;
  9956. while ((match = re_attr.exec(htmlattr))) {
  9957. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()]
  9958. ? match[2] || match[3] || match[4]
  9959. : utils.unhtml(match[2] || match[3] || match[4]);
  9960. }
  9961. elm.attrs = attrs;
  9962. }
  9963. //trace:3970
  9964. // //如果parent下不能放elm
  9965. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  9966. // parent = parent.parentNode;
  9967. // elm.parentNode = parent;
  9968. // }
  9969. parent.children.push(elm);
  9970. //如果是自闭合节点返回父亲节点
  9971. return dtd.$empty[tagName] ? parent : elm;
  9972. }
  9973. function comment(parent, data) {
  9974. parent.children.push(
  9975. new uNode({
  9976. type: "comment",
  9977. data: data,
  9978. parentNode: parent
  9979. })
  9980. );
  9981. }
  9982. var match,
  9983. currentIndex = 0,
  9984. nextIndex = 0;
  9985. //设置根节点
  9986. var root = new uNode({
  9987. type: "root",
  9988. children: []
  9989. });
  9990. var currentParent = root;
  9991. while ((match = re_tag.exec(htmlstr))) {
  9992. currentIndex = match.index;
  9993. try {
  9994. if (currentIndex > nextIndex) {
  9995. //text node
  9996. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  9997. }
  9998. if (match[3]) {
  9999. if (dtd.$cdata[currentParent.tagName]) {
  10000. text(currentParent, match[0]);
  10001. } else {
  10002. //start tag
  10003. currentParent = element(
  10004. currentParent,
  10005. match[3].toLowerCase(),
  10006. match[4]
  10007. );
  10008. }
  10009. } else if (match[1]) {
  10010. if (currentParent.type != "root") {
  10011. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  10012. text(currentParent, match[0]);
  10013. } else {
  10014. var tmpParent = currentParent;
  10015. while (
  10016. currentParent.type == "element" &&
  10017. currentParent.tagName != match[1].toLowerCase()
  10018. ) {
  10019. currentParent = currentParent.parentNode;
  10020. if (currentParent.type == "root") {
  10021. currentParent = tmpParent;
  10022. throw "break";
  10023. }
  10024. }
  10025. //end tag
  10026. currentParent = currentParent.parentNode;
  10027. }
  10028. }
  10029. } else if (match[2]) {
  10030. //comment
  10031. comment(currentParent, match[2]);
  10032. }
  10033. } catch (e) {}
  10034. nextIndex = re_tag.lastIndex;
  10035. }
  10036. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  10037. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  10038. if (nextIndex < htmlstr.length) {
  10039. text(currentParent, htmlstr.slice(nextIndex));
  10040. }
  10041. return root;
  10042. });
  10043. // core/filternode.js
  10044. /**
  10045. * UE过滤节点的静态方法
  10046. * @file
  10047. */
  10048. /**
  10049. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  10050. * @module UE
  10051. */
  10052. /**
  10053. * 根据传入节点和过滤规则过滤相应节点
  10054. * @module UE
  10055. * @since 1.2.6.1
  10056. * @method filterNode
  10057. * @param { Object } root 指定root节点
  10058. * @param { Object } rules 过滤规则json对象
  10059. * @example
  10060. * ```javascript
  10061. * UE.filterNode(root,editor.options.filterRules);
  10062. * ```
  10063. */
  10064. var filterNode = (UE.filterNode = (function() {
  10065. function filterNode(node, rules) {
  10066. switch (node.type) {
  10067. case "text":
  10068. break;
  10069. case "element":
  10070. var val;
  10071. if ((val = rules[node.tagName])) {
  10072. if (val === "-") {
  10073. node.parentNode.removeChild(node);
  10074. } else if (utils.isFunction(val)) {
  10075. var parentNode = node.parentNode,
  10076. index = node.getIndex();
  10077. val(node);
  10078. if (node.parentNode) {
  10079. if (node.children) {
  10080. for (var i = 0, ci; (ci = node.children[i]); ) {
  10081. filterNode(ci, rules);
  10082. if (ci.parentNode) {
  10083. i++;
  10084. }
  10085. }
  10086. }
  10087. } else {
  10088. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10089. filterNode(ci, rules);
  10090. if (ci.parentNode) {
  10091. i++;
  10092. }
  10093. }
  10094. }
  10095. } else {
  10096. var attrs = val["$"];
  10097. if (attrs && node.attrs) {
  10098. var tmpAttrs = {},
  10099. tmpVal;
  10100. for (var a in attrs) {
  10101. tmpVal = node.getAttr(a);
  10102. //todo 只先对style单独处理
  10103. if (a == "style" && utils.isArray(attrs[a])) {
  10104. var tmpCssStyle = [];
  10105. utils.each(attrs[a], function(v) {
  10106. var tmp;
  10107. if ((tmp = node.getStyle(v))) {
  10108. tmpCssStyle.push(v + ":" + tmp);
  10109. }
  10110. });
  10111. tmpVal = tmpCssStyle.join(";");
  10112. }
  10113. if (tmpVal) {
  10114. tmpAttrs[a] = tmpVal;
  10115. }
  10116. }
  10117. node.attrs = tmpAttrs;
  10118. }
  10119. if (node.children) {
  10120. for (var i = 0, ci; (ci = node.children[i]); ) {
  10121. filterNode(ci, rules);
  10122. if (ci.parentNode) {
  10123. i++;
  10124. }
  10125. }
  10126. }
  10127. }
  10128. } else {
  10129. //如果不在名单里扣出子节点并删除该节点,cdata除外
  10130. if (dtd.$cdata[node.tagName]) {
  10131. node.parentNode.removeChild(node);
  10132. } else {
  10133. var parentNode = node.parentNode,
  10134. index = node.getIndex();
  10135. node.parentNode.removeChild(node, true);
  10136. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10137. filterNode(ci, rules);
  10138. if (ci.parentNode) {
  10139. i++;
  10140. }
  10141. }
  10142. }
  10143. }
  10144. break;
  10145. case "comment":
  10146. node.parentNode.removeChild(node);
  10147. }
  10148. }
  10149. return function(root, rules) {
  10150. if (utils.isEmptyObject(rules)) {
  10151. return root;
  10152. }
  10153. var val;
  10154. if ((val = rules["-"])) {
  10155. utils.each(val.split(" "), function(k) {
  10156. rules[k] = "-";
  10157. });
  10158. }
  10159. for (var i = 0, ci; (ci = root.children[i]); ) {
  10160. filterNode(ci, rules);
  10161. if (ci.parentNode) {
  10162. i++;
  10163. }
  10164. }
  10165. return root;
  10166. };
  10167. })());
  10168. // core/plugin.js
  10169. /**
  10170. * Created with JetBrains PhpStorm.
  10171. * User: campaign
  10172. * Date: 10/8/13
  10173. * Time: 6:15 PM
  10174. * To change this template use File | Settings | File Templates.
  10175. */
  10176. UE.plugin = (function() {
  10177. var _plugins = {};
  10178. return {
  10179. register: function(pluginName, fn, oldOptionName, afterDisabled) {
  10180. if (oldOptionName && utils.isFunction(oldOptionName)) {
  10181. afterDisabled = oldOptionName;
  10182. oldOptionName = null;
  10183. }
  10184. _plugins[pluginName] = {
  10185. optionName: oldOptionName || pluginName,
  10186. execFn: fn,
  10187. //当插件被禁用时执行
  10188. afterDisabled: afterDisabled
  10189. };
  10190. },
  10191. load: function(editor) {
  10192. utils.each(_plugins, function(plugin) {
  10193. var _export = plugin.execFn.call(editor);
  10194. if (editor.options[plugin.optionName] !== false) {
  10195. if (_export) {
  10196. //后边需要再做扩展
  10197. utils.each(_export, function(v, k) {
  10198. switch (k.toLowerCase()) {
  10199. case "shortcutkey":
  10200. editor.addshortcutkey(v);
  10201. break;
  10202. case "bindevents":
  10203. utils.each(v, function(fn, eventName) {
  10204. editor.addListener(eventName, fn);
  10205. });
  10206. break;
  10207. case "bindmultievents":
  10208. utils.each(utils.isArray(v) ? v : [v], function(event) {
  10209. var types = utils.trim(event.type).split(/\s+/);
  10210. utils.each(types, function(eventName) {
  10211. editor.addListener(eventName, event.handler);
  10212. });
  10213. });
  10214. break;
  10215. case "commands":
  10216. utils.each(v, function(execFn, execName) {
  10217. editor.commands[execName] = execFn;
  10218. });
  10219. break;
  10220. case "outputrule":
  10221. editor.addOutputRule(v);
  10222. break;
  10223. case "inputrule":
  10224. editor.addInputRule(v);
  10225. break;
  10226. case "defaultoptions":
  10227. editor.setOpt(v);
  10228. }
  10229. });
  10230. }
  10231. } else if (plugin.afterDisabled) {
  10232. plugin.afterDisabled.call(editor);
  10233. }
  10234. });
  10235. //向下兼容
  10236. utils.each(UE.plugins, function(plugin) {
  10237. plugin.call(editor);
  10238. });
  10239. },
  10240. run: function(pluginName, editor) {
  10241. var plugin = _plugins[pluginName];
  10242. if (plugin) {
  10243. plugin.exeFn.call(editor);
  10244. }
  10245. }
  10246. };
  10247. })();
  10248. // core/keymap.js
  10249. var keymap = (UE.keymap = {
  10250. Backspace: 8,
  10251. Tab: 9,
  10252. Enter: 13,
  10253. Shift: 16,
  10254. Control: 17,
  10255. Alt: 18,
  10256. CapsLock: 20,
  10257. Esc: 27,
  10258. Spacebar: 32,
  10259. PageUp: 33,
  10260. PageDown: 34,
  10261. End: 35,
  10262. Home: 36,
  10263. Left: 37,
  10264. Up: 38,
  10265. Right: 39,
  10266. Down: 40,
  10267. Insert: 45,
  10268. Del: 46,
  10269. NumLock: 144,
  10270. Cmd: 91,
  10271. "=": 187,
  10272. "-": 189,
  10273. b: 66,
  10274. i: 73,
  10275. //回退
  10276. z: 90,
  10277. y: 89,
  10278. //粘贴
  10279. v: 86,
  10280. x: 88,
  10281. s: 83,
  10282. n: 78
  10283. });
  10284. // core/localstorage.js
  10285. //存储媒介封装
  10286. var LocalStorage = (UE.LocalStorage = (function() {
  10287. var storage = window.localStorage || getUserData() || null,
  10288. LOCAL_FILE = "localStorage";
  10289. return {
  10290. saveLocalData: function(key, data) {
  10291. if (storage && data) {
  10292. storage.setItem(key, data);
  10293. return true;
  10294. }
  10295. return false;
  10296. },
  10297. getLocalData: function(key) {
  10298. if (storage) {
  10299. return storage.getItem(key);
  10300. }
  10301. return null;
  10302. },
  10303. removeItem: function(key) {
  10304. storage && storage.removeItem(key);
  10305. }
  10306. };
  10307. function getUserData() {
  10308. var container = document.createElement("div");
  10309. container.style.display = "none";
  10310. if (!container.addBehavior) {
  10311. return null;
  10312. }
  10313. container.addBehavior("#default#userdata");
  10314. return {
  10315. getItem: function(key) {
  10316. var result = null;
  10317. try {
  10318. document.body.appendChild(container);
  10319. container.load(LOCAL_FILE);
  10320. result = container.getAttribute(key);
  10321. document.body.removeChild(container);
  10322. } catch (e) {}
  10323. return result;
  10324. },
  10325. setItem: function(key, value) {
  10326. document.body.appendChild(container);
  10327. container.setAttribute(key, value);
  10328. container.save(LOCAL_FILE);
  10329. document.body.removeChild(container);
  10330. },
  10331. //// 暂时没有用到
  10332. //clear: function () {
  10333. //
  10334. // var expiresTime = new Date();
  10335. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  10336. // document.body.appendChild(container);
  10337. // container.expires = expiresTime.toUTCString();
  10338. // container.save(LOCAL_FILE);
  10339. // document.body.removeChild(container);
  10340. //
  10341. //},
  10342. removeItem: function(key) {
  10343. document.body.appendChild(container);
  10344. container.removeAttribute(key);
  10345. container.save(LOCAL_FILE);
  10346. document.body.removeChild(container);
  10347. }
  10348. };
  10349. }
  10350. })());
  10351. ;(function() {
  10352. var ROOTKEY = "ueditor_preference";
  10353. UE.Editor.prototype.setPreferences = function(key, value) {
  10354. var obj = {};
  10355. if (utils.isString(key)) {
  10356. obj[key] = value;
  10357. } else {
  10358. obj = key;
  10359. }
  10360. var data = LocalStorage.getLocalData(ROOTKEY);
  10361. if (data && (data = utils.str2json(data))) {
  10362. utils.extend(data, obj);
  10363. } else {
  10364. data = obj;
  10365. }
  10366. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  10367. };
  10368. UE.Editor.prototype.getPreferences = function(key) {
  10369. var data = LocalStorage.getLocalData(ROOTKEY);
  10370. if (data && (data = utils.str2json(data))) {
  10371. return key ? data[key] : data;
  10372. }
  10373. return null;
  10374. };
  10375. UE.Editor.prototype.removePreferences = function(key) {
  10376. var data = LocalStorage.getLocalData(ROOTKEY);
  10377. if (data && (data = utils.str2json(data))) {
  10378. data[key] = undefined;
  10379. delete data[key];
  10380. }
  10381. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  10382. };
  10383. })();
  10384. // plugins/defaultfilter.js
  10385. ///import core
  10386. ///plugin 编辑器默认的过滤转换机制
  10387. UE.plugins["defaultfilter"] = function() {
  10388. var me = this;
  10389. me.setOpt({
  10390. allowDivTransToP: true,
  10391. disabledTableInTable: true,
  10392. rgb2Hex: true
  10393. });
  10394. //默认的过滤处理
  10395. //进入编辑器的内容处理
  10396. me.addInputRule(function(root) {
  10397. var allowDivTransToP = this.options.allowDivTransToP;
  10398. var val;
  10399. function tdParent(node) {
  10400. while (node && node.type == "element") {
  10401. if (node.tagName == "td") {
  10402. return true;
  10403. }
  10404. node = node.parentNode;
  10405. }
  10406. return false;
  10407. }
  10408. //进行默认的处理
  10409. root.traversal(function(node) {
  10410. if (node.type == "element") {
  10411. if (
  10412. !dtd.$cdata[node.tagName] &&
  10413. me.options.autoClearEmptyNode &&
  10414. dtd.$inline[node.tagName] &&
  10415. !dtd.$empty[node.tagName] &&
  10416. (!node.attrs || utils.isEmptyObject(node.attrs))
  10417. ) {
  10418. if (!node.firstChild()) node.parentNode.removeChild(node);
  10419. else if (
  10420. node.tagName == "span" &&
  10421. (!node.attrs || utils.isEmptyObject(node.attrs))
  10422. ) {
  10423. node.parentNode.removeChild(node, true);
  10424. }
  10425. return;
  10426. }
  10427. switch (node.tagName) {
  10428. case "style":
  10429. case "script":
  10430. node.setAttr({
  10431. cdata_tag: node.tagName,
  10432. cdata_data: node.innerHTML() || "",
  10433. _ue_custom_node_: "true"
  10434. });
  10435. node.tagName = "div";
  10436. node.innerHTML("");
  10437. break;
  10438. case "a":
  10439. if ((val = node.getAttr("href"))) {
  10440. node.setAttr("_href", val);
  10441. }
  10442. break;
  10443. case "img":
  10444. //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  10445. if ((val = node.getAttr("src"))) {
  10446. if (/^data:/.test(val)) {
  10447. node.parentNode.removeChild(node);
  10448. break;
  10449. }
  10450. }
  10451. node.setAttr("_src", node.getAttr("src"));
  10452. break;
  10453. case "span":
  10454. if (browser.webkit && (val = node.getStyle("white-space"))) {
  10455. if (/nowrap|normal/.test(val)) {
  10456. node.setStyle("white-space", "");
  10457. if (
  10458. me.options.autoClearEmptyNode &&
  10459. utils.isEmptyObject(node.attrs)
  10460. ) {
  10461. node.parentNode.removeChild(node, true);
  10462. }
  10463. }
  10464. }
  10465. val = node.getAttr("id");
  10466. if (val && /^_baidu_bookmark_/i.test(val)) {
  10467. node.parentNode.removeChild(node);
  10468. }
  10469. break;
  10470. case "p":
  10471. if ((val = node.getAttr("align"))) {
  10472. node.setAttr("align");
  10473. node.setStyle("text-align", val);
  10474. }
  10475. //trace:3431
  10476. // var cssStyle = node.getAttr('style');
  10477. // if (cssStyle) {
  10478. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  10479. // node.setAttr('style', cssStyle)
  10480. //
  10481. // }
  10482. //p标签不允许嵌套
  10483. utils.each(node.children, function(n) {
  10484. if (n.type == "element" && n.tagName == "p") {
  10485. var next = n.nextSibling();
  10486. node.parentNode.insertAfter(n, node);
  10487. var last = n;
  10488. while (next) {
  10489. var tmp = next.nextSibling();
  10490. node.parentNode.insertAfter(next, last);
  10491. last = next;
  10492. next = tmp;
  10493. }
  10494. return false;
  10495. }
  10496. });
  10497. if (!node.firstChild()) {
  10498. node.innerHTML(browser.ie ? "&nbsp;" : "<br/>");
  10499. }
  10500. break;
  10501. case "div":
  10502. if (node.getAttr("cdata_tag")) {
  10503. break;
  10504. }
  10505. //针对代码这里不处理插入代码的div
  10506. val = node.getAttr("class");
  10507. if (val && /^line number\d+/.test(val)) {
  10508. break;
  10509. }
  10510. if (!allowDivTransToP) {
  10511. break;
  10512. }
  10513. var tmpNode,
  10514. p = UE.uNode.createElement("p");
  10515. while ((tmpNode = node.firstChild())) {
  10516. if (
  10517. tmpNode.type == "text" ||
  10518. !UE.dom.dtd.$block[tmpNode.tagName]
  10519. ) {
  10520. p.appendChild(tmpNode);
  10521. } else {
  10522. if (p.firstChild()) {
  10523. node.parentNode.insertBefore(p, node);
  10524. p = UE.uNode.createElement("p");
  10525. } else {
  10526. node.parentNode.insertBefore(tmpNode, node);
  10527. }
  10528. }
  10529. }
  10530. if (p.firstChild()) {
  10531. node.parentNode.insertBefore(p, node);
  10532. }
  10533. node.parentNode.removeChild(node);
  10534. break;
  10535. case "dl":
  10536. node.tagName = "ul";
  10537. break;
  10538. case "dt":
  10539. case "dd":
  10540. node.tagName = "li";
  10541. break;
  10542. case "li":
  10543. var className = node.getAttr("class");
  10544. if (!className || !/list\-/.test(className)) {
  10545. node.setAttr();
  10546. }
  10547. var tmpNodes = node.getNodesByTagName("ol ul");
  10548. UE.utils.each(tmpNodes, function(n) {
  10549. node.parentNode.insertAfter(n, node);
  10550. });
  10551. break;
  10552. case "td":
  10553. case "th":
  10554. case "caption":
  10555. if (!node.children || !node.children.length) {
  10556. node.appendChild(
  10557. browser.ie11below
  10558. ? UE.uNode.createText(" ")
  10559. : UE.uNode.createElement("br")
  10560. );
  10561. }
  10562. break;
  10563. case "table":
  10564. if (me.options.disabledTableInTable && tdParent(node)) {
  10565. node.parentNode.insertBefore(
  10566. UE.uNode.createText(node.innerText()),
  10567. node
  10568. );
  10569. node.parentNode.removeChild(node);
  10570. }
  10571. }
  10572. }
  10573. // if(node.type == 'comment'){
  10574. // node.parentNode.removeChild(node);
  10575. // }
  10576. });
  10577. });
  10578. //从编辑器出去的内容处理
  10579. me.addOutputRule(function(root) {
  10580. var val;
  10581. root.traversal(function(node) {
  10582. if (node.type == "element") {
  10583. if (
  10584. me.options.autoClearEmptyNode &&
  10585. dtd.$inline[node.tagName] &&
  10586. !dtd.$empty[node.tagName] &&
  10587. (!node.attrs || utils.isEmptyObject(node.attrs))
  10588. ) {
  10589. if (!node.firstChild()) node.parentNode.removeChild(node);
  10590. else if (
  10591. node.tagName == "span" &&
  10592. (!node.attrs || utils.isEmptyObject(node.attrs))
  10593. ) {
  10594. node.parentNode.removeChild(node, true);
  10595. }
  10596. return;
  10597. }
  10598. switch (node.tagName) {
  10599. case "div":
  10600. if ((val = node.getAttr("cdata_tag"))) {
  10601. node.tagName = val;
  10602. node.appendChild(UE.uNode.createText(node.getAttr("cdata_data")));
  10603. node.setAttr({
  10604. cdata_tag: "",
  10605. cdata_data: "",
  10606. _ue_custom_node_: ""
  10607. });
  10608. }
  10609. break;
  10610. case "a":
  10611. if ((val = node.getAttr("_href"))) {
  10612. node.setAttr({
  10613. href: utils.html(val),
  10614. _href: ""
  10615. });
  10616. }
  10617. break;
  10618. break;
  10619. case "span":
  10620. val = node.getAttr("id");
  10621. if (val && /^_baidu_bookmark_/i.test(val)) {
  10622. node.parentNode.removeChild(node);
  10623. }
  10624. //将color的rgb格式转换为#16进制格式
  10625. if (me.getOpt("rgb2Hex")) {
  10626. var cssStyle = node.getAttr("style");
  10627. if (cssStyle) {
  10628. node.setAttr(
  10629. "style",
  10630. cssStyle.replace(/rgba?\(([\d,\s]+)\)/g, function(a, value) {
  10631. var array = value.split(",");
  10632. if (array.length > 3) return "";
  10633. value = "#";
  10634. for (var i = 0, color; (color = array[i++]); ) {
  10635. color = parseInt(
  10636. color.replace(/[^\d]/gi, ""),
  10637. 10
  10638. ).toString(16);
  10639. value += color.length == 1 ? "0" + color : color;
  10640. }
  10641. return value.toUpperCase();
  10642. })
  10643. );
  10644. }
  10645. }
  10646. break;
  10647. case "img":
  10648. if ((val = node.getAttr("_src"))) {
  10649. node.setAttr({
  10650. src: node.getAttr("_src"),
  10651. _src: ""
  10652. });
  10653. }
  10654. }
  10655. }
  10656. });
  10657. });
  10658. };
  10659. // plugins/inserthtml.js
  10660. /**
  10661. * 插入html字符串插件
  10662. * @file
  10663. * @since 1.2.6.1
  10664. */
  10665. /**
  10666. * 插入html代码
  10667. * @command inserthtml
  10668. * @method execCommand
  10669. * @param { String } cmd 命令字符串
  10670. * @param { String } html 插入的html字符串
  10671. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10672. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10673. * @example
  10674. * ```javascript
  10675. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10676. * //执行命令,插入<b>CC</b>
  10677. * //插入后的效果 xxx<b>CC</b>xxx
  10678. * //<p>xx|xxx</p> 当前选区为闭合状态
  10679. * //插入<p>CC</p>
  10680. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10681. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10682. * //插入 xxxx
  10683. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10684. * ```
  10685. */
  10686. UE.commands["inserthtml"] = {
  10687. execCommand: function(command, html, notNeedFilter) {
  10688. var me = this,
  10689. range,
  10690. div;
  10691. if (!html) {
  10692. return;
  10693. }
  10694. if (me.fireEvent("beforeinserthtml", html) === true) {
  10695. return;
  10696. }
  10697. range = me.selection.getRange();
  10698. div = range.document.createElement("div");
  10699. div.style.display = "inline";
  10700. if (!notNeedFilter) {
  10701. var root = UE.htmlparser(html);
  10702. //如果给了过滤规则就先进行过滤
  10703. if (me.options.filterRules) {
  10704. UE.filterNode(root, me.options.filterRules);
  10705. }
  10706. //执行默认的处理
  10707. me.filterInputRule(root);
  10708. html = root.toHtml();
  10709. }
  10710. div.innerHTML = utils.trim(html);
  10711. if (!range.collapsed) {
  10712. var tmpNode = range.startContainer;
  10713. if (domUtils.isFillChar(tmpNode)) {
  10714. range.setStartBefore(tmpNode);
  10715. }
  10716. tmpNode = range.endContainer;
  10717. if (domUtils.isFillChar(tmpNode)) {
  10718. range.setEndAfter(tmpNode);
  10719. }
  10720. range.txtToElmBoundary();
  10721. //结束边界可能放到了br的前边,要把br包含进来
  10722. // x[xxx]<br/>
  10723. if (range.endContainer && range.endContainer.nodeType == 1) {
  10724. tmpNode = range.endContainer.childNodes[range.endOffset];
  10725. if (tmpNode && domUtils.isBr(tmpNode)) {
  10726. range.setEndAfter(tmpNode);
  10727. }
  10728. }
  10729. if (range.startOffset == 0) {
  10730. tmpNode = range.startContainer;
  10731. if (domUtils.isBoundaryNode(tmpNode, "firstChild")) {
  10732. tmpNode = range.endContainer;
  10733. if (
  10734. range.endOffset ==
  10735. (tmpNode.nodeType == 3
  10736. ? tmpNode.nodeValue.length
  10737. : tmpNode.childNodes.length) &&
  10738. domUtils.isBoundaryNode(tmpNode, "lastChild")
  10739. ) {
  10740. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  10741. range.setStart(me.body.firstChild, 0).collapse(true);
  10742. }
  10743. }
  10744. }
  10745. !range.collapsed && range.deleteContents();
  10746. if (range.startContainer.nodeType == 1) {
  10747. var child = range.startContainer.childNodes[range.startOffset],
  10748. pre;
  10749. if (
  10750. child &&
  10751. domUtils.isBlockElm(child) &&
  10752. (pre = child.previousSibling) &&
  10753. domUtils.isBlockElm(pre)
  10754. ) {
  10755. range.setEnd(pre, pre.childNodes.length).collapse();
  10756. while (child.firstChild) {
  10757. pre.appendChild(child.firstChild);
  10758. }
  10759. domUtils.remove(child);
  10760. }
  10761. }
  10762. }
  10763. var child,
  10764. parent,
  10765. pre,
  10766. tmp,
  10767. hadBreak = 0,
  10768. nextNode;
  10769. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10770. if (range.inFillChar()) {
  10771. child = range.startContainer;
  10772. if (domUtils.isFillChar(child)) {
  10773. range.setStartBefore(child).collapse(true);
  10774. domUtils.remove(child);
  10775. } else if (domUtils.isFillChar(child, true)) {
  10776. child.nodeValue = child.nodeValue.replace(fillCharReg, "");
  10777. range.startOffset--;
  10778. range.collapsed && range.collapse(true);
  10779. }
  10780. }
  10781. //列表单独处理
  10782. var li = domUtils.findParentByTagName(range.startContainer, "li", true);
  10783. if (li) {
  10784. var next, last;
  10785. while ((child = div.firstChild)) {
  10786. //针对hr单独处理一下先
  10787. while (
  10788. child &&
  10789. (child.nodeType == 3 ||
  10790. !domUtils.isBlockElm(child) ||
  10791. child.tagName == "HR")
  10792. ) {
  10793. next = child.nextSibling;
  10794. range.insertNode(child).collapse();
  10795. last = child;
  10796. child = next;
  10797. }
  10798. if (child) {
  10799. if (/^(ol|ul)$/i.test(child.tagName)) {
  10800. while (child.firstChild) {
  10801. last = child.firstChild;
  10802. domUtils.insertAfter(li, child.firstChild);
  10803. li = li.nextSibling;
  10804. }
  10805. domUtils.remove(child);
  10806. } else {
  10807. var tmpLi;
  10808. next = child.nextSibling;
  10809. tmpLi = me.document.createElement("li");
  10810. domUtils.insertAfter(li, tmpLi);
  10811. tmpLi.appendChild(child);
  10812. last = child;
  10813. child = next;
  10814. li = tmpLi;
  10815. }
  10816. }
  10817. }
  10818. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  10819. if (domUtils.isEmptyBlock(li)) {
  10820. domUtils.remove(li);
  10821. }
  10822. if (last) {
  10823. range.setStartAfter(last).collapse(true).select(true);
  10824. }
  10825. } else {
  10826. while ((child = div.firstChild)) {
  10827. if (hadBreak) {
  10828. var p = me.document.createElement("p");
  10829. while (child && (child.nodeType == 3 || !dtd.$block[child.tagName])) {
  10830. nextNode = child.nextSibling;
  10831. p.appendChild(child);
  10832. child = nextNode;
  10833. }
  10834. if (p.firstChild) {
  10835. child = p;
  10836. }
  10837. }
  10838. range.insertNode(child);
  10839. nextNode = child.nextSibling;
  10840. if (
  10841. !hadBreak &&
  10842. child.nodeType == domUtils.NODE_ELEMENT &&
  10843. domUtils.isBlockElm(child)
  10844. ) {
  10845. parent = domUtils.findParent(child, function(node) {
  10846. return domUtils.isBlockElm(node);
  10847. });
  10848. if (
  10849. parent &&
  10850. parent.tagName.toLowerCase() != "body" &&
  10851. !(
  10852. dtd[parent.tagName][child.nodeName] && child.parentNode === parent
  10853. )
  10854. ) {
  10855. if (!dtd[parent.tagName][child.nodeName]) {
  10856. pre = parent;
  10857. } else {
  10858. tmp = child.parentNode;
  10859. while (tmp !== parent) {
  10860. pre = tmp;
  10861. tmp = tmp.parentNode;
  10862. }
  10863. }
  10864. domUtils.breakParent(child, pre || tmp);
  10865. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10866. var pre = child.previousSibling;
  10867. domUtils.trimWhiteTextNode(pre);
  10868. if (!pre.childNodes.length) {
  10869. domUtils.remove(pre);
  10870. }
  10871. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10872. if (
  10873. !browser.ie &&
  10874. (next = child.nextSibling) &&
  10875. domUtils.isBlockElm(next) &&
  10876. next.lastChild &&
  10877. !domUtils.isBr(next.lastChild)
  10878. ) {
  10879. next.appendChild(me.document.createElement("br"));
  10880. }
  10881. hadBreak = 1;
  10882. }
  10883. }
  10884. var next = child.nextSibling;
  10885. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10886. range.setStart(next, 0).collapse(true);
  10887. break;
  10888. }
  10889. range.setEndAfter(child).collapse();
  10890. }
  10891. child = range.startContainer;
  10892. if (nextNode && domUtils.isBr(nextNode)) {
  10893. domUtils.remove(nextNode);
  10894. }
  10895. //用chrome可能有空白展位符
  10896. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  10897. if ((nextNode = child.nextSibling)) {
  10898. domUtils.remove(child);
  10899. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  10900. range.setStart(nextNode, 0).collapse(true).shrinkBoundary();
  10901. }
  10902. } else {
  10903. try {
  10904. child.innerHTML = browser.ie ? domUtils.fillChar : "<br/>";
  10905. } catch (e) {
  10906. range.setStartBefore(child);
  10907. domUtils.remove(child);
  10908. }
  10909. }
  10910. }
  10911. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  10912. try {
  10913. range.select(true);
  10914. } catch (e) {}
  10915. }
  10916. setTimeout(function() {
  10917. range = me.selection.getRange();
  10918. range.scrollToView(
  10919. me.autoHeightEnabled,
  10920. me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0
  10921. );
  10922. me.fireEvent("afterinserthtml", html);
  10923. }, 200);
  10924. }
  10925. };
  10926. // plugins/autotypeset.js
  10927. /**
  10928. * 自动排版
  10929. * @file
  10930. * @since 1.2.6.1
  10931. */
  10932. /**
  10933. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  10934. * @command autotypeset
  10935. * @method execCommand
  10936. * @param { String } cmd 命令字符串
  10937. * @example
  10938. * ```javascript
  10939. * editor.execCommand( 'autotypeset' );
  10940. * ```
  10941. */
  10942. UE.plugins["autotypeset"] = function() {
  10943. this.setOpt({
  10944. autotypeset: {
  10945. mergeEmptyline: true, //合并空行
  10946. removeClass: true, //去掉冗余的class
  10947. removeEmptyline: false, //去掉空行
  10948. textAlign: "left", //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  10949. imageBlockLine: "center", //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  10950. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  10951. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  10952. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  10953. removeEmptyNode: false, // 去掉空节点
  10954. //可以去掉的标签
  10955. removeTagNames: utils.extend({ div: 1 }, dtd.$removeEmpty),
  10956. indent: false, // 行首缩进
  10957. indentValue: "2em", //行首缩进的大小
  10958. bdc2sb: false,
  10959. tobdc: false
  10960. }
  10961. });
  10962. var me = this,
  10963. opt = me.options.autotypeset,
  10964. remainClass = {
  10965. selectTdClass: 1,
  10966. pagebreak: 1,
  10967. anchorclass: 1
  10968. },
  10969. remainTag = {
  10970. li: 1
  10971. },
  10972. tags = {
  10973. div: 1,
  10974. p: 1,
  10975. //trace:2183 这些也认为是行
  10976. blockquote: 1,
  10977. center: 1,
  10978. h1: 1,
  10979. h2: 1,
  10980. h3: 1,
  10981. h4: 1,
  10982. h5: 1,
  10983. h6: 1,
  10984. span: 1
  10985. },
  10986. highlightCont;
  10987. //升级了版本,但配置项目里没有autotypeset
  10988. if (!opt) {
  10989. return;
  10990. }
  10991. readLocalOpts();
  10992. function isLine(node, notEmpty) {
  10993. if (!node || node.nodeType == 3) return 0;
  10994. if (domUtils.isBr(node)) return 1;
  10995. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  10996. if (
  10997. (highlightCont && highlightCont.contains(node)) ||
  10998. node.getAttribute("pagebreak")
  10999. ) {
  11000. return 0;
  11001. }
  11002. return notEmpty
  11003. ? !domUtils.isEmptyBlock(node)
  11004. : domUtils.isEmptyBlock(
  11005. node,
  11006. new RegExp("[\\s" + domUtils.fillChar + "]", "g")
  11007. );
  11008. }
  11009. }
  11010. function removeNotAttributeSpan(node) {
  11011. if (!node.style.cssText) {
  11012. domUtils.removeAttributes(node, ["style"]);
  11013. if (
  11014. node.tagName.toLowerCase() == "span" &&
  11015. domUtils.hasNoAttributes(node)
  11016. ) {
  11017. domUtils.remove(node, true);
  11018. }
  11019. }
  11020. }
  11021. function autotype(type, html) {
  11022. var me = this,
  11023. cont;
  11024. if (html) {
  11025. if (!opt.pasteFilter) {
  11026. return;
  11027. }
  11028. cont = me.document.createElement("div");
  11029. cont.innerHTML = html.html;
  11030. } else {
  11031. cont = me.document.body;
  11032. }
  11033. var nodes = domUtils.getElementsByTagName(cont, "*");
  11034. // 行首缩进,段落方向,段间距,段内间距
  11035. for (var i = 0, ci; (ci = nodes[i++]); ) {
  11036. if (me.fireEvent("excludeNodeinautotype", ci) === true) {
  11037. continue;
  11038. }
  11039. //font-size
  11040. if (opt.clearFontSize && ci.style.fontSize) {
  11041. domUtils.removeStyle(ci, "font-size");
  11042. removeNotAttributeSpan(ci);
  11043. }
  11044. //font-family
  11045. if (opt.clearFontFamily && ci.style.fontFamily) {
  11046. domUtils.removeStyle(ci, "font-family");
  11047. removeNotAttributeSpan(ci);
  11048. }
  11049. if (isLine(ci)) {
  11050. //合并空行
  11051. if (opt.mergeEmptyline) {
  11052. var next = ci.nextSibling,
  11053. tmpNode,
  11054. isBr = domUtils.isBr(ci);
  11055. while (isLine(next)) {
  11056. tmpNode = next;
  11057. next = tmpNode.nextSibling;
  11058. if (isBr && (!next || (next && !domUtils.isBr(next)))) {
  11059. break;
  11060. }
  11061. domUtils.remove(tmpNode);
  11062. }
  11063. }
  11064. //去掉空行,保留占位的空行
  11065. if (
  11066. opt.removeEmptyline &&
  11067. domUtils.inDoc(ci, cont) &&
  11068. !remainTag[ci.parentNode.tagName.toLowerCase()]
  11069. ) {
  11070. if (domUtils.isBr(ci)) {
  11071. next = ci.nextSibling;
  11072. if (next && !domUtils.isBr(next)) {
  11073. continue;
  11074. }
  11075. }
  11076. domUtils.remove(ci);
  11077. continue;
  11078. }
  11079. }
  11080. if (isLine(ci, true) && ci.tagName != "SPAN") {
  11081. if (opt.indent) {
  11082. ci.style.textIndent = opt.indentValue;
  11083. }
  11084. if (opt.textAlign) {
  11085. ci.style.textAlign = opt.textAlign;
  11086. }
  11087. // if(opt.lineHeight)
  11088. // ci.style.lineHeight = opt.lineHeight + 'cm';
  11089. }
  11090. //去掉class,保留的class不去掉
  11091. if (
  11092. opt.removeClass &&
  11093. ci.className &&
  11094. !remainClass[ci.className.toLowerCase()]
  11095. ) {
  11096. if (highlightCont && highlightCont.contains(ci)) {
  11097. continue;
  11098. }
  11099. domUtils.removeAttributes(ci, ["class"]);
  11100. }
  11101. //表情不处理
  11102. if (
  11103. opt.imageBlockLine &&
  11104. ci.tagName.toLowerCase() == "img" &&
  11105. !ci.getAttribute("emotion")
  11106. ) {
  11107. if (html) {
  11108. var img = ci;
  11109. switch (opt.imageBlockLine) {
  11110. case "left":
  11111. case "right":
  11112. case "none":
  11113. var pN = img.parentNode,
  11114. tmpNode,
  11115. pre,
  11116. next;
  11117. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  11118. pN = pN.parentNode;
  11119. }
  11120. tmpNode = pN;
  11121. if (
  11122. tmpNode.tagName == "P" &&
  11123. domUtils.getStyle(tmpNode, "text-align") == "center"
  11124. ) {
  11125. if (
  11126. !domUtils.isBody(tmpNode) &&
  11127. domUtils.getChildCount(tmpNode, function(node) {
  11128. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11129. }) == 1
  11130. ) {
  11131. pre = tmpNode.previousSibling;
  11132. next = tmpNode.nextSibling;
  11133. if (
  11134. pre &&
  11135. next &&
  11136. pre.nodeType == 1 &&
  11137. next.nodeType == 1 &&
  11138. pre.tagName == next.tagName &&
  11139. domUtils.isBlockElm(pre)
  11140. ) {
  11141. pre.appendChild(tmpNode.firstChild);
  11142. while (next.firstChild) {
  11143. pre.appendChild(next.firstChild);
  11144. }
  11145. domUtils.remove(tmpNode);
  11146. domUtils.remove(next);
  11147. } else {
  11148. domUtils.setStyle(tmpNode, "text-align", "");
  11149. }
  11150. }
  11151. }
  11152. domUtils.setStyle(img, "float", opt.imageBlockLine);
  11153. break;
  11154. case "center":
  11155. if (me.queryCommandValue("imagefloat") != "center") {
  11156. pN = img.parentNode;
  11157. domUtils.setStyle(img, "float", "none");
  11158. tmpNode = img;
  11159. while (
  11160. pN &&
  11161. domUtils.getChildCount(pN, function(node) {
  11162. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11163. }) == 1 &&
  11164. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  11165. ) {
  11166. tmpNode = pN;
  11167. pN = pN.parentNode;
  11168. }
  11169. var pNode = me.document.createElement("p");
  11170. domUtils.setAttributes(pNode, {
  11171. style: "text-align:center"
  11172. });
  11173. tmpNode.parentNode.insertBefore(pNode, tmpNode);
  11174. pNode.appendChild(tmpNode);
  11175. domUtils.setStyle(tmpNode, "float", "");
  11176. }
  11177. }
  11178. } else {
  11179. var range = me.selection.getRange();
  11180. range.selectNode(ci).select();
  11181. me.execCommand("imagefloat", opt.imageBlockLine);
  11182. }
  11183. }
  11184. //去掉冗余的标签
  11185. if (opt.removeEmptyNode) {
  11186. if (
  11187. opt.removeTagNames[ci.tagName.toLowerCase()] &&
  11188. domUtils.hasNoAttributes(ci) &&
  11189. domUtils.isEmptyBlock(ci)
  11190. ) {
  11191. domUtils.remove(ci);
  11192. }
  11193. }
  11194. }
  11195. if (opt.tobdc) {
  11196. var root = UE.htmlparser(cont.innerHTML);
  11197. root.traversal(function(node) {
  11198. if (node.type == "text") {
  11199. node.data = ToDBC(node.data);
  11200. }
  11201. });
  11202. cont.innerHTML = root.toHtml();
  11203. }
  11204. if (opt.bdc2sb) {
  11205. var root = UE.htmlparser(cont.innerHTML);
  11206. root.traversal(function(node) {
  11207. if (node.type == "text") {
  11208. node.data = DBC2SB(node.data);
  11209. }
  11210. });
  11211. cont.innerHTML = root.toHtml();
  11212. }
  11213. if (html) {
  11214. html.html = cont.innerHTML;
  11215. }
  11216. }
  11217. if (opt.pasteFilter) {
  11218. me.addListener("beforepaste", autotype);
  11219. }
  11220. function DBC2SB(str) {
  11221. var result = "";
  11222. for (var i = 0; i < str.length; i++) {
  11223. var code = str.charCodeAt(i); //获取当前字符的unicode编码
  11224. if (code >= 65281 && code <= 65373) {
  11225. //在这个unicode编码范围中的是所有的英文字母已经各种字符
  11226. result += String.fromCharCode(str.charCodeAt(i) - 65248); //把全角字符的unicode编码转换为对应半角字符的unicode码
  11227. } else if (code == 12288) {
  11228. //空格
  11229. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  11230. } else {
  11231. result += str.charAt(i);
  11232. }
  11233. }
  11234. return result;
  11235. }
  11236. function ToDBC(txtstring) {
  11237. txtstring = utils.html(txtstring);
  11238. var tmp = "";
  11239. var mark = ""; /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  11240. for (var i = 0; i < txtstring.length; i++) {
  11241. if (txtstring.charCodeAt(i) == 32) {
  11242. tmp = tmp + String.fromCharCode(12288);
  11243. } else if (txtstring.charCodeAt(i) < 127) {
  11244. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
  11245. } else {
  11246. tmp += txtstring.charAt(i);
  11247. }
  11248. }
  11249. return tmp;
  11250. }
  11251. function readLocalOpts() {
  11252. var cookieOpt = me.getPreferences("autotypeset");
  11253. utils.extend(me.options.autotypeset, cookieOpt);
  11254. }
  11255. me.commands["autotypeset"] = {
  11256. execCommand: function() {
  11257. me.removeListener("beforepaste", autotype);
  11258. if (opt.pasteFilter) {
  11259. me.addListener("beforepaste", autotype);
  11260. }
  11261. autotype.call(me);
  11262. }
  11263. };
  11264. };
  11265. // plugins/autosubmit.js
  11266. /**
  11267. * 快捷键提交
  11268. * @file
  11269. * @since 1.2.6.1
  11270. */
  11271. /**
  11272. * 提交表单
  11273. * @command autosubmit
  11274. * @method execCommand
  11275. * @param { String } cmd 命令字符串
  11276. * @example
  11277. * ```javascript
  11278. * editor.execCommand( 'autosubmit' );
  11279. * ```
  11280. */
  11281. UE.plugin.register("autosubmit", function() {
  11282. return {
  11283. shortcutkey: {
  11284. autosubmit: "ctrl+13" //手动提交
  11285. },
  11286. commands: {
  11287. autosubmit: {
  11288. execCommand: function() {
  11289. var me = this,
  11290. form = domUtils.findParentByTagName(me.iframe, "form", false);
  11291. if (form) {
  11292. if (me.fireEvent("beforesubmit") === false) {
  11293. return;
  11294. }
  11295. me.sync();
  11296. form.submit();
  11297. }
  11298. }
  11299. }
  11300. }
  11301. };
  11302. });
  11303. // plugins/background.js
  11304. /**
  11305. * 背景插件,为UEditor提供设置背景功能
  11306. * @file
  11307. * @since 1.2.6.1
  11308. */
  11309. UE.plugin.register("background", function() {
  11310. var me = this,
  11311. cssRuleId = "editor_background",
  11312. isSetColored,
  11313. reg = new RegExp("body[\\s]*\\{(.+)\\}", "i");
  11314. function stringToObj(str) {
  11315. var obj = {},
  11316. styles = str.split(";");
  11317. utils.each(styles, function(v) {
  11318. var index = v.indexOf(":"),
  11319. key = utils.trim(v.substr(0, index)).toLowerCase();
  11320. key && (obj[key] = utils.trim(v.substr(index + 1) || ""));
  11321. });
  11322. return obj;
  11323. }
  11324. function setBackground(obj) {
  11325. if (obj) {
  11326. var styles = [];
  11327. for (var name in obj) {
  11328. if (obj.hasOwnProperty(name)) {
  11329. styles.push(name + ":" + obj[name] + "; ");
  11330. }
  11331. }
  11332. utils.cssRule(
  11333. cssRuleId,
  11334. styles.length ? "body{" + styles.join("") + "}" : "",
  11335. me.document
  11336. );
  11337. } else {
  11338. utils.cssRule(cssRuleId, "", me.document);
  11339. }
  11340. }
  11341. //重写editor.hasContent方法
  11342. var orgFn = me.hasContents;
  11343. me.hasContents = function() {
  11344. if (me.queryCommandValue("background")) {
  11345. return true;
  11346. }
  11347. return orgFn.apply(me, arguments);
  11348. };
  11349. return {
  11350. bindEvents: {
  11351. getAllHtml: function(type, headHtml) {
  11352. var body = this.body,
  11353. su = domUtils.getComputedStyle(body, "background-image"),
  11354. url = "";
  11355. if (su.indexOf(me.options.imagePath) > 0) {
  11356. url = su
  11357. .substring(su.indexOf(me.options.imagePath), su.length - 1)
  11358. .replace(/"|\(|\)/gi, "");
  11359. } else {
  11360. url = su != "none" ? su.replace(/url\("?|"?\)/gi, "") : "";
  11361. }
  11362. var html = '<style type="text/css">body{';
  11363. var bgObj = {
  11364. "background-color":
  11365. domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  11366. "background-image": url ? "url(" + url + ")" : "",
  11367. "background-repeat":
  11368. domUtils.getComputedStyle(body, "background-repeat") || "",
  11369. "background-position": browser.ie
  11370. ? domUtils.getComputedStyle(body, "background-position-x") +
  11371. " " +
  11372. domUtils.getComputedStyle(body, "background-position-y")
  11373. : domUtils.getComputedStyle(body, "background-position"),
  11374. height: domUtils.getComputedStyle(body, "height")
  11375. };
  11376. for (var name in bgObj) {
  11377. if (bgObj.hasOwnProperty(name)) {
  11378. html += name + ":" + bgObj[name] + "; ";
  11379. }
  11380. }
  11381. html += "}</style> ";
  11382. headHtml.push(html);
  11383. },
  11384. aftersetcontent: function() {
  11385. if (isSetColored == false) setBackground();
  11386. }
  11387. },
  11388. inputRule: function(root) {
  11389. isSetColored = false;
  11390. utils.each(root.getNodesByTagName("p"), function(p) {
  11391. var styles = p.getAttr("data-background");
  11392. if (styles) {
  11393. isSetColored = true;
  11394. setBackground(stringToObj(styles));
  11395. p.parentNode.removeChild(p);
  11396. }
  11397. });
  11398. },
  11399. outputRule: function(root) {
  11400. var me = this,
  11401. styles = (utils.cssRule(cssRuleId, me.document) || "")
  11402. .replace(/[\n\r]+/g, "")
  11403. .match(reg);
  11404. if (styles) {
  11405. root.appendChild(
  11406. UE.uNode.createElement(
  11407. '<p style="display:none;" data-background="' +
  11408. utils.trim(styles[1].replace(/"/g, "").replace(/[\s]+/g, " ")) +
  11409. '"><br/></p>'
  11410. )
  11411. );
  11412. }
  11413. },
  11414. commands: {
  11415. background: {
  11416. execCommand: function(cmd, obj) {
  11417. setBackground(obj);
  11418. },
  11419. queryCommandValue: function() {
  11420. var me = this,
  11421. styles = (utils.cssRule(cssRuleId, me.document) || "")
  11422. .replace(/[\n\r]+/g, "")
  11423. .match(reg);
  11424. return styles ? stringToObj(styles[1]) : null;
  11425. },
  11426. notNeedUndo: true
  11427. }
  11428. }
  11429. };
  11430. });
  11431. // plugins/image.js
  11432. /**
  11433. * 图片插入、排版插件
  11434. * @file
  11435. * @since 1.2.6.1
  11436. */
  11437. /**
  11438. * 图片对齐方式
  11439. * @command imagefloat
  11440. * @method execCommand
  11441. * @remind 值center为独占一行居中
  11442. * @param { String } cmd 命令字符串
  11443. * @param { String } align 对齐方式,可传left、right、none、center
  11444. * @remaind center表示图片独占一行
  11445. * @example
  11446. * ```javascript
  11447. * editor.execCommand( 'imagefloat', 'center' );
  11448. * ```
  11449. */
  11450. /**
  11451. * 如果选区所在位置是图片区域
  11452. * @command imagefloat
  11453. * @method queryCommandValue
  11454. * @param { String } cmd 命令字符串
  11455. * @return { String } 返回图片对齐方式
  11456. * @example
  11457. * ```javascript
  11458. * editor.queryCommandValue( 'imagefloat' );
  11459. * ```
  11460. */
  11461. UE.commands["imagefloat"] = {
  11462. execCommand: function(cmd, align) {
  11463. var me = this,
  11464. range = me.selection.getRange();
  11465. if (!range.collapsed) {
  11466. var img = range.getClosedNode();
  11467. if (img && img.tagName == "IMG") {
  11468. switch (align) {
  11469. case "left":
  11470. case "right":
  11471. case "none":
  11472. var pN = img.parentNode,
  11473. tmpNode,
  11474. pre,
  11475. next;
  11476. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  11477. pN = pN.parentNode;
  11478. }
  11479. tmpNode = pN;
  11480. if (
  11481. tmpNode.tagName == "P" &&
  11482. domUtils.getStyle(tmpNode, "text-align") == "center"
  11483. ) {
  11484. if (
  11485. !domUtils.isBody(tmpNode) &&
  11486. domUtils.getChildCount(tmpNode, function(node) {
  11487. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11488. }) == 1
  11489. ) {
  11490. pre = tmpNode.previousSibling;
  11491. next = tmpNode.nextSibling;
  11492. if (
  11493. pre &&
  11494. next &&
  11495. pre.nodeType == 1 &&
  11496. next.nodeType == 1 &&
  11497. pre.tagName == next.tagName &&
  11498. domUtils.isBlockElm(pre)
  11499. ) {
  11500. pre.appendChild(tmpNode.firstChild);
  11501. while (next.firstChild) {
  11502. pre.appendChild(next.firstChild);
  11503. }
  11504. domUtils.remove(tmpNode);
  11505. domUtils.remove(next);
  11506. } else {
  11507. domUtils.setStyle(tmpNode, "text-align", "");
  11508. }
  11509. }
  11510. range.selectNode(img).select();
  11511. }
  11512. domUtils.setStyle(img, "float", align == "none" ? "" : align);
  11513. if (align == "none") {
  11514. domUtils.removeAttributes(img, "align");
  11515. }
  11516. break;
  11517. case "center":
  11518. if (me.queryCommandValue("imagefloat") != "center") {
  11519. var pN = img.parentNode;
  11520. domUtils.setStyle(img, "float", "");
  11521. domUtils.removeAttributes(img, "align");
  11522. tmpNode = img;
  11523. while (
  11524. pN &&
  11525. domUtils.getChildCount(pN, function(node) {
  11526. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11527. }) == 1 &&
  11528. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  11529. ) {
  11530. tmpNode = pN;
  11531. pN = pN.parentNode;
  11532. }
  11533. range.setStartBefore(tmpNode).setCursor(false);
  11534. pN = me.document.createElement("div");
  11535. pN.appendChild(tmpNode);
  11536. domUtils.setStyle(tmpNode, "float", "");
  11537. me.execCommand(
  11538. "insertHtml",
  11539. '<p class="_img_parent_tmp" style="text-align:center">' +
  11540. pN.innerHTML +
  11541. "</p>"
  11542. );
  11543. tmpNode = me.document.getElementsByClassName("_img_parent_tmp")[0];
  11544. tmpNode.removeAttribute("class");
  11545. tmpNode = tmpNode.firstChild;
  11546. range.selectNode(tmpNode).select();
  11547. //去掉后边多余的元素
  11548. next = tmpNode.parentNode.nextSibling;
  11549. if (next && domUtils.isEmptyNode(next)) {
  11550. domUtils.remove(next);
  11551. }
  11552. }
  11553. break;
  11554. }
  11555. }
  11556. }
  11557. },
  11558. queryCommandValue: function() {
  11559. var range = this.selection.getRange(),
  11560. startNode,
  11561. floatStyle;
  11562. if (range.collapsed) {
  11563. return "none";
  11564. }
  11565. startNode = range.getClosedNode();
  11566. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  11567. floatStyle =
  11568. domUtils.getComputedStyle(startNode, "float") ||
  11569. startNode.getAttribute("align");
  11570. if (floatStyle == "none") {
  11571. floatStyle = domUtils.getComputedStyle(
  11572. startNode.parentNode,
  11573. "text-align"
  11574. ) == "center"
  11575. ? "center"
  11576. : floatStyle;
  11577. }
  11578. return {
  11579. left: 1,
  11580. right: 1,
  11581. center: 1
  11582. }[floatStyle]
  11583. ? floatStyle
  11584. : "none";
  11585. }
  11586. return "none";
  11587. },
  11588. queryCommandState: function() {
  11589. var range = this.selection.getRange(),
  11590. startNode;
  11591. if (range.collapsed) return -1;
  11592. startNode = range.getClosedNode();
  11593. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  11594. return 0;
  11595. }
  11596. return -1;
  11597. }
  11598. };
  11599. /**
  11600. * 插入图片
  11601. * @command insertimage
  11602. * @method execCommand
  11603. * @param { String } cmd 命令字符串
  11604. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  11605. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  11606. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  11607. * @example
  11608. * ```javascript
  11609. * editor.execCommand( 'insertimage', {
  11610. * src:'a/b/c.jpg',
  11611. * width:'100',
  11612. * height:'100'
  11613. * } );
  11614. * ```
  11615. * @example
  11616. * ```javascript
  11617. * editor.execCommand( 'insertimage', [{
  11618. * src:'a/b/c.jpg',
  11619. * width:'100',
  11620. * height:'100'
  11621. * },{
  11622. * src:'a/b/d.jpg',
  11623. * width:'100',
  11624. * height:'100'
  11625. * }] );
  11626. * ```
  11627. */
  11628. UE.commands["insertimage"] = {
  11629. execCommand: function(cmd, opt) {
  11630. opt = utils.isArray(opt) ? opt : [opt];
  11631. if (!opt.length) {
  11632. return;
  11633. }
  11634. var me = this,
  11635. range = me.selection.getRange(),
  11636. img = range.getClosedNode();
  11637. if (me.fireEvent("beforeinsertimage", opt) === true) {
  11638. return;
  11639. }
  11640. if (
  11641. img &&
  11642. /img/i.test(img.tagName) &&
  11643. (img.className != "edui-faked-video" ||
  11644. img.className.indexOf("edui-upload-video") != -1) &&
  11645. !img.getAttribute("word_img")
  11646. ) {
  11647. var first = opt.shift();
  11648. var floatStyle = first["floatStyle"];
  11649. delete first["floatStyle"];
  11650. //// img.style.border = (first.border||0) +"px solid #000";
  11651. //// img.style.margin = (first.margin||0) +"px";
  11652. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  11653. domUtils.setAttributes(img, first);
  11654. me.execCommand("imagefloat", floatStyle);
  11655. if (opt.length > 0) {
  11656. range.setStartAfter(img).setCursor(false, true);
  11657. me.execCommand("insertimage", opt);
  11658. }
  11659. } else {
  11660. var html = [],
  11661. str = "",
  11662. ci;
  11663. ci = opt[0];
  11664. if (opt.length == 1) {
  11665. str =
  11666. '<img src="' +
  11667. ci.src +
  11668. '" ' +
  11669. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  11670. (ci.width ? 'width="' + ci.width + '" ' : "") +
  11671. (ci.height ? ' height="' + ci.height + '" ' : "") +
  11672. (ci["floatStyle"] == "left" || ci["floatStyle"] == "right"
  11673. ? ' style="float:' + ci["floatStyle"] + ';"'
  11674. : "") +
  11675. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : "") +
  11676. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : "") +
  11677. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : "") +
  11678. (ci.hspace && ci.hspace != "0"
  11679. ? ' hspace = "' + ci.hspace + '"'
  11680. : "") +
  11681. (ci.vspace && ci.vspace != "0"
  11682. ? ' vspace = "' + ci.vspace + '"'
  11683. : "") +
  11684. "/>";
  11685. if (ci["floatStyle"] == "center") {
  11686. str = '<p style="text-align: center">' + str + "</p>";
  11687. }
  11688. html.push(str);
  11689. } else {
  11690. for (var i = 0; (ci = opt[i++]); ) {
  11691. str =
  11692. "<p " +
  11693. (ci["floatStyle"] == "center"
  11694. ? 'style="text-align: center" '
  11695. : "") +
  11696. '><img src="' +
  11697. ci.src +
  11698. '" ' +
  11699. (ci.width ? 'width="' + ci.width + '" ' : "") +
  11700. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  11701. (ci.height ? ' height="' + ci.height + '" ' : "") +
  11702. ' style="' +
  11703. (ci["floatStyle"] && ci["floatStyle"] != "center"
  11704. ? "float:" + ci["floatStyle"] + ";"
  11705. : "") +
  11706. (ci.border || "") +
  11707. '" ' +
  11708. (ci.title ? ' title="' + ci.title + '"' : "") +
  11709. " /></p>";
  11710. html.push(str);
  11711. }
  11712. }
  11713. me.execCommand("insertHtml", html.join(""));
  11714. }
  11715. me.fireEvent("afterinsertimage", opt);
  11716. }
  11717. };
  11718. // plugins/justify.js
  11719. /**
  11720. * 段落格式
  11721. * @file
  11722. * @since 1.2.6.1
  11723. */
  11724. /**
  11725. * 段落对齐方式
  11726. * @command justify
  11727. * @method execCommand
  11728. * @param { String } cmd 命令字符串
  11729. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  11730. * @example
  11731. * ```javascript
  11732. * editor.execCommand( 'justify', 'center' );
  11733. * ```
  11734. */
  11735. /**
  11736. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  11737. * @command justify
  11738. * @method queryCommandValue
  11739. * @param { String } cmd 命令字符串
  11740. * @return { String } 返回段落对齐方式
  11741. * @example
  11742. * ```javascript
  11743. * editor.queryCommandValue( 'justify' );
  11744. * ```
  11745. */
  11746. UE.plugins["justify"] = function() {
  11747. var me = this,
  11748. block = domUtils.isBlockElm,
  11749. defaultValue = {
  11750. left: 1,
  11751. right: 1,
  11752. center: 1,
  11753. justify: 1
  11754. },
  11755. doJustify = function(range, style) {
  11756. var bookmark = range.createBookmark(),
  11757. filterFn = function(node) {
  11758. return node.nodeType == 1
  11759. ? node.tagName.toLowerCase() != "br" &&
  11760. !domUtils.isBookmarkNode(node)
  11761. : !domUtils.isWhitespace(node);
  11762. };
  11763. range.enlarge(true);
  11764. var bookmark2 = range.createBookmark(),
  11765. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  11766. tmpRange = range.cloneRange(),
  11767. tmpNode;
  11768. while (
  11769. current &&
  11770. !(
  11771. domUtils.getPosition(current, bookmark2.end) &
  11772. domUtils.POSITION_FOLLOWING
  11773. )
  11774. ) {
  11775. if (current.nodeType == 3 || !block(current)) {
  11776. tmpRange.setStartBefore(current);
  11777. while (current && current !== bookmark2.end && !block(current)) {
  11778. tmpNode = current;
  11779. current = domUtils.getNextDomNode(current, false, null, function(
  11780. node
  11781. ) {
  11782. return !block(node);
  11783. });
  11784. }
  11785. tmpRange.setEndAfter(tmpNode);
  11786. var common = tmpRange.getCommonAncestor();
  11787. if (!domUtils.isBody(common) && block(common)) {
  11788. domUtils.setStyles(
  11789. common,
  11790. utils.isString(style) ? { "text-align": style } : style
  11791. );
  11792. current = common;
  11793. } else {
  11794. var p = range.document.createElement("p");
  11795. domUtils.setStyles(
  11796. p,
  11797. utils.isString(style) ? { "text-align": style } : style
  11798. );
  11799. var frag = tmpRange.extractContents();
  11800. p.appendChild(frag);
  11801. tmpRange.insertNode(p);
  11802. current = p;
  11803. }
  11804. current = domUtils.getNextDomNode(current, false, filterFn);
  11805. } else {
  11806. current = domUtils.getNextDomNode(current, true, filterFn);
  11807. }
  11808. }
  11809. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  11810. };
  11811. UE.commands["justify"] = {
  11812. execCommand: function(cmdName, align) {
  11813. var range = this.selection.getRange(),
  11814. txt;
  11815. //闭合时单独处理
  11816. if (range.collapsed) {
  11817. txt = this.document.createTextNode("p");
  11818. range.insertNode(txt);
  11819. }
  11820. doJustify(range, align);
  11821. if (txt) {
  11822. range.setStartBefore(txt).collapse(true);
  11823. domUtils.remove(txt);
  11824. }
  11825. range.select();
  11826. return true;
  11827. },
  11828. queryCommandValue: function() {
  11829. var startNode = this.selection.getStart(),
  11830. value = domUtils.getComputedStyle(startNode, "text-align");
  11831. return defaultValue[value] ? value : "left";
  11832. },
  11833. queryCommandState: function() {
  11834. var start = this.selection.getStart(),
  11835. cell =
  11836. start &&
  11837. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  11838. return cell ? -1 : 0;
  11839. }
  11840. };
  11841. };
  11842. // plugins/font.js
  11843. /**
  11844. * 字体颜色,背景色,字号,字体,下划线,删除线
  11845. * @file
  11846. * @since 1.2.6.1
  11847. */
  11848. /**
  11849. * 字体颜色
  11850. * @command forecolor
  11851. * @method execCommand
  11852. * @param { String } cmd 命令字符串
  11853. * @param { String } value 色值(必须十六进制)
  11854. * @example
  11855. * ```javascript
  11856. * editor.execCommand( 'forecolor', '#000' );
  11857. * ```
  11858. */
  11859. /**
  11860. * 返回选区字体颜色
  11861. * @command forecolor
  11862. * @method queryCommandValue
  11863. * @param { String } cmd 命令字符串
  11864. * @return { String } 返回字体颜色
  11865. * @example
  11866. * ```javascript
  11867. * editor.queryCommandValue( 'forecolor' );
  11868. * ```
  11869. */
  11870. /**
  11871. * 字体背景颜色
  11872. * @command backcolor
  11873. * @method execCommand
  11874. * @param { String } cmd 命令字符串
  11875. * @param { String } value 色值(必须十六进制)
  11876. * @example
  11877. * ```javascript
  11878. * editor.execCommand( 'backcolor', '#000' );
  11879. * ```
  11880. */
  11881. /**
  11882. * 返回选区字体颜色
  11883. * @command backcolor
  11884. * @method queryCommandValue
  11885. * @param { String } cmd 命令字符串
  11886. * @return { String } 返回字体背景颜色
  11887. * @example
  11888. * ```javascript
  11889. * editor.queryCommandValue( 'backcolor' );
  11890. * ```
  11891. */
  11892. /**
  11893. * 字体大小
  11894. * @command fontsize
  11895. * @method execCommand
  11896. * @param { String } cmd 命令字符串
  11897. * @param { String } value 字体大小
  11898. * @example
  11899. * ```javascript
  11900. * editor.execCommand( 'fontsize', '14px' );
  11901. * ```
  11902. */
  11903. /**
  11904. * 返回选区字体大小
  11905. * @command fontsize
  11906. * @method queryCommandValue
  11907. * @param { String } cmd 命令字符串
  11908. * @return { String } 返回字体大小
  11909. * @example
  11910. * ```javascript
  11911. * editor.queryCommandValue( 'fontsize' );
  11912. * ```
  11913. */
  11914. /**
  11915. * 字体样式
  11916. * @command fontfamily
  11917. * @method execCommand
  11918. * @param { String } cmd 命令字符串
  11919. * @param { String } value 字体样式
  11920. * @example
  11921. * ```javascript
  11922. * editor.execCommand( 'fontfamily', '微软雅黑' );
  11923. * ```
  11924. */
  11925. /**
  11926. * 返回选区字体样式
  11927. * @command fontfamily
  11928. * @method queryCommandValue
  11929. * @param { String } cmd 命令字符串
  11930. * @return { String } 返回字体样式
  11931. * @example
  11932. * ```javascript
  11933. * editor.queryCommandValue( 'fontfamily' );
  11934. * ```
  11935. */
  11936. /**
  11937. * 字体下划线,与删除线互斥
  11938. * @command underline
  11939. * @method execCommand
  11940. * @param { String } cmd 命令字符串
  11941. * @example
  11942. * ```javascript
  11943. * editor.execCommand( 'underline' );
  11944. * ```
  11945. */
  11946. /**
  11947. * 字体删除线,与下划线互斥
  11948. * @command strikethrough
  11949. * @method execCommand
  11950. * @param { String } cmd 命令字符串
  11951. * @example
  11952. * ```javascript
  11953. * editor.execCommand( 'strikethrough' );
  11954. * ```
  11955. */
  11956. /**
  11957. * 字体边框
  11958. * @command fontborder
  11959. * @method execCommand
  11960. * @param { String } cmd 命令字符串
  11961. * @example
  11962. * ```javascript
  11963. * editor.execCommand( 'fontborder' );
  11964. * ```
  11965. */
  11966. UE.plugins["font"] = function() {
  11967. var me = this,
  11968. fonts = {
  11969. forecolor: "color",
  11970. backcolor: "background-color",
  11971. fontsize: "font-size",
  11972. fontfamily: "font-family",
  11973. underline: "text-decoration",
  11974. strikethrough: "text-decoration",
  11975. fontborder: "border"
  11976. },
  11977. needCmd = { underline: 1, strikethrough: 1, fontborder: 1 },
  11978. needSetChild = {
  11979. forecolor: "color",
  11980. backcolor: "background-color",
  11981. fontsize: "font-size",
  11982. fontfamily: "font-family"
  11983. };
  11984. me.setOpt({
  11985. fontfamily: [
  11986. { name: "songti", val: "宋体,SimSun" },
  11987. { name: "yahei", val: "微软雅黑,Microsoft YaHei" },
  11988. { name: "kaiti", val: "楷体,楷体_GB2312, SimKai" },
  11989. { name: "heiti", val: "黑体, SimHei" },
  11990. { name: "lishu", val: "隶书, SimLi" },
  11991. { name: "andaleMono", val: "andale mono" },
  11992. { name: "arial", val: "arial, helvetica,sans-serif" },
  11993. { name: "arialBlack", val: "arial black,avant garde" },
  11994. { name: "comicSansMs", val: "comic sans ms" },
  11995. { name: "impact", val: "impact,chicago" },
  11996. { name: "timesNewRoman", val: "times new roman" }
  11997. ],
  11998. fontsize: [10, 11, 12, 14, 16, 18, 20, 24, 36]
  11999. });
  12000. function mergeWithParent(node) {
  12001. var parent;
  12002. while ((parent = node.parentNode)) {
  12003. if (
  12004. parent.tagName == "SPAN" &&
  12005. domUtils.getChildCount(parent, function(child) {
  12006. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child);
  12007. }) == 1
  12008. ) {
  12009. parent.style.cssText += node.style.cssText;
  12010. domUtils.remove(node, true);
  12011. node = parent;
  12012. } else {
  12013. break;
  12014. }
  12015. }
  12016. }
  12017. function mergeChild(rng, cmdName, value) {
  12018. if (needSetChild[cmdName]) {
  12019. rng.adjustmentBoundary();
  12020. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  12021. rng.traversal(function(node){
  12022. var start;
  12023. if(domUtils.isTagNode(node,'span')){
  12024. start = node;
  12025. }else{
  12026. start = domUtils.getElementsByTagName(node,'span')[0];
  12027. }
  12028. if (start && domUtils.isTagNode(start, "span")) {
  12029. var bk = rng.createBookmark();
  12030. utils.each(domUtils.getElementsByTagName(start, "span"), function(
  12031. span
  12032. ) {
  12033. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  12034. if (
  12035. cmdName == "backcolor" &&
  12036. domUtils
  12037. .getComputedStyle(span, "background-color")
  12038. .toLowerCase() === value
  12039. ) {
  12040. return;
  12041. }
  12042. domUtils.removeStyle(span, needSetChild[cmdName]);
  12043. if (span.style.cssText.replace(/^\s+$/, "").length == 0) {
  12044. domUtils.remove(span, true);
  12045. }
  12046. });
  12047. rng.moveToBookmark(bk);
  12048. }
  12049. });
  12050. }
  12051. }
  12052. }
  12053. function mergesibling(rng, cmdName, value) {
  12054. var collapsed = rng.collapsed,
  12055. bk = rng.createBookmark(),
  12056. common;
  12057. if (collapsed) {
  12058. common = bk.start.parentNode;
  12059. while (dtd.$inline[common.tagName]) {
  12060. common = common.parentNode;
  12061. }
  12062. } else {
  12063. common = domUtils.getCommonAncestor(bk.start, bk.end);
  12064. }
  12065. utils.each(domUtils.getElementsByTagName(common, "span"), function(span) {
  12066. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  12067. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  12068. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  12069. domUtils.remove(span, true);
  12070. } else {
  12071. domUtils.removeStyle(span, "border");
  12072. }
  12073. return;
  12074. }
  12075. if (
  12076. /border/i.test(span.style.cssText) &&
  12077. span.parentNode.tagName == "SPAN" &&
  12078. /border/i.test(span.parentNode.style.cssText)
  12079. ) {
  12080. span.style.cssText = span.style.cssText.replace(
  12081. /border[^:]*:[^;]+;?/gi,
  12082. ""
  12083. );
  12084. }
  12085. if (!(cmdName == "fontborder" && value == "none")) {
  12086. var next = span.nextSibling;
  12087. while (next && next.nodeType == 1 && next.tagName == "SPAN") {
  12088. if (domUtils.isBookmarkNode(next) && cmdName == "fontborder") {
  12089. span.appendChild(next);
  12090. next = span.nextSibling;
  12091. continue;
  12092. }
  12093. if (next.style.cssText == span.style.cssText) {
  12094. domUtils.moveChild(next, span);
  12095. domUtils.remove(next);
  12096. }
  12097. if (span.nextSibling === next) break;
  12098. next = span.nextSibling;
  12099. }
  12100. }
  12101. mergeWithParent(span);
  12102. if (browser.ie && browser.version > 8) {
  12103. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  12104. var parent = domUtils.findParent(span, function(n) {
  12105. return (
  12106. n.tagName == "SPAN" && /background-color/.test(n.style.cssText)
  12107. );
  12108. });
  12109. if (parent && !/background-color/.test(span.style.cssText)) {
  12110. span.style.backgroundColor = parent.style.backgroundColor;
  12111. }
  12112. }
  12113. });
  12114. rng.moveToBookmark(bk);
  12115. mergeChild(rng, cmdName, value);
  12116. }
  12117. me.addInputRule(function(root) {
  12118. utils.each(root.getNodesByTagName("u s del font strike"), function(node) {
  12119. if (node.tagName == "font") {
  12120. var cssStyle = [];
  12121. for (var p in node.attrs) {
  12122. switch (p) {
  12123. case "size":
  12124. cssStyle.push(
  12125. "font-size:" +
  12126. ({
  12127. "1": "10",
  12128. "2": "12",
  12129. "3": "16",
  12130. "4": "18",
  12131. "5": "24",
  12132. "6": "32",
  12133. "7": "48"
  12134. }[node.attrs[p]] || node.attrs[p]) +
  12135. "px"
  12136. );
  12137. break;
  12138. case "color":
  12139. cssStyle.push("color:" + node.attrs[p]);
  12140. break;
  12141. case "face":
  12142. cssStyle.push("font-family:" + node.attrs[p]);
  12143. break;
  12144. case "style":
  12145. cssStyle.push(node.attrs[p]);
  12146. }
  12147. }
  12148. node.attrs = {
  12149. style: cssStyle.join(";")
  12150. };
  12151. } else {
  12152. var val = node.tagName == "u" ? "underline" : "line-through";
  12153. node.attrs = {
  12154. style: (node.getAttr("style") || "") + "text-decoration:" + val + ";"
  12155. };
  12156. }
  12157. node.tagName = "span";
  12158. });
  12159. // utils.each(root.getNodesByTagName('span'), function (node) {
  12160. // var val;
  12161. // if(val = node.getAttr('class')){
  12162. // if(/fontstrikethrough/.test(val)){
  12163. // node.setStyle('text-decoration','line-through');
  12164. // if(node.attrs['class']){
  12165. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  12166. // }else{
  12167. // node.setAttr('class')
  12168. // }
  12169. // }
  12170. // if(/fontborder/.test(val)){
  12171. // node.setStyle('border','1px solid #000');
  12172. // if(node.attrs['class']){
  12173. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  12174. // }else{
  12175. // node.setAttr('class')
  12176. // }
  12177. // }
  12178. // }
  12179. // });
  12180. });
  12181. // me.addOutputRule(function(root){
  12182. // utils.each(root.getNodesByTagName('span'), function (node) {
  12183. // var val;
  12184. // if(val = node.getStyle('text-decoration')){
  12185. // if(/line-through/.test(val)){
  12186. // if(node.attrs['class']){
  12187. // node.attrs['class'] += ' fontstrikethrough';
  12188. // }else{
  12189. // node.setAttr('class','fontstrikethrough')
  12190. // }
  12191. // }
  12192. //
  12193. // node.setStyle('text-decoration')
  12194. // }
  12195. // if(val = node.getStyle('border')){
  12196. // if(/1px/.test(val) && /solid/.test(val)){
  12197. // if(node.attrs['class']){
  12198. // node.attrs['class'] += ' fontborder';
  12199. //
  12200. // }else{
  12201. // node.setAttr('class','fontborder')
  12202. // }
  12203. // }
  12204. // node.setStyle('border')
  12205. //
  12206. // }
  12207. // });
  12208. // });
  12209. for (var p in fonts) {
  12210. (function(cmd, style) {
  12211. UE.commands[cmd] = {
  12212. execCommand: function(cmdName, value) {
  12213. value =
  12214. value ||
  12215. (this.queryCommandState(cmdName)
  12216. ? "none"
  12217. : cmdName == "underline"
  12218. ? "underline"
  12219. : cmdName == "fontborder" ? "1px solid #000" : "line-through");
  12220. var me = this,
  12221. range = this.selection.getRange(),
  12222. text;
  12223. if (value == "default") {
  12224. if (range.collapsed) {
  12225. text = me.document.createTextNode("font");
  12226. range.insertNode(text).select();
  12227. }
  12228. me.execCommand("removeFormat", "span,a", style);
  12229. if (text) {
  12230. range.setStartBefore(text).collapse(true);
  12231. domUtils.remove(text);
  12232. }
  12233. mergesibling(range, cmdName, value);
  12234. range.select();
  12235. } else {
  12236. if (!range.collapsed) {
  12237. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  12238. me.execCommand("removeFormat", "span,a", style);
  12239. }
  12240. range = me.selection.getRange();
  12241. range.applyInlineStyle("span", { style: style + ":" + value });
  12242. mergesibling(range, cmdName, value);
  12243. range.select();
  12244. } else {
  12245. var span = domUtils.findParentByTagName(
  12246. range.startContainer,
  12247. "span",
  12248. true
  12249. );
  12250. text = me.document.createTextNode("font");
  12251. if (
  12252. span &&
  12253. !span.children.length &&
  12254. !span[browser.ie ? "innerText" : "textContent"].replace(
  12255. fillCharReg,
  12256. ""
  12257. ).length
  12258. ) {
  12259. //for ie hack when enter
  12260. range.insertNode(text);
  12261. if (needCmd[cmd]) {
  12262. range.selectNode(text).select();
  12263. me.execCommand("removeFormat", "span,a", style, null);
  12264. span = domUtils.findParentByTagName(text, "span", true);
  12265. range.setStartBefore(text);
  12266. }
  12267. span && (span.style.cssText += ";" + style + ":" + value);
  12268. range.collapse(true).select();
  12269. } else {
  12270. range.insertNode(text);
  12271. range.selectNode(text).select();
  12272. span = range.document.createElement("span");
  12273. if (needCmd[cmd]) {
  12274. //a标签内的不处理跳过
  12275. if (domUtils.findParentByTagName(text, "a", true)) {
  12276. range.setStartBefore(text).setCursor();
  12277. domUtils.remove(text);
  12278. return;
  12279. }
  12280. me.execCommand("removeFormat", "span,a", style);
  12281. }
  12282. span.style.cssText = style + ":" + value;
  12283. text.parentNode.insertBefore(span, text);
  12284. //修复,span套span 但样式不继承的问题
  12285. if (!browser.ie || (browser.ie && browser.version == 9)) {
  12286. var spanParent = span.parentNode;
  12287. while (!domUtils.isBlockElm(spanParent)) {
  12288. if (spanParent.tagName == "SPAN") {
  12289. //opera合并style不会加入";"
  12290. span.style.cssText =
  12291. spanParent.style.cssText + ";" + span.style.cssText;
  12292. }
  12293. spanParent = spanParent.parentNode;
  12294. }
  12295. }
  12296. if (opera) {
  12297. setTimeout(function() {
  12298. range.setStart(span, 0).collapse(true);
  12299. mergesibling(range, cmdName, value);
  12300. range.select();
  12301. });
  12302. } else {
  12303. range.setStart(span, 0).collapse(true);
  12304. mergesibling(range, cmdName, value);
  12305. range.select();
  12306. }
  12307. //trace:981
  12308. //domUtils.mergeToParent(span)
  12309. }
  12310. domUtils.remove(text);
  12311. }
  12312. }
  12313. return true;
  12314. },
  12315. queryCommandValue: function(cmdName) {
  12316. var startNode = this.selection.getStart();
  12317. //trace:946
  12318. if (cmdName == "underline" || cmdName == "strikethrough") {
  12319. var tmpNode = startNode,
  12320. value;
  12321. while (
  12322. tmpNode &&
  12323. !domUtils.isBlockElm(tmpNode) &&
  12324. !domUtils.isBody(tmpNode)
  12325. ) {
  12326. if (tmpNode.nodeType == 1) {
  12327. value = domUtils.getComputedStyle(tmpNode, style);
  12328. if (value != "none") {
  12329. return value;
  12330. }
  12331. }
  12332. tmpNode = tmpNode.parentNode;
  12333. }
  12334. return "none";
  12335. }
  12336. if (cmdName == "fontborder") {
  12337. var tmp = startNode,
  12338. val;
  12339. while (tmp && dtd.$inline[tmp.tagName]) {
  12340. if ((val = domUtils.getComputedStyle(tmp, "border"))) {
  12341. if (/1px/.test(val) && /solid/.test(val)) {
  12342. return val;
  12343. }
  12344. }
  12345. tmp = tmp.parentNode;
  12346. }
  12347. return "";
  12348. }
  12349. if (cmdName == "FontSize") {
  12350. var styleVal = domUtils.getComputedStyle(startNode, style),
  12351. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal);
  12352. if (tmp) {
  12353. return Math.floor(tmp[1]) + tmp[2];
  12354. }
  12355. return styleVal;
  12356. }
  12357. return domUtils.getComputedStyle(startNode, style);
  12358. },
  12359. queryCommandState: function(cmdName) {
  12360. if (!needCmd[cmdName]) return 0;
  12361. var val = this.queryCommandValue(cmdName);
  12362. if (cmdName == "fontborder") {
  12363. return /1px/.test(val) && /solid/.test(val);
  12364. } else {
  12365. return cmdName == "underline"
  12366. ? /underline/.test(val)
  12367. : /line\-through/.test(val);
  12368. }
  12369. }
  12370. };
  12371. })(p, fonts[p]);
  12372. }
  12373. };
  12374. // plugins/link.js
  12375. /**
  12376. * 超链接
  12377. * @file
  12378. * @since 1.2.6.1
  12379. */
  12380. /**
  12381. * 插入超链接
  12382. * @command link
  12383. * @method execCommand
  12384. * @param { String } cmd 命令字符串
  12385. * @param { Object } options 设置自定义属性,例如:url、title、target
  12386. * @example
  12387. * ```javascript
  12388. * editor.execCommand( 'link', '{
  12389. * url:'ueditor.baidu.com',
  12390. * title:'ueditor',
  12391. * target:'_blank'
  12392. * }' );
  12393. * ```
  12394. */
  12395. /**
  12396. * 返回当前选中的第一个超链接节点
  12397. * @command link
  12398. * @method queryCommandValue
  12399. * @param { String } cmd 命令字符串
  12400. * @return { Element } 超链接节点
  12401. * @example
  12402. * ```javascript
  12403. * editor.queryCommandValue( 'link' );
  12404. * ```
  12405. */
  12406. /**
  12407. * 取消超链接
  12408. * @command unlink
  12409. * @method execCommand
  12410. * @param { String } cmd 命令字符串
  12411. * @example
  12412. * ```javascript
  12413. * editor.execCommand( 'unlink');
  12414. * ```
  12415. */
  12416. UE.plugins["link"] = function() {
  12417. function optimize(range) {
  12418. var start = range.startContainer,
  12419. end = range.endContainer;
  12420. if ((start = domUtils.findParentByTagName(start, "a", true))) {
  12421. range.setStartBefore(start);
  12422. }
  12423. if ((end = domUtils.findParentByTagName(end, "a", true))) {
  12424. range.setEndAfter(end);
  12425. }
  12426. }
  12427. UE.commands["unlink"] = {
  12428. execCommand: function() {
  12429. var range = this.selection.getRange(),
  12430. bookmark;
  12431. if (
  12432. range.collapsed &&
  12433. !domUtils.findParentByTagName(range.startContainer, "a", true)
  12434. ) {
  12435. return;
  12436. }
  12437. bookmark = range.createBookmark();
  12438. optimize(range);
  12439. range.removeInlineStyle("a").moveToBookmark(bookmark).select();
  12440. },
  12441. queryCommandState: function() {
  12442. return !this.highlight && this.queryCommandValue("link") ? 0 : -1;
  12443. }
  12444. };
  12445. function doLink(range, opt, me) {
  12446. var rngClone = range.cloneRange(),
  12447. link = me.queryCommandValue("link");
  12448. optimize((range = range.adjustmentBoundary()));
  12449. var start = range.startContainer;
  12450. if (start.nodeType == 1 && link) {
  12451. start = start.childNodes[range.startOffset];
  12452. if (
  12453. start &&
  12454. start.nodeType == 1 &&
  12455. start.tagName == "A" &&
  12456. /^(?:https?|ftp|file)\s*:\s*\/\//.test(
  12457. start[browser.ie ? "innerText" : "textContent"]
  12458. )
  12459. ) {
  12460. start[browser.ie ? "innerText" : "textContent"] = utils.html(
  12461. opt.textValue || opt.href
  12462. );
  12463. }
  12464. }
  12465. if (!rngClone.collapsed || link) {
  12466. range.removeInlineStyle("a");
  12467. rngClone = range.cloneRange();
  12468. }
  12469. if (rngClone.collapsed) {
  12470. var a = range.document.createElement("a"),
  12471. text = "";
  12472. if (opt.textValue) {
  12473. text = utils.html(opt.textValue);
  12474. delete opt.textValue;
  12475. } else {
  12476. text = utils.html(opt.href);
  12477. }
  12478. domUtils.setAttributes(a, opt);
  12479. start = domUtils.findParentByTagName(rngClone.startContainer, "a", true);
  12480. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  12481. range.setStartAfter(start).collapse(true);
  12482. }
  12483. a[browser.ie ? "innerText" : "textContent"] = text;
  12484. range.insertNode(a).selectNode(a);
  12485. } else {
  12486. range.applyInlineStyle("a", opt);
  12487. }
  12488. }
  12489. UE.commands["link"] = {
  12490. execCommand: function(cmdName, opt) {
  12491. var range;
  12492. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
  12493. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
  12494. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
  12495. doLink((range = this.selection.getRange()), opt, this);
  12496. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  12497. range.collapse().select(true);
  12498. },
  12499. queryCommandValue: function() {
  12500. var range = this.selection.getRange(),
  12501. node;
  12502. if (range.collapsed) {
  12503. // node = this.selection.getStart();
  12504. //在ie下getstart()取值偏上了
  12505. node = range.startContainer;
  12506. node = node.nodeType == 1 ? node : node.parentNode;
  12507. if (
  12508. node &&
  12509. (node = domUtils.findParentByTagName(node, "a", true)) &&
  12510. !domUtils.isInNodeEndBoundary(range, node)
  12511. ) {
  12512. return node;
  12513. }
  12514. } else {
  12515. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  12516. range.shrinkBoundary();
  12517. var start = range.startContainer.nodeType == 3 ||
  12518. !range.startContainer.childNodes[range.startOffset]
  12519. ? range.startContainer
  12520. : range.startContainer.childNodes[range.startOffset],
  12521. end = range.endContainer.nodeType == 3 || range.endOffset == 0
  12522. ? range.endContainer
  12523. : range.endContainer.childNodes[range.endOffset - 1],
  12524. common = range.getCommonAncestor();
  12525. node = domUtils.findParentByTagName(common, "a", true);
  12526. if (!node && common.nodeType == 1) {
  12527. var as = common.getElementsByTagName("a"),
  12528. ps,
  12529. pe;
  12530. for (var i = 0, ci; (ci = as[i++]); ) {
  12531. (ps = domUtils.getPosition(ci, start)), (pe = domUtils.getPosition(
  12532. ci,
  12533. end
  12534. ));
  12535. if (
  12536. (ps & domUtils.POSITION_FOLLOWING ||
  12537. ps & domUtils.POSITION_CONTAINS) &&
  12538. (pe & domUtils.POSITION_PRECEDING ||
  12539. pe & domUtils.POSITION_CONTAINS)
  12540. ) {
  12541. node = ci;
  12542. break;
  12543. }
  12544. }
  12545. }
  12546. return node;
  12547. }
  12548. },
  12549. queryCommandState: function() {
  12550. //判断如果是视频的话连接不可用
  12551. //fix 853
  12552. var img = this.selection.getRange().getClosedNode(),
  12553. flag =
  12554. img &&
  12555. (img.className == "edui-faked-video" ||
  12556. img.className.indexOf("edui-upload-video") != -1);
  12557. return flag ? -1 : 0;
  12558. }
  12559. };
  12560. };
  12561. // plugins/iframe.js
  12562. ///import core
  12563. ///import plugins\inserthtml.js
  12564. ///commands 插入框架
  12565. ///commandsName InsertFrame
  12566. ///commandsTitle 插入Iframe
  12567. ///commandsDialog dialogs\insertframe
  12568. UE.plugins["insertframe"] = function() {
  12569. var me = this;
  12570. function deleteIframe() {
  12571. me._iframe && delete me._iframe;
  12572. }
  12573. me.addListener("selectionchange", function() {
  12574. deleteIframe();
  12575. });
  12576. };
  12577. // plugins/scrawl.js
  12578. ///import core
  12579. ///commands 涂鸦
  12580. ///commandsName Scrawl
  12581. ///commandsTitle 涂鸦
  12582. ///commandsDialog dialogs\scrawl
  12583. UE.commands["scrawl"] = {
  12584. queryCommandState: function() {
  12585. return browser.ie && browser.version <= 8 ? -1 : 0;
  12586. }
  12587. };
  12588. // plugins/removeformat.js
  12589. /**
  12590. * 清除格式
  12591. * @file
  12592. * @since 1.2.6.1
  12593. */
  12594. /**
  12595. * 清除文字样式
  12596. * @command removeformat
  12597. * @method execCommand
  12598. * @param { String } cmd 命令字符串
  12599. * @param {String} tags 以逗号隔开的标签。如:strong
  12600. * @param {String} style 样式如:color
  12601. * @param {String} attrs 属性如:width
  12602. * @example
  12603. * ```javascript
  12604. * editor.execCommand( 'removeformat', 'strong','color','width' );
  12605. * ```
  12606. */
  12607. UE.plugins["removeformat"] = function() {
  12608. var me = this;
  12609. me.setOpt({
  12610. removeFormatTags:
  12611. "b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var",
  12612. removeFormatAttributes: "class,style,lang,width,height,align,hspace,valign"
  12613. });
  12614. me.commands["removeformat"] = {
  12615. execCommand: function(cmdName, tags, style, attrs, notIncludeA) {
  12616. var tagReg = new RegExp(
  12617. "^(?:" +
  12618. (tags || this.options.removeFormatTags).replace(/,/g, "|") +
  12619. ")$",
  12620. "i"
  12621. ),
  12622. removeFormatAttributes = style
  12623. ? []
  12624. : (attrs || this.options.removeFormatAttributes).split(","),
  12625. range = new dom.Range(this.document),
  12626. bookmark,
  12627. node,
  12628. parent,
  12629. filter = function(node) {
  12630. return node.nodeType == 1;
  12631. };
  12632. function isRedundantSpan(node) {
  12633. if (node.nodeType == 3 || node.tagName.toLowerCase() != "span") {
  12634. return 0;
  12635. }
  12636. if (browser.ie) {
  12637. //ie 下判断实效,所以只能简单用style来判断
  12638. //return node.style.cssText == '' ? 1 : 0;
  12639. var attrs = node.attributes;
  12640. if (attrs.length) {
  12641. for (var i = 0, l = attrs.length; i < l; i++) {
  12642. if (attrs[i].specified) {
  12643. return 0;
  12644. }
  12645. }
  12646. return 1;
  12647. }
  12648. }
  12649. return !node.attributes.length;
  12650. }
  12651. function doRemove(range) {
  12652. var bookmark1 = range.createBookmark();
  12653. if (range.collapsed) {
  12654. range.enlarge(true);
  12655. }
  12656. //不能把a标签切了
  12657. if (!notIncludeA) {
  12658. var aNode = domUtils.findParentByTagName(
  12659. range.startContainer,
  12660. "a",
  12661. true
  12662. );
  12663. if (aNode) {
  12664. range.setStartBefore(aNode);
  12665. }
  12666. aNode = domUtils.findParentByTagName(range.endContainer, "a", true);
  12667. if (aNode) {
  12668. range.setEndAfter(aNode);
  12669. }
  12670. }
  12671. bookmark = range.createBookmark();
  12672. node = bookmark.start;
  12673. //切开始
  12674. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12675. domUtils.breakParent(node, parent);
  12676. domUtils.clearEmptySibling(node);
  12677. }
  12678. if (bookmark.end) {
  12679. //切结束
  12680. node = bookmark.end;
  12681. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12682. domUtils.breakParent(node, parent);
  12683. domUtils.clearEmptySibling(node);
  12684. }
  12685. //开始去除样式
  12686. var current = domUtils.getNextDomNode(bookmark.start, false, filter),
  12687. next;
  12688. while (current) {
  12689. if (current == bookmark.end) {
  12690. break;
  12691. }
  12692. next = domUtils.getNextDomNode(current, true, filter);
  12693. if (
  12694. !dtd.$empty[current.tagName.toLowerCase()] &&
  12695. !domUtils.isBookmarkNode(current)
  12696. ) {
  12697. if (tagReg.test(current.tagName)) {
  12698. if (style) {
  12699. domUtils.removeStyle(current, style);
  12700. if (isRedundantSpan(current) && style != "text-decoration") {
  12701. domUtils.remove(current, true);
  12702. }
  12703. } else {
  12704. domUtils.remove(current, true);
  12705. }
  12706. } else {
  12707. //trace:939 不能把list上的样式去掉
  12708. if (
  12709. !dtd.$tableContent[current.tagName] &&
  12710. !dtd.$list[current.tagName]
  12711. ) {
  12712. domUtils.removeAttributes(current, removeFormatAttributes);
  12713. if (isRedundantSpan(current)) {
  12714. domUtils.remove(current, true);
  12715. }
  12716. }
  12717. }
  12718. }
  12719. current = next;
  12720. }
  12721. }
  12722. //trace:1035
  12723. //trace:1096 不能把td上的样式去掉,比如边框
  12724. var pN = bookmark.start.parentNode;
  12725. if (
  12726. domUtils.isBlockElm(pN) &&
  12727. !dtd.$tableContent[pN.tagName] &&
  12728. !dtd.$list[pN.tagName]
  12729. ) {
  12730. domUtils.removeAttributes(pN, removeFormatAttributes);
  12731. }
  12732. pN = bookmark.end.parentNode;
  12733. if (
  12734. bookmark.end &&
  12735. domUtils.isBlockElm(pN) &&
  12736. !dtd.$tableContent[pN.tagName] &&
  12737. !dtd.$list[pN.tagName]
  12738. ) {
  12739. domUtils.removeAttributes(pN, removeFormatAttributes);
  12740. }
  12741. range.moveToBookmark(bookmark).moveToBookmark(bookmark1);
  12742. //清除冗余的代码 <b><bookmark></b>
  12743. var node = range.startContainer,
  12744. tmp,
  12745. collapsed = range.collapsed;
  12746. while (
  12747. node.nodeType == 1 &&
  12748. domUtils.isEmptyNode(node) &&
  12749. dtd.$removeEmpty[node.tagName]
  12750. ) {
  12751. tmp = node.parentNode;
  12752. range.setStartBefore(node);
  12753. //trace:937
  12754. //更新结束边界
  12755. if (range.startContainer === range.endContainer) {
  12756. range.endOffset--;
  12757. }
  12758. domUtils.remove(node);
  12759. node = tmp;
  12760. }
  12761. if (!collapsed) {
  12762. node = range.endContainer;
  12763. while (
  12764. node.nodeType == 1 &&
  12765. domUtils.isEmptyNode(node) &&
  12766. dtd.$removeEmpty[node.tagName]
  12767. ) {
  12768. tmp = node.parentNode;
  12769. range.setEndBefore(node);
  12770. domUtils.remove(node);
  12771. node = tmp;
  12772. }
  12773. }
  12774. }
  12775. range = this.selection.getRange();
  12776. doRemove(range);
  12777. range.select();
  12778. }
  12779. };
  12780. };
  12781. // plugins/blockquote.js
  12782. /**
  12783. * 添加引用
  12784. * @file
  12785. * @since 1.2.6.1
  12786. */
  12787. /**
  12788. * 添加引用
  12789. * @command blockquote
  12790. * @method execCommand
  12791. * @param { String } cmd 命令字符串
  12792. * @example
  12793. * ```javascript
  12794. * editor.execCommand( 'blockquote' );
  12795. * ```
  12796. */
  12797. /**
  12798. * 添加引用
  12799. * @command blockquote
  12800. * @method execCommand
  12801. * @param { String } cmd 命令字符串
  12802. * @param { Object } attrs 节点属性
  12803. * @example
  12804. * ```javascript
  12805. * editor.execCommand( 'blockquote',{
  12806. * style: "color: red;"
  12807. * } );
  12808. * ```
  12809. */
  12810. UE.plugins["blockquote"] = function() {
  12811. var me = this;
  12812. function getObj(editor) {
  12813. return domUtils.filterNodeList(
  12814. editor.selection.getStartElementPath(),
  12815. "blockquote"
  12816. );
  12817. }
  12818. me.commands["blockquote"] = {
  12819. execCommand: function(cmdName, attrs) {
  12820. var range = this.selection.getRange(),
  12821. obj = getObj(this),
  12822. blockquote = dtd.blockquote,
  12823. bookmark = range.createBookmark();
  12824. if (obj) {
  12825. var start = range.startContainer,
  12826. startBlock = domUtils.isBlockElm(start)
  12827. ? start
  12828. : domUtils.findParent(start, function(node) {
  12829. return domUtils.isBlockElm(node);
  12830. }),
  12831. end = range.endContainer,
  12832. endBlock = domUtils.isBlockElm(end)
  12833. ? end
  12834. : domUtils.findParent(end, function(node) {
  12835. return domUtils.isBlockElm(node);
  12836. });
  12837. //处理一下li
  12838. startBlock =
  12839. domUtils.findParentByTagName(startBlock, "li", true) || startBlock;
  12840. endBlock =
  12841. domUtils.findParentByTagName(endBlock, "li", true) || endBlock;
  12842. if (
  12843. startBlock.tagName == "LI" ||
  12844. startBlock.tagName == "TD" ||
  12845. startBlock === obj ||
  12846. domUtils.isBody(startBlock)
  12847. ) {
  12848. domUtils.remove(obj, true);
  12849. } else {
  12850. domUtils.breakParent(startBlock, obj);
  12851. }
  12852. if (startBlock !== endBlock) {
  12853. obj = domUtils.findParentByTagName(endBlock, "blockquote");
  12854. if (obj) {
  12855. if (
  12856. endBlock.tagName == "LI" ||
  12857. endBlock.tagName == "TD" ||
  12858. domUtils.isBody(endBlock)
  12859. ) {
  12860. obj.parentNode && domUtils.remove(obj, true);
  12861. } else {
  12862. domUtils.breakParent(endBlock, obj);
  12863. }
  12864. }
  12865. }
  12866. var blockquotes = domUtils.getElementsByTagName(
  12867. this.document,
  12868. "blockquote"
  12869. );
  12870. for (var i = 0, bi; (bi = blockquotes[i++]); ) {
  12871. if (!bi.childNodes.length) {
  12872. domUtils.remove(bi);
  12873. } else if (
  12874. domUtils.getPosition(bi, startBlock) &
  12875. domUtils.POSITION_FOLLOWING &&
  12876. domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING
  12877. ) {
  12878. domUtils.remove(bi, true);
  12879. }
  12880. }
  12881. } else {
  12882. var tmpRange = range.cloneRange(),
  12883. node = tmpRange.startContainer.nodeType == 1
  12884. ? tmpRange.startContainer
  12885. : tmpRange.startContainer.parentNode,
  12886. preNode = node,
  12887. doEnd = 1;
  12888. //调整开始
  12889. while (1) {
  12890. if (domUtils.isBody(node)) {
  12891. if (preNode !== node) {
  12892. if (range.collapsed) {
  12893. tmpRange.selectNode(preNode);
  12894. doEnd = 0;
  12895. } else {
  12896. tmpRange.setStartBefore(preNode);
  12897. }
  12898. } else {
  12899. tmpRange.setStart(node, 0);
  12900. }
  12901. break;
  12902. }
  12903. if (!blockquote[node.tagName]) {
  12904. if (range.collapsed) {
  12905. tmpRange.selectNode(preNode);
  12906. } else {
  12907. tmpRange.setStartBefore(preNode);
  12908. }
  12909. break;
  12910. }
  12911. preNode = node;
  12912. node = node.parentNode;
  12913. }
  12914. //调整结束
  12915. if (doEnd) {
  12916. preNode = node = node = tmpRange.endContainer.nodeType == 1
  12917. ? tmpRange.endContainer
  12918. : tmpRange.endContainer.parentNode;
  12919. while (1) {
  12920. if (domUtils.isBody(node)) {
  12921. if (preNode !== node) {
  12922. tmpRange.setEndAfter(preNode);
  12923. } else {
  12924. tmpRange.setEnd(node, node.childNodes.length);
  12925. }
  12926. break;
  12927. }
  12928. if (!blockquote[node.tagName]) {
  12929. tmpRange.setEndAfter(preNode);
  12930. break;
  12931. }
  12932. preNode = node;
  12933. node = node.parentNode;
  12934. }
  12935. }
  12936. node = range.document.createElement("blockquote");
  12937. domUtils.setAttributes(node, attrs);
  12938. node.appendChild(tmpRange.extractContents());
  12939. tmpRange.insertNode(node);
  12940. //去除重复的
  12941. var childs = domUtils.getElementsByTagName(node, "blockquote");
  12942. for (var i = 0, ci; (ci = childs[i++]); ) {
  12943. if (ci.parentNode) {
  12944. domUtils.remove(ci, true);
  12945. }
  12946. }
  12947. }
  12948. range.moveToBookmark(bookmark).select();
  12949. },
  12950. queryCommandState: function() {
  12951. return getObj(this) ? 1 : 0;
  12952. }
  12953. };
  12954. };
  12955. // plugins/convertcase.js
  12956. /**
  12957. * 大小写转换
  12958. * @file
  12959. * @since 1.2.6.1
  12960. */
  12961. /**
  12962. * 把选区内文本变大写,与“tolowercase”命令互斥
  12963. * @command touppercase
  12964. * @method execCommand
  12965. * @param { String } cmd 命令字符串
  12966. * @example
  12967. * ```javascript
  12968. * editor.execCommand( 'touppercase' );
  12969. * ```
  12970. */
  12971. /**
  12972. * 把选区内文本变小写,与“touppercase”命令互斥
  12973. * @command tolowercase
  12974. * @method execCommand
  12975. * @param { String } cmd 命令字符串
  12976. * @example
  12977. * ```javascript
  12978. * editor.execCommand( 'tolowercase' );
  12979. * ```
  12980. */
  12981. UE.commands["touppercase"] = UE.commands["tolowercase"] = {
  12982. execCommand: function(cmd) {
  12983. var me = this;
  12984. var rng = me.selection.getRange();
  12985. if (rng.collapsed) {
  12986. return rng;
  12987. }
  12988. var bk = rng.createBookmark(),
  12989. bkEnd = bk.end,
  12990. filterFn = function(node) {
  12991. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  12992. },
  12993. curNode = domUtils.getNextDomNode(bk.start, false, filterFn);
  12994. while (
  12995. curNode &&
  12996. domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING
  12997. ) {
  12998. if (curNode.nodeType == 3) {
  12999. curNode.nodeValue = curNode.nodeValue[
  13000. cmd == "touppercase" ? "toUpperCase" : "toLowerCase"
  13001. ]();
  13002. }
  13003. curNode = domUtils.getNextDomNode(curNode, true, filterFn);
  13004. if (curNode === bkEnd) {
  13005. break;
  13006. }
  13007. }
  13008. rng.moveToBookmark(bk).select();
  13009. }
  13010. };
  13011. // plugins/indent.js
  13012. /**
  13013. * 首行缩进
  13014. * @file
  13015. * @since 1.2.6.1
  13016. */
  13017. /**
  13018. * 缩进
  13019. * @command indent
  13020. * @method execCommand
  13021. * @param { String } cmd 命令字符串
  13022. * @example
  13023. * ```javascript
  13024. * editor.execCommand( 'indent' );
  13025. * ```
  13026. */
  13027. UE.commands["indent"] = {
  13028. execCommand: function() {
  13029. var me = this,
  13030. value = me.queryCommandState("indent")
  13031. ? "0em"
  13032. : me.options.indentValue || "2em";
  13033. me.execCommand("Paragraph", "p", { style: "text-indent:" + value });
  13034. },
  13035. queryCommandState: function() {
  13036. var pN = domUtils.filterNodeList(
  13037. this.selection.getStartElementPath(),
  13038. "p h1 h2 h3 h4 h5 h6"
  13039. );
  13040. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0;
  13041. }
  13042. };
  13043. // plugins/print.js
  13044. /**
  13045. * 打印
  13046. * @file
  13047. * @since 1.2.6.1
  13048. */
  13049. /**
  13050. * 打印
  13051. * @command print
  13052. * @method execCommand
  13053. * @param { String } cmd 命令字符串
  13054. * @example
  13055. * ```javascript
  13056. * editor.execCommand( 'print' );
  13057. * ```
  13058. */
  13059. UE.commands["print"] = {
  13060. execCommand: function() {
  13061. this.window.print();
  13062. },
  13063. notNeedUndo: 1
  13064. };
  13065. // plugins/preview.js
  13066. /**
  13067. * 预览
  13068. * @file
  13069. * @since 1.2.6.1
  13070. */
  13071. /**
  13072. * 预览
  13073. * @command preview
  13074. * @method execCommand
  13075. * @param { String } cmd 命令字符串
  13076. * @example
  13077. * ```javascript
  13078. * editor.execCommand( 'preview' );
  13079. * ```
  13080. */
  13081. UE.commands["preview"] = {
  13082. execCommand: function() {
  13083. var w = window.open("", "_blank", ""),
  13084. d = w.document;
  13085. d.open();
  13086. d.write(
  13087. '<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' +
  13088. this.options.UEDITOR_HOME_URL +
  13089. 'ueditor.parse.js"></script><script>' +
  13090. "setTimeout(function(){uParse('div',{rootPath: '" +
  13091. this.options.UEDITOR_HOME_URL +
  13092. "'})},300)" +
  13093. "</script></head><body><div>" +
  13094. this.getContent(null, null, true) +
  13095. "</div></body></html>"
  13096. );
  13097. d.close();
  13098. },
  13099. notNeedUndo: 1
  13100. };
  13101. // plugins/selectall.js
  13102. /**
  13103. * 全选
  13104. * @file
  13105. * @since 1.2.6.1
  13106. */
  13107. /**
  13108. * 选中所有内容
  13109. * @command selectall
  13110. * @method execCommand
  13111. * @param { String } cmd 命令字符串
  13112. * @example
  13113. * ```javascript
  13114. * editor.execCommand( 'selectall' );
  13115. * ```
  13116. */
  13117. UE.plugins["selectall"] = function() {
  13118. var me = this;
  13119. me.commands["selectall"] = {
  13120. execCommand: function() {
  13121. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  13122. var me = this,
  13123. body = me.body,
  13124. range = me.selection.getRange();
  13125. range.selectNodeContents(body);
  13126. if (domUtils.isEmptyBlock(body)) {
  13127. //opera不能自动合并到元素的里边,要手动处理一下
  13128. if (browser.opera && body.firstChild && body.firstChild.nodeType == 1) {
  13129. range.setStartAtFirst(body.firstChild);
  13130. }
  13131. range.collapse(true);
  13132. }
  13133. range.select(true);
  13134. },
  13135. notNeedUndo: 1
  13136. };
  13137. //快捷键
  13138. me.addshortcutkey({
  13139. selectAll: "ctrl+65"
  13140. });
  13141. };
  13142. // plugins/paragraph.js
  13143. /**
  13144. * 段落样式
  13145. * @file
  13146. * @since 1.2.6.1
  13147. */
  13148. /**
  13149. * 段落格式
  13150. * @command paragraph
  13151. * @method execCommand
  13152. * @param { String } cmd 命令字符串
  13153. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  13154. * @param {Object} attrs 标签的属性
  13155. * @example
  13156. * ```javascript
  13157. * editor.execCommand( 'Paragraph','h1','{
  13158. * class:'test'
  13159. * }' );
  13160. * ```
  13161. */
  13162. /**
  13163. * 返回选区内节点标签名
  13164. * @command paragraph
  13165. * @method queryCommandValue
  13166. * @param { String } cmd 命令字符串
  13167. * @return { String } 节点标签名
  13168. * @example
  13169. * ```javascript
  13170. * editor.queryCommandValue( 'Paragraph' );
  13171. * ```
  13172. */
  13173. UE.plugins["paragraph"] = function() {
  13174. var me = this,
  13175. block = domUtils.isBlockElm,
  13176. notExchange = ["TD", "LI", "PRE"],
  13177. doParagraph = function(range, style, attrs, sourceCmdName) {
  13178. var bookmark = range.createBookmark(),
  13179. filterFn = function(node) {
  13180. return node.nodeType == 1
  13181. ? node.tagName.toLowerCase() != "br" &&
  13182. !domUtils.isBookmarkNode(node)
  13183. : !domUtils.isWhitespace(node);
  13184. },
  13185. para;
  13186. range.enlarge(true);
  13187. var bookmark2 = range.createBookmark(),
  13188. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13189. tmpRange = range.cloneRange(),
  13190. tmpNode;
  13191. while (
  13192. current &&
  13193. !(
  13194. domUtils.getPosition(current, bookmark2.end) &
  13195. domUtils.POSITION_FOLLOWING
  13196. )
  13197. ) {
  13198. if (current.nodeType == 3 || !block(current)) {
  13199. tmpRange.setStartBefore(current);
  13200. while (current && current !== bookmark2.end && !block(current)) {
  13201. tmpNode = current;
  13202. current = domUtils.getNextDomNode(current, false, null, function(
  13203. node
  13204. ) {
  13205. return !block(node);
  13206. });
  13207. }
  13208. tmpRange.setEndAfter(tmpNode);
  13209. para = range.document.createElement(style);
  13210. if (attrs) {
  13211. domUtils.setAttributes(para, attrs);
  13212. if (
  13213. sourceCmdName &&
  13214. sourceCmdName == "customstyle" &&
  13215. attrs.style
  13216. ) {
  13217. para.style.cssText = attrs.style;
  13218. }
  13219. }
  13220. para.appendChild(tmpRange.extractContents());
  13221. //需要内容占位
  13222. if (domUtils.isEmptyNode(para)) {
  13223. domUtils.fillChar(range.document, para);
  13224. }
  13225. tmpRange.insertNode(para);
  13226. var parent = para.parentNode;
  13227. //如果para上一级是一个block元素且不是body,td就删除它
  13228. if (
  13229. block(parent) &&
  13230. !domUtils.isBody(para.parentNode) &&
  13231. utils.indexOf(notExchange, parent.tagName) == -1
  13232. ) {
  13233. //存储dir,style
  13234. if (!(sourceCmdName && sourceCmdName == "customstyle")) {
  13235. parent.getAttribute("dir") &&
  13236. para.setAttribute("dir", parent.getAttribute("dir"));
  13237. //trace:1070
  13238. parent.style.cssText &&
  13239. (para.style.cssText =
  13240. parent.style.cssText + ";" + para.style.cssText);
  13241. //trace:1030
  13242. parent.style.textAlign &&
  13243. !para.style.textAlign &&
  13244. (para.style.textAlign = parent.style.textAlign);
  13245. parent.style.textIndent &&
  13246. !para.style.textIndent &&
  13247. (para.style.textIndent = parent.style.textIndent);
  13248. parent.style.padding &&
  13249. !para.style.padding &&
  13250. (para.style.padding = parent.style.padding);
  13251. }
  13252. //trace:1706 选择的就是h1-6要删除
  13253. if (
  13254. attrs &&
  13255. /h\d/i.test(parent.tagName) &&
  13256. !/h\d/i.test(para.tagName)
  13257. ) {
  13258. domUtils.setAttributes(parent, attrs);
  13259. if (
  13260. sourceCmdName &&
  13261. sourceCmdName == "customstyle" &&
  13262. attrs.style
  13263. ) {
  13264. parent.style.cssText = attrs.style;
  13265. }
  13266. domUtils.remove(para.parentNode, true);
  13267. para = parent;
  13268. } else {
  13269. domUtils.remove(para.parentNode, true);
  13270. }
  13271. }
  13272. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  13273. current = parent;
  13274. } else {
  13275. current = para;
  13276. }
  13277. current = domUtils.getNextDomNode(current, false, filterFn);
  13278. } else {
  13279. current = domUtils.getNextDomNode(current, true, filterFn);
  13280. }
  13281. }
  13282. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  13283. };
  13284. me.setOpt("paragraph", {
  13285. p: "",
  13286. h1: "",
  13287. h2: "",
  13288. h3: "",
  13289. h4: "",
  13290. h5: "",
  13291. h6: ""
  13292. });
  13293. me.commands["paragraph"] = {
  13294. execCommand: function(cmdName, style, attrs, sourceCmdName) {
  13295. var range = this.selection.getRange();
  13296. //闭合时单独处理
  13297. if (range.collapsed) {
  13298. var txt = this.document.createTextNode("p");
  13299. range.insertNode(txt);
  13300. //去掉冗余的fillchar
  13301. if (browser.ie) {
  13302. var node = txt.previousSibling;
  13303. if (node && domUtils.isWhitespace(node)) {
  13304. domUtils.remove(node);
  13305. }
  13306. node = txt.nextSibling;
  13307. if (node && domUtils.isWhitespace(node)) {
  13308. domUtils.remove(node);
  13309. }
  13310. }
  13311. }
  13312. range = doParagraph(range, style, attrs, sourceCmdName);
  13313. if (txt) {
  13314. range.setStartBefore(txt).collapse(true);
  13315. pN = txt.parentNode;
  13316. domUtils.remove(txt);
  13317. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  13318. domUtils.fillNode(this.document, pN);
  13319. }
  13320. }
  13321. if (
  13322. browser.gecko &&
  13323. range.collapsed &&
  13324. range.startContainer.nodeType == 1
  13325. ) {
  13326. var child = range.startContainer.childNodes[range.startOffset];
  13327. if (
  13328. child &&
  13329. child.nodeType == 1 &&
  13330. child.tagName.toLowerCase() == style
  13331. ) {
  13332. range.setStart(child, 0).collapse(true);
  13333. }
  13334. }
  13335. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  13336. range.select();
  13337. return true;
  13338. },
  13339. queryCommandValue: function() {
  13340. var node = domUtils.filterNodeList(
  13341. this.selection.getStartElementPath(),
  13342. "p h1 h2 h3 h4 h5 h6"
  13343. );
  13344. return node ? node.tagName.toLowerCase() : "";
  13345. }
  13346. };
  13347. };
  13348. // plugins/directionality.js
  13349. /**
  13350. * 设置文字输入的方向的插件
  13351. * @file
  13352. * @since 1.2.6.1
  13353. */
  13354. ;(function() {
  13355. var block = domUtils.isBlockElm,
  13356. getObj = function(editor) {
  13357. // var startNode = editor.selection.getStart(),
  13358. // parents;
  13359. // if ( startNode ) {
  13360. // //查找所有的是block的父亲节点
  13361. // parents = domUtils.findParents( startNode, true, block, true );
  13362. // for ( var i = 0,ci; ci = parents[i++]; ) {
  13363. // if ( ci.getAttribute( 'dir' ) ) {
  13364. // return ci;
  13365. // }
  13366. // }
  13367. // }
  13368. return domUtils.filterNodeList(
  13369. editor.selection.getStartElementPath(),
  13370. function(n) {
  13371. return n && n.nodeType == 1 && n.getAttribute("dir");
  13372. }
  13373. );
  13374. },
  13375. doDirectionality = function(range, editor, forward) {
  13376. var bookmark,
  13377. filterFn = function(node) {
  13378. return node.nodeType == 1
  13379. ? !domUtils.isBookmarkNode(node)
  13380. : !domUtils.isWhitespace(node);
  13381. },
  13382. obj = getObj(editor);
  13383. if (obj && range.collapsed) {
  13384. obj.setAttribute("dir", forward);
  13385. return range;
  13386. }
  13387. bookmark = range.createBookmark();
  13388. range.enlarge(true);
  13389. var bookmark2 = range.createBookmark(),
  13390. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13391. tmpRange = range.cloneRange(),
  13392. tmpNode;
  13393. while (
  13394. current &&
  13395. !(
  13396. domUtils.getPosition(current, bookmark2.end) &
  13397. domUtils.POSITION_FOLLOWING
  13398. )
  13399. ) {
  13400. if (current.nodeType == 3 || !block(current)) {
  13401. tmpRange.setStartBefore(current);
  13402. while (current && current !== bookmark2.end && !block(current)) {
  13403. tmpNode = current;
  13404. current = domUtils.getNextDomNode(current, false, null, function(
  13405. node
  13406. ) {
  13407. return !block(node);
  13408. });
  13409. }
  13410. tmpRange.setEndAfter(tmpNode);
  13411. var common = tmpRange.getCommonAncestor();
  13412. if (!domUtils.isBody(common) && block(common)) {
  13413. //遍历到了block节点
  13414. common.setAttribute("dir", forward);
  13415. current = common;
  13416. } else {
  13417. //没有遍历到,添加一个block节点
  13418. var p = range.document.createElement("p");
  13419. p.setAttribute("dir", forward);
  13420. var frag = tmpRange.extractContents();
  13421. p.appendChild(frag);
  13422. tmpRange.insertNode(p);
  13423. current = p;
  13424. }
  13425. current = domUtils.getNextDomNode(current, false, filterFn);
  13426. } else {
  13427. current = domUtils.getNextDomNode(current, true, filterFn);
  13428. }
  13429. }
  13430. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  13431. };
  13432. /**
  13433. * 文字输入方向
  13434. * @command directionality
  13435. * @method execCommand
  13436. * @param { String } cmdName 命令字符串
  13437. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  13438. * @example
  13439. * ```javascript
  13440. * editor.execCommand( 'directionality', 'ltr');
  13441. * ```
  13442. */
  13443. /**
  13444. * 查询当前选区的文字输入方向
  13445. * @command directionality
  13446. * @method queryCommandValue
  13447. * @param { String } cmdName 命令字符串
  13448. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  13449. * @example
  13450. * ```javascript
  13451. * editor.queryCommandValue( 'directionality');
  13452. * ```
  13453. */
  13454. UE.commands["directionality"] = {
  13455. execCommand: function(cmdName, forward) {
  13456. var range = this.selection.getRange();
  13457. //闭合时单独处理
  13458. if (range.collapsed) {
  13459. var txt = this.document.createTextNode("d");
  13460. range.insertNode(txt);
  13461. }
  13462. doDirectionality(range, this, forward);
  13463. if (txt) {
  13464. range.setStartBefore(txt).collapse(true);
  13465. domUtils.remove(txt);
  13466. }
  13467. range.select();
  13468. return true;
  13469. },
  13470. queryCommandValue: function() {
  13471. var node = getObj(this);
  13472. return node ? node.getAttribute("dir") : "ltr";
  13473. }
  13474. };
  13475. })();
  13476. // plugins/horizontal.js
  13477. /**
  13478. * 插入分割线插件
  13479. * @file
  13480. * @since 1.2.6.1
  13481. */
  13482. /**
  13483. * 插入分割线
  13484. * @command horizontal
  13485. * @method execCommand
  13486. * @param { String } cmdName 命令字符串
  13487. * @example
  13488. * ```javascript
  13489. * editor.execCommand( 'horizontal' );
  13490. * ```
  13491. */
  13492. UE.plugins["horizontal"] = function() {
  13493. var me = this;
  13494. me.commands["horizontal"] = {
  13495. execCommand: function(cmdName) {
  13496. var me = this;
  13497. if (me.queryCommandState(cmdName) !== -1) {
  13498. me.execCommand("insertHtml", "<hr>");
  13499. var range = me.selection.getRange(),
  13500. start = range.startContainer;
  13501. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  13502. var tmp;
  13503. if ((tmp = start.childNodes[range.startOffset - 1])) {
  13504. if (tmp.nodeType == 1 && tmp.tagName == "HR") {
  13505. if (me.options.enterTag == "p") {
  13506. tmp = me.document.createElement("p");
  13507. range.insertNode(tmp);
  13508. range.setStart(tmp, 0).setCursor();
  13509. } else {
  13510. tmp = me.document.createElement("br");
  13511. range.insertNode(tmp);
  13512. range.setStartBefore(tmp).setCursor();
  13513. }
  13514. }
  13515. }
  13516. }
  13517. return true;
  13518. }
  13519. },
  13520. //边界在table里不能加分隔线
  13521. queryCommandState: function() {
  13522. return domUtils.filterNodeList(
  13523. this.selection.getStartElementPath(),
  13524. "table"
  13525. )
  13526. ? -1
  13527. : 0;
  13528. }
  13529. };
  13530. // me.addListener('delkeyup',function(){
  13531. // var rng = this.selection.getRange();
  13532. // if(browser.ie && browser.version > 8){
  13533. // rng.txtToElmBoundary(true);
  13534. // if(domUtils.isStartInblock(rng)){
  13535. // var tmpNode = rng.startContainer;
  13536. // var pre = tmpNode.previousSibling;
  13537. // if(pre && domUtils.isTagNode(pre,'hr')){
  13538. // domUtils.remove(pre);
  13539. // rng.select();
  13540. // return;
  13541. // }
  13542. // }
  13543. // }
  13544. // if(domUtils.isBody(rng.startContainer)){
  13545. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  13546. // if(hr && hr.nodeName == 'HR'){
  13547. // var next = hr.nextSibling;
  13548. // if(next){
  13549. // rng.setStart(next,0)
  13550. // }else if(hr.previousSibling){
  13551. // rng.setStartAtLast(hr.previousSibling)
  13552. // }else{
  13553. // var p = this.document.createElement('p');
  13554. // hr.parentNode.insertBefore(p,hr);
  13555. // domUtils.fillNode(this.document,p);
  13556. // rng.setStart(p,0);
  13557. // }
  13558. // domUtils.remove(hr);
  13559. // rng.setCursor(false,true);
  13560. // }
  13561. // }
  13562. // })
  13563. me.addListener("delkeydown", function(name, evt) {
  13564. var rng = this.selection.getRange();
  13565. rng.txtToElmBoundary(true);
  13566. if (domUtils.isStartInblock(rng)) {
  13567. var tmpNode = rng.startContainer;
  13568. var pre = tmpNode.previousSibling;
  13569. if (pre && domUtils.isTagNode(pre, "hr")) {
  13570. domUtils.remove(pre);
  13571. rng.select();
  13572. domUtils.preventDefault(evt);
  13573. return true;
  13574. }
  13575. }
  13576. });
  13577. };
  13578. // plugins/time.js
  13579. /**
  13580. * 插入时间和日期
  13581. * @file
  13582. * @since 1.2.6.1
  13583. */
  13584. /**
  13585. * 插入时间,默认格式:12:59:59
  13586. * @command time
  13587. * @method execCommand
  13588. * @param { String } cmd 命令字符串
  13589. * @example
  13590. * ```javascript
  13591. * editor.execCommand( 'time');
  13592. * ```
  13593. */
  13594. /**
  13595. * 插入日期,默认格式:2013-08-30
  13596. * @command date
  13597. * @method execCommand
  13598. * @param { String } cmd 命令字符串
  13599. * @example
  13600. * ```javascript
  13601. * editor.execCommand( 'date');
  13602. * ```
  13603. */
  13604. UE.commands["time"] = UE.commands["date"] = {
  13605. execCommand: function(cmd, format) {
  13606. var date = new Date();
  13607. function formatTime(date, format) {
  13608. var hh = ("0" + date.getHours()).slice(-2),
  13609. ii = ("0" + date.getMinutes()).slice(-2),
  13610. ss = ("0" + date.getSeconds()).slice(-2);
  13611. format = format || "hh:ii:ss";
  13612. return format.replace(/hh/gi, hh).replace(/ii/gi, ii).replace(/ss/gi, ss);
  13613. }
  13614. function formatDate(date, format) {
  13615. var yyyy = ("000" + date.getFullYear()).slice(-4),
  13616. yy = yyyy.slice(-2),
  13617. mm = ("0" + (date.getMonth() + 1)).slice(-2),
  13618. dd = ("0" + date.getDate()).slice(-2);
  13619. format = format || "yyyy-mm-dd";
  13620. return format
  13621. .replace(/yyyy/gi, yyyy)
  13622. .replace(/yy/gi, yy)
  13623. .replace(/mm/gi, mm)
  13624. .replace(/dd/gi, dd);
  13625. }
  13626. this.execCommand(
  13627. "insertHtml",
  13628. cmd == "time" ? formatTime(date, format) : formatDate(date, format)
  13629. );
  13630. }
  13631. };
  13632. // plugins/rowspacing.js
  13633. /**
  13634. * 段前段后间距插件
  13635. * @file
  13636. * @since 1.2.6.1
  13637. */
  13638. /**
  13639. * 设置段间距
  13640. * @command rowspacing
  13641. * @method execCommand
  13642. * @param { String } cmd 命令字符串
  13643. * @param { String } value 段间距的值,以px为单位
  13644. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  13645. * @example
  13646. * ```javascript
  13647. * editor.execCommand( 'rowspacing', '10', 'top' );
  13648. * ```
  13649. */
  13650. UE.plugins["rowspacing"] = function() {
  13651. var me = this;
  13652. me.setOpt({
  13653. rowspacingtop: ["5", "10", "15", "20", "25"],
  13654. rowspacingbottom: ["5", "10", "15", "20", "25"]
  13655. });
  13656. me.commands["rowspacing"] = {
  13657. execCommand: function(cmdName, value, dir) {
  13658. this.execCommand("paragraph", "p", {
  13659. style: "margin-" + dir + ":" + value + "px"
  13660. });
  13661. return true;
  13662. },
  13663. queryCommandValue: function(cmdName, dir) {
  13664. var pN = domUtils.filterNodeList(
  13665. this.selection.getStartElementPath(),
  13666. function(node) {
  13667. return domUtils.isBlockElm(node);
  13668. }
  13669. ),
  13670. value;
  13671. //trace:1026
  13672. if (pN) {
  13673. value = domUtils
  13674. .getComputedStyle(pN, "margin-" + dir)
  13675. .replace(/[^\d]/g, "");
  13676. return !value ? 0 : value;
  13677. }
  13678. return 0;
  13679. }
  13680. };
  13681. };
  13682. // plugins/lineheight.js
  13683. /**
  13684. * 设置行内间距
  13685. * @file
  13686. * @since 1.2.6.1
  13687. */
  13688. UE.plugins["lineheight"] = function() {
  13689. var me = this;
  13690. me.setOpt({ lineheight: ["1", "1.5", "1.75", "2", "3", "4", "5"] });
  13691. /**
  13692. * 行距
  13693. * @command lineheight
  13694. * @method execCommand
  13695. * @param { String } cmdName 命令字符串
  13696. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  13697. * @example
  13698. * ```javascript
  13699. * editor.execCommand( 'lineheight', 1.5);
  13700. * ```
  13701. */
  13702. /**
  13703. * 查询当前选区内容的行高大小
  13704. * @command lineheight
  13705. * @method queryCommandValue
  13706. * @param { String } cmd 命令字符串
  13707. * @return { String } 返回当前行高大小
  13708. * @example
  13709. * ```javascript
  13710. * editor.queryCommandValue( 'lineheight' );
  13711. * ```
  13712. */
  13713. me.commands["lineheight"] = {
  13714. execCommand: function(cmdName, value) {
  13715. this.execCommand("paragraph", "p", {
  13716. style: "line-height:" + (value == "1" ? "normal" : value + "em")
  13717. });
  13718. return true;
  13719. },
  13720. queryCommandValue: function() {
  13721. var pN = domUtils.filterNodeList(
  13722. this.selection.getStartElementPath(),
  13723. function(node) {
  13724. return domUtils.isBlockElm(node);
  13725. }
  13726. );
  13727. if (pN) {
  13728. var value = domUtils.getComputedStyle(pN, "line-height");
  13729. return value == "normal" ? 1 : value.replace(/[^\d.]*/gi, "");
  13730. }
  13731. }
  13732. };
  13733. };
  13734. // plugins/insertcode.js
  13735. /**
  13736. * 插入代码插件
  13737. * @file
  13738. * @since 1.2.6.1
  13739. */
  13740. UE.plugins["insertcode"] = function() {
  13741. var me = this;
  13742. me.ready(function() {
  13743. utils.cssRule(
  13744. "pre",
  13745. "pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}",
  13746. me.document
  13747. );
  13748. });
  13749. me.setOpt("insertcode", {
  13750. as3: "ActionScript3",
  13751. bash: "Bash/Shell",
  13752. cpp: "C/C++",
  13753. css: "Css",
  13754. cf: "CodeFunction",
  13755. "c#": "C#",
  13756. delphi: "Delphi",
  13757. diff: "Diff",
  13758. erlang: "Erlang",
  13759. groovy: "Groovy",
  13760. html: "Html",
  13761. java: "Java",
  13762. jfx: "JavaFx",
  13763. js: "Javascript",
  13764. pl: "Perl",
  13765. php: "Php",
  13766. plain: "Plain Text",
  13767. ps: "PowerShell",
  13768. python: "Python",
  13769. ruby: "Ruby",
  13770. scala: "Scala",
  13771. sql: "Sql",
  13772. vb: "Vb",
  13773. xml: "Xml"
  13774. });
  13775. /**
  13776. * 插入代码
  13777. * @command insertcode
  13778. * @method execCommand
  13779. * @param { String } cmd 命令字符串
  13780. * @param { String } lang 插入代码的语言
  13781. * @example
  13782. * ```javascript
  13783. * editor.execCommand( 'insertcode', 'javascript' );
  13784. * ```
  13785. */
  13786. /**
  13787. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  13788. * @command insertcode
  13789. * @method queryCommandValue
  13790. * @param { String } cmd 命令字符串
  13791. * @return { String } 返回代码的语言
  13792. * @example
  13793. * ```javascript
  13794. * editor.queryCommandValue( 'insertcode' );
  13795. * ```
  13796. */
  13797. me.commands["insertcode"] = {
  13798. execCommand: function(cmd, lang) {
  13799. var me = this,
  13800. rng = me.selection.getRange(),
  13801. pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  13802. if (pre) {
  13803. pre.className = "brush:" + lang + ";toolbar:false;";
  13804. } else {
  13805. var code = "";
  13806. if (rng.collapsed) {
  13807. code = browser.ie && browser.ie11below
  13808. ? browser.version <= 8 ? "&nbsp;" : ""
  13809. : "<br/>";
  13810. } else {
  13811. var frag = rng.extractContents();
  13812. var div = me.document.createElement("div");
  13813. div.appendChild(frag);
  13814. utils.each(
  13815. UE.filterNode(
  13816. UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, "")),
  13817. me.options.filterTxtRules
  13818. ).children,
  13819. function(node) {
  13820. if (browser.ie && browser.ie11below && browser.version > 8) {
  13821. if (node.type == "element") {
  13822. if (node.tagName == "br") {
  13823. code += "\n";
  13824. } else if (!dtd.$empty[node.tagName]) {
  13825. utils.each(node.children, function(cn) {
  13826. if (cn.type == "element") {
  13827. if (cn.tagName == "br") {
  13828. code += "\n";
  13829. } else if (!dtd.$empty[node.tagName]) {
  13830. code += cn.innerText();
  13831. }
  13832. } else {
  13833. code += cn.data;
  13834. }
  13835. });
  13836. if (!/\n$/.test(code)) {
  13837. code += "\n";
  13838. }
  13839. }
  13840. } else {
  13841. code += node.data + "\n";
  13842. }
  13843. if (!node.nextSibling() && /\n$/.test(code)) {
  13844. code = code.replace(/\n$/, "");
  13845. }
  13846. } else {
  13847. if (browser.ie && browser.ie11below) {
  13848. if (node.type == "element") {
  13849. if (node.tagName == "br") {
  13850. code += "<br>";
  13851. } else if (!dtd.$empty[node.tagName]) {
  13852. utils.each(node.children, function(cn) {
  13853. if (cn.type == "element") {
  13854. if (cn.tagName == "br") {
  13855. code += "<br>";
  13856. } else if (!dtd.$empty[node.tagName]) {
  13857. code += cn.innerText();
  13858. }
  13859. } else {
  13860. code += cn.data;
  13861. }
  13862. });
  13863. if (!/br>$/.test(code)) {
  13864. code += "<br>";
  13865. }
  13866. }
  13867. } else {
  13868. code += node.data + "<br>";
  13869. }
  13870. if (!node.nextSibling() && /<br>$/.test(code)) {
  13871. code = code.replace(/<br>$/, "");
  13872. }
  13873. } else {
  13874. code += node.type == "element"
  13875. ? dtd.$empty[node.tagName] ? "" : node.innerText()
  13876. : node.data;
  13877. if (!/br\/?\s*>$/.test(code)) {
  13878. if (!node.nextSibling()) return;
  13879. code += "<br>";
  13880. }
  13881. }
  13882. }
  13883. }
  13884. );
  13885. }
  13886. me.execCommand(
  13887. "inserthtml",
  13888. '<pre id="coder"class="brush:' +
  13889. lang +
  13890. ';toolbar:false">' +
  13891. code +
  13892. "</pre>",
  13893. true
  13894. );
  13895. pre = me.document.getElementById("coder");
  13896. domUtils.removeAttributes(pre, "id");
  13897. var tmpNode = pre.previousSibling;
  13898. if (
  13899. tmpNode &&
  13900. ((tmpNode.nodeType == 3 &&
  13901. tmpNode.nodeValue.length == 1 &&
  13902. browser.ie &&
  13903. browser.version == 6) ||
  13904. domUtils.isEmptyBlock(tmpNode))
  13905. ) {
  13906. domUtils.remove(tmpNode);
  13907. }
  13908. var rng = me.selection.getRange();
  13909. if (domUtils.isEmptyBlock(pre)) {
  13910. rng.setStart(pre, 0).setCursor(false, true);
  13911. } else {
  13912. rng.selectNodeContents(pre).select();
  13913. }
  13914. }
  13915. },
  13916. queryCommandValue: function() {
  13917. var path = this.selection.getStartElementPath();
  13918. var lang = "";
  13919. utils.each(path, function(node) {
  13920. if (node.nodeName == "PRE") {
  13921. var match = node.className.match(/brush:([^;]+)/);
  13922. lang = match && match[1] ? match[1] : "";
  13923. return false;
  13924. }
  13925. });
  13926. return lang;
  13927. }
  13928. };
  13929. me.addInputRule(function(root) {
  13930. utils.each(root.getNodesByTagName("pre"), function(pre) {
  13931. var brs = pre.getNodesByTagName("br");
  13932. if (brs.length) {
  13933. browser.ie &&
  13934. browser.ie11below &&
  13935. browser.version > 8 &&
  13936. utils.each(brs, function(br) {
  13937. var txt = UE.uNode.createText("\n");
  13938. br.parentNode.insertBefore(txt, br);
  13939. br.parentNode.removeChild(br);
  13940. });
  13941. return;
  13942. }
  13943. if (browser.ie && browser.ie11below && browser.version > 8) return;
  13944. var code = pre.innerText().split(/\n/);
  13945. pre.innerHTML("");
  13946. utils.each(code, function(c) {
  13947. if (c.length) {
  13948. pre.appendChild(UE.uNode.createText(c));
  13949. }
  13950. pre.appendChild(UE.uNode.createElement("br"));
  13951. });
  13952. });
  13953. });
  13954. me.addOutputRule(function(root) {
  13955. utils.each(root.getNodesByTagName("pre"), function(pre) {
  13956. var code = "";
  13957. utils.each(pre.children, function(n) {
  13958. if (n.type == "text") {
  13959. //在ie下文本内容有可能末尾带有\n要去掉
  13960. //trace:3396
  13961. code += n.data.replace(/[ ]/g, "&nbsp;").replace(/\n$/, "");
  13962. } else {
  13963. if (n.tagName == "br") {
  13964. code += "\n";
  13965. } else {
  13966. code += !dtd.$empty[n.tagName] ? "" : n.innerText();
  13967. }
  13968. }
  13969. });
  13970. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ""));
  13971. });
  13972. });
  13973. //不需要判断highlight的command列表
  13974. me.notNeedCodeQuery = {
  13975. help: 1,
  13976. undo: 1,
  13977. redo: 1,
  13978. source: 1,
  13979. print: 1,
  13980. searchreplace: 1,
  13981. fullscreen: 1,
  13982. preview: 1,
  13983. insertparagraph: 1,
  13984. elementpath: 1,
  13985. insertcode: 1,
  13986. inserthtml: 1,
  13987. selectall: 1
  13988. };
  13989. //将queyCommamndState重置
  13990. var orgQuery = me.queryCommandState;
  13991. me.queryCommandState = function(cmd) {
  13992. var me = this;
  13993. if (
  13994. !me.notNeedCodeQuery[cmd.toLowerCase()] &&
  13995. me.selection &&
  13996. me.queryCommandValue("insertcode")
  13997. ) {
  13998. return -1;
  13999. }
  14000. return UE.Editor.prototype.queryCommandState.apply(this, arguments);
  14001. };
  14002. me.addListener("beforeenterkeydown", function() {
  14003. var rng = me.selection.getRange();
  14004. var pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14005. if (pre) {
  14006. me.fireEvent("saveScene");
  14007. if (!rng.collapsed) {
  14008. rng.deleteContents();
  14009. }
  14010. if (!browser.ie || browser.ie9above) {
  14011. var tmpNode = me.document.createElement("br"),
  14012. pre;
  14013. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true);
  14014. var next = tmpNode.nextSibling;
  14015. if (!next && (!browser.ie || browser.version > 10)) {
  14016. rng.insertNode(tmpNode.cloneNode(false));
  14017. } else {
  14018. rng.setStartAfter(tmpNode);
  14019. }
  14020. pre = tmpNode.previousSibling;
  14021. var tmp;
  14022. while (pre) {
  14023. tmp = pre;
  14024. pre = pre.previousSibling;
  14025. if (!pre || pre.nodeName == "BR") {
  14026. pre = tmp;
  14027. break;
  14028. }
  14029. }
  14030. if (pre) {
  14031. var str = "";
  14032. while (
  14033. pre &&
  14034. pre.nodeName != "BR" &&
  14035. new RegExp("^[\\s" + domUtils.fillChar + "]*$").test(pre.nodeValue)
  14036. ) {
  14037. str += pre.nodeValue;
  14038. pre = pre.nextSibling;
  14039. }
  14040. if (pre.nodeName != "BR") {
  14041. var match = pre.nodeValue.match(
  14042. new RegExp("^([\\s" + domUtils.fillChar + "]+)")
  14043. );
  14044. if (match && match[1]) {
  14045. str += match[1];
  14046. }
  14047. }
  14048. if (str) {
  14049. str = me.document.createTextNode(str);
  14050. rng.insertNode(str).setStartAfter(str);
  14051. }
  14052. }
  14053. rng.collapse(true).select(true);
  14054. } else {
  14055. if (browser.version > 8) {
  14056. var txt = me.document.createTextNode("\n");
  14057. var start = rng.startContainer;
  14058. if (rng.startOffset == 0) {
  14059. var preNode = start.previousSibling;
  14060. if (preNode) {
  14061. rng.insertNode(txt);
  14062. var fillchar = me.document.createTextNode(" ");
  14063. rng
  14064. .setStartAfter(txt)
  14065. .insertNode(fillchar)
  14066. .setStart(fillchar, 0)
  14067. .collapse(true)
  14068. .select(true);
  14069. }
  14070. } else {
  14071. rng.insertNode(txt).setStartAfter(txt);
  14072. var fillchar = me.document.createTextNode(" ");
  14073. start = rng.startContainer.childNodes[rng.startOffset];
  14074. if (start && !/^\n/.test(start.nodeValue)) {
  14075. rng.setStartBefore(txt);
  14076. }
  14077. rng
  14078. .insertNode(fillchar)
  14079. .setStart(fillchar, 0)
  14080. .collapse(true)
  14081. .select(true);
  14082. }
  14083. } else {
  14084. var tmpNode = me.document.createElement("br");
  14085. rng.insertNode(tmpNode);
  14086. rng.insertNode(me.document.createTextNode(domUtils.fillChar));
  14087. rng.setStartAfter(tmpNode);
  14088. pre = tmpNode.previousSibling;
  14089. var tmp;
  14090. while (pre) {
  14091. tmp = pre;
  14092. pre = pre.previousSibling;
  14093. if (!pre || pre.nodeName == "BR") {
  14094. pre = tmp;
  14095. break;
  14096. }
  14097. }
  14098. if (pre) {
  14099. var str = "";
  14100. while (
  14101. pre &&
  14102. pre.nodeName != "BR" &&
  14103. new RegExp("^[ " + domUtils.fillChar + "]*$").test(pre.nodeValue)
  14104. ) {
  14105. str += pre.nodeValue;
  14106. pre = pre.nextSibling;
  14107. }
  14108. if (pre.nodeName != "BR") {
  14109. var match = pre.nodeValue.match(
  14110. new RegExp("^([ " + domUtils.fillChar + "]+)")
  14111. );
  14112. if (match && match[1]) {
  14113. str += match[1];
  14114. }
  14115. }
  14116. str = me.document.createTextNode(str);
  14117. rng.insertNode(str).setStartAfter(str);
  14118. }
  14119. rng.collapse(true).select();
  14120. }
  14121. }
  14122. me.fireEvent("saveScene");
  14123. return true;
  14124. }
  14125. });
  14126. me.addListener("tabkeydown", function(cmd, evt) {
  14127. var rng = me.selection.getRange();
  14128. var pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14129. if (pre) {
  14130. me.fireEvent("saveScene");
  14131. if (evt.shiftKey) {
  14132. } else {
  14133. if (!rng.collapsed) {
  14134. var bk = rng.createBookmark();
  14135. var start = bk.start.previousSibling;
  14136. while (start) {
  14137. if (pre.firstChild === start && !domUtils.isBr(start)) {
  14138. pre.insertBefore(me.document.createTextNode(" "), start);
  14139. break;
  14140. }
  14141. if (domUtils.isBr(start)) {
  14142. pre.insertBefore(
  14143. me.document.createTextNode(" "),
  14144. start.nextSibling
  14145. );
  14146. break;
  14147. }
  14148. start = start.previousSibling;
  14149. }
  14150. var end = bk.end;
  14151. start = bk.start.nextSibling;
  14152. if (pre.firstChild === bk.start) {
  14153. pre.insertBefore(
  14154. me.document.createTextNode(" "),
  14155. start.nextSibling
  14156. );
  14157. }
  14158. while (start && start !== end) {
  14159. if (domUtils.isBr(start) && start.nextSibling) {
  14160. if (start.nextSibling === end) {
  14161. break;
  14162. }
  14163. pre.insertBefore(
  14164. me.document.createTextNode(" "),
  14165. start.nextSibling
  14166. );
  14167. }
  14168. start = start.nextSibling;
  14169. }
  14170. rng.moveToBookmark(bk).select();
  14171. } else {
  14172. var tmpNode = me.document.createTextNode(" ");
  14173. rng
  14174. .insertNode(tmpNode)
  14175. .setStartAfter(tmpNode)
  14176. .collapse(true)
  14177. .select(true);
  14178. }
  14179. }
  14180. me.fireEvent("saveScene");
  14181. return true;
  14182. }
  14183. });
  14184. me.addListener("beforeinserthtml", function(evtName, html) {
  14185. var me = this,
  14186. rng = me.selection.getRange(),
  14187. pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14188. if (pre) {
  14189. if (!rng.collapsed) {
  14190. rng.deleteContents();
  14191. }
  14192. var htmlstr = "";
  14193. if (browser.ie && browser.version > 8) {
  14194. utils.each(
  14195. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14196. .children,
  14197. function(node) {
  14198. if (node.type == "element") {
  14199. if (node.tagName == "br") {
  14200. htmlstr += "\n";
  14201. } else if (!dtd.$empty[node.tagName]) {
  14202. utils.each(node.children, function(cn) {
  14203. if (cn.type == "element") {
  14204. if (cn.tagName == "br") {
  14205. htmlstr += "\n";
  14206. } else if (!dtd.$empty[node.tagName]) {
  14207. htmlstr += cn.innerText();
  14208. }
  14209. } else {
  14210. htmlstr += cn.data;
  14211. }
  14212. });
  14213. if (!/\n$/.test(htmlstr)) {
  14214. htmlstr += "\n";
  14215. }
  14216. }
  14217. } else {
  14218. htmlstr += node.data + "\n";
  14219. }
  14220. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  14221. htmlstr = htmlstr.replace(/\n$/, "");
  14222. }
  14223. }
  14224. );
  14225. var tmpNode = me.document.createTextNode(
  14226. utils.html(htmlstr.replace(/&nbsp;/g, " "))
  14227. );
  14228. rng.insertNode(tmpNode).selectNode(tmpNode).select();
  14229. } else {
  14230. var frag = me.document.createDocumentFragment();
  14231. utils.each(
  14232. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14233. .children,
  14234. function(node) {
  14235. if (node.type == "element") {
  14236. if (node.tagName == "br") {
  14237. frag.appendChild(me.document.createElement("br"));
  14238. } else if (!dtd.$empty[node.tagName]) {
  14239. utils.each(node.children, function(cn) {
  14240. if (cn.type == "element") {
  14241. if (cn.tagName == "br") {
  14242. frag.appendChild(me.document.createElement("br"));
  14243. } else if (!dtd.$empty[node.tagName]) {
  14244. frag.appendChild(
  14245. me.document.createTextNode(
  14246. utils.html(cn.innerText().replace(/&nbsp;/g, " "))
  14247. )
  14248. );
  14249. }
  14250. } else {
  14251. frag.appendChild(
  14252. me.document.createTextNode(
  14253. utils.html(cn.data.replace(/&nbsp;/g, " "))
  14254. )
  14255. );
  14256. }
  14257. });
  14258. if (frag.lastChild.nodeName != "BR") {
  14259. frag.appendChild(me.document.createElement("br"));
  14260. }
  14261. }
  14262. } else {
  14263. frag.appendChild(
  14264. me.document.createTextNode(
  14265. utils.html(node.data.replace(/&nbsp;/g, " "))
  14266. )
  14267. );
  14268. }
  14269. if (!node.nextSibling() && frag.lastChild.nodeName == "BR") {
  14270. frag.removeChild(frag.lastChild);
  14271. }
  14272. }
  14273. );
  14274. rng.insertNode(frag).select();
  14275. }
  14276. return true;
  14277. }
  14278. });
  14279. //方向键的处理
  14280. me.addListener("keydown", function(cmd, evt) {
  14281. var me = this,
  14282. keyCode = evt.keyCode || evt.which;
  14283. if (keyCode == 40) {
  14284. var rng = me.selection.getRange(),
  14285. pre,
  14286. start = rng.startContainer;
  14287. if (
  14288. rng.collapsed &&
  14289. (pre = domUtils.findParentByTagName(rng.startContainer, "pre", true)) &&
  14290. !pre.nextSibling
  14291. ) {
  14292. var last = pre.lastChild;
  14293. while (last && last.nodeName == "BR") {
  14294. last = last.previousSibling;
  14295. }
  14296. if (
  14297. last === start ||
  14298. (rng.startContainer === pre &&
  14299. rng.startOffset == pre.childNodes.length)
  14300. ) {
  14301. me.execCommand("insertparagraph");
  14302. domUtils.preventDefault(evt);
  14303. }
  14304. }
  14305. }
  14306. });
  14307. //trace:3395
  14308. me.addListener("delkeydown", function(type, evt) {
  14309. var rng = this.selection.getRange();
  14310. rng.txtToElmBoundary(true);
  14311. var start = rng.startContainer;
  14312. if (
  14313. domUtils.isTagNode(start, "pre") &&
  14314. rng.collapsed &&
  14315. domUtils.isStartInblock(rng)
  14316. ) {
  14317. var p = me.document.createElement("p");
  14318. domUtils.fillNode(me.document, p);
  14319. start.parentNode.insertBefore(p, start);
  14320. domUtils.remove(start);
  14321. rng.setStart(p, 0).setCursor(false, true);
  14322. domUtils.preventDefault(evt);
  14323. return true;
  14324. }
  14325. });
  14326. };
  14327. // plugins/cleardoc.js
  14328. /**
  14329. * 清空文档插件
  14330. * @file
  14331. * @since 1.2.6.1
  14332. */
  14333. /**
  14334. * 清空文档
  14335. * @command cleardoc
  14336. * @method execCommand
  14337. * @param { String } cmd 命令字符串
  14338. * @example
  14339. * ```javascript
  14340. * //editor 是编辑器实例
  14341. * editor.execCommand('cleardoc');
  14342. * ```
  14343. */
  14344. UE.commands["cleardoc"] = {
  14345. execCommand: function(cmdName) {
  14346. var me = this,
  14347. enterTag = me.options.enterTag,
  14348. range = me.selection.getRange();
  14349. if (enterTag == "br") {
  14350. me.body.innerHTML = "<br/>";
  14351. range.setStart(me.body, 0).setCursor();
  14352. } else {
  14353. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  14354. range.setStart(me.body.firstChild, 0).setCursor(false, true);
  14355. }
  14356. setTimeout(function() {
  14357. me.fireEvent("clearDoc");
  14358. }, 0);
  14359. }
  14360. };
  14361. // plugins/anchor.js
  14362. /**
  14363. * 锚点插件,为UEditor提供插入锚点支持
  14364. * @file
  14365. * @since 1.2.6.1
  14366. */
  14367. UE.plugin.register("anchor", function () {
  14368. var me = this;
  14369. return {
  14370. bindEvents: {
  14371. ready: function () {
  14372. utils.cssRule(
  14373. "anchor",
  14374. ".anchorclass{background: url('" +
  14375. this.options.themePath +
  14376. this.options.theme +
  14377. "/images/anchor.gif') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 16px;}",
  14378. this.document
  14379. );
  14380. }
  14381. },
  14382. outputRule: function (root) {
  14383. utils.each(root.getNodesByTagName("img"), function (a) {
  14384. var val;
  14385. if ((val = a.getAttr("anchorname"))) {
  14386. a.tagName = "a";
  14387. a.setAttr({
  14388. anchorname: "",
  14389. name: val,
  14390. class: ""
  14391. });
  14392. }
  14393. });
  14394. },
  14395. inputRule: function (root) {
  14396. utils.each(root.getNodesByTagName("a"), function (a) {
  14397. var val;
  14398. if ((val = a.getAttr("name")) && !a.getAttr("href")) {
  14399. //过滤掉word冗余标签
  14400. //_Toc\d+有可能勿命中
  14401. if (/^\_Toc\d+$/.test(val)) {
  14402. a.parentNode.removeChild(a);
  14403. return;
  14404. }
  14405. a.tagName = "img";
  14406. a.setAttr({
  14407. anchorname: a.getAttr("name"),
  14408. class: "anchorclass"
  14409. });
  14410. a.setAttr("name");
  14411. }
  14412. });
  14413. },
  14414. commands: {
  14415. /**
  14416. * 插入锚点
  14417. * @command anchor
  14418. * @method execCommand
  14419. * @param { String } cmd 命令字符串
  14420. * @param { String } name 锚点名称字符串
  14421. * @example
  14422. * ```javascript
  14423. * //editor 是编辑器实例
  14424. * editor.execCommand('anchor', 'anchor1');
  14425. * ```
  14426. */
  14427. anchor: {
  14428. execCommand: function (cmd, name) {
  14429. var range = this.selection.getRange(),
  14430. img = range.getClosedNode();
  14431. if (img && img.getAttribute("anchorname")) {
  14432. if (name) {
  14433. img.setAttribute("anchorname", name);
  14434. } else {
  14435. range.setStartBefore(img).setCursor();
  14436. domUtils.remove(img);
  14437. }
  14438. } else {
  14439. if (name) {
  14440. //只在选区的开始插入
  14441. var anchor = utils.renderTplstr('<img anchorname="${name}" class="anchorclass"></img>', {
  14442. name: name
  14443. });
  14444. me.execCommand("inserthtml", anchor, true);
  14445. }
  14446. }
  14447. }
  14448. }
  14449. }
  14450. };
  14451. });
  14452. // plugins/wordcount.js
  14453. ///import core
  14454. ///commands 字数统计
  14455. ///commandsName WordCount,wordCount
  14456. ///commandsTitle 字数统计
  14457. /*
  14458. * Created by JetBrains WebStorm.
  14459. * User: taoqili
  14460. * Date: 11-9-7
  14461. * Time: 下午8:18
  14462. * To change this template use File | Settings | File Templates.
  14463. */
  14464. UE.plugins["wordcount"] = function() {
  14465. var me = this;
  14466. me.setOpt("wordCount", true);
  14467. me.addListener("contentchange", function() {
  14468. me.fireEvent("wordcount");
  14469. });
  14470. var timer;
  14471. me.addListener("ready", function() {
  14472. var me = this;
  14473. domUtils.on(me.body, "keyup", function(evt) {
  14474. var code = evt.keyCode || evt.which,
  14475. //忽略的按键,ctr,alt,shift,方向键
  14476. ignores = {
  14477. "16": 1,
  14478. "18": 1,
  14479. "20": 1,
  14480. "37": 1,
  14481. "38": 1,
  14482. "39": 1,
  14483. "40": 1
  14484. };
  14485. if (code in ignores) return;
  14486. clearTimeout(timer);
  14487. timer = setTimeout(function() {
  14488. me.fireEvent("wordcount");
  14489. }, 200);
  14490. });
  14491. });
  14492. };
  14493. // plugins/pagebreak.js
  14494. /**
  14495. * 分页功能插件
  14496. * @file
  14497. * @since 1.2.6.1
  14498. */
  14499. UE.plugins["pagebreak"] = function() {
  14500. var me = this,
  14501. notBreakTags = ["td"];
  14502. me.setOpt("pageBreakTag", "_ueditor_page_break_tag_");
  14503. function fillNode(node) {
  14504. if (domUtils.isEmptyBlock(node)) {
  14505. var firstChild = node.firstChild,
  14506. tmpNode;
  14507. while (
  14508. firstChild &&
  14509. firstChild.nodeType == 1 &&
  14510. domUtils.isEmptyBlock(firstChild)
  14511. ) {
  14512. tmpNode = firstChild;
  14513. firstChild = firstChild.firstChild;
  14514. }
  14515. !tmpNode && (tmpNode = node);
  14516. domUtils.fillNode(me.document, tmpNode);
  14517. }
  14518. }
  14519. //分页符样式添加
  14520. me.ready(function() {
  14521. utils.cssRule(
  14522. "pagebreak",
  14523. ".pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}",
  14524. me.document
  14525. );
  14526. });
  14527. function isHr(node) {
  14528. return (
  14529. node &&
  14530. node.nodeType == 1 &&
  14531. node.tagName == "HR" &&
  14532. node.className == "pagebreak"
  14533. );
  14534. }
  14535. me.addInputRule(function(root) {
  14536. root.traversal(function(node) {
  14537. if (node.type == "text" && node.data == me.options.pageBreakTag) {
  14538. var hr = UE.uNode.createElement(
  14539. '<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">'
  14540. );
  14541. node.parentNode.insertBefore(hr, node);
  14542. node.parentNode.removeChild(node);
  14543. }
  14544. });
  14545. });
  14546. me.addOutputRule(function(node) {
  14547. utils.each(node.getNodesByTagName("hr"), function(n) {
  14548. if (n.getAttr("class") == "pagebreak") {
  14549. var txt = UE.uNode.createText(me.options.pageBreakTag);
  14550. n.parentNode.insertBefore(txt, n);
  14551. n.parentNode.removeChild(n);
  14552. }
  14553. });
  14554. });
  14555. /**
  14556. * 插入分页符
  14557. * @command pagebreak
  14558. * @method execCommand
  14559. * @param { String } cmd 命令字符串
  14560. * @remind 在表格中插入分页符会把表格切分成两部分
  14561. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  14562. * 以便于提交数据到服务器端后处理分页。
  14563. * @example
  14564. * ```javascript
  14565. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  14566. * ```
  14567. */
  14568. me.commands["pagebreak"] = {
  14569. execCommand: function() {
  14570. var range = me.selection.getRange(),
  14571. hr = me.document.createElement("hr");
  14572. domUtils.setAttributes(hr, {
  14573. class: "pagebreak",
  14574. noshade: "noshade",
  14575. size: "5"
  14576. });
  14577. domUtils.unSelectable(hr);
  14578. //table单独处理
  14579. var node = domUtils.findParentByTagName(
  14580. range.startContainer,
  14581. notBreakTags,
  14582. true
  14583. ),
  14584. parents = [],
  14585. pN;
  14586. if (node) {
  14587. switch (node.tagName) {
  14588. case "TD":
  14589. pN = node.parentNode;
  14590. if (!pN.previousSibling) {
  14591. var table = domUtils.findParentByTagName(pN, "table");
  14592. // var tableWrapDiv = table.parentNode;
  14593. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  14594. // && tableWrapDiv.tagName == 'DIV'
  14595. // && tableWrapDiv.getAttribute('dropdrag')
  14596. // ){
  14597. // domUtils.remove(tableWrapDiv,true);
  14598. // }
  14599. table.parentNode.insertBefore(hr, table);
  14600. parents = domUtils.findParents(hr, true);
  14601. } else {
  14602. pN.parentNode.insertBefore(hr, pN);
  14603. parents = domUtils.findParents(hr);
  14604. }
  14605. pN = parents[1];
  14606. if (hr !== pN) {
  14607. domUtils.breakParent(hr, pN);
  14608. }
  14609. //table要重写绑定一下拖拽
  14610. me.fireEvent("afteradjusttable", me.document);
  14611. }
  14612. } else {
  14613. if (!range.collapsed) {
  14614. range.deleteContents();
  14615. var start = range.startContainer;
  14616. while (
  14617. !domUtils.isBody(start) &&
  14618. domUtils.isBlockElm(start) &&
  14619. domUtils.isEmptyNode(start)
  14620. ) {
  14621. range.setStartBefore(start).collapse(true);
  14622. domUtils.remove(start);
  14623. start = range.startContainer;
  14624. }
  14625. }
  14626. range.insertNode(hr);
  14627. var pN = hr.parentNode,
  14628. nextNode;
  14629. while (!domUtils.isBody(pN)) {
  14630. domUtils.breakParent(hr, pN);
  14631. nextNode = hr.nextSibling;
  14632. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  14633. domUtils.remove(nextNode);
  14634. }
  14635. pN = hr.parentNode;
  14636. }
  14637. nextNode = hr.nextSibling;
  14638. var pre = hr.previousSibling;
  14639. if (isHr(pre)) {
  14640. domUtils.remove(pre);
  14641. } else {
  14642. pre && fillNode(pre);
  14643. }
  14644. if (!nextNode) {
  14645. var p = me.document.createElement("p");
  14646. hr.parentNode.appendChild(p);
  14647. domUtils.fillNode(me.document, p);
  14648. range.setStart(p, 0).collapse(true);
  14649. } else {
  14650. if (isHr(nextNode)) {
  14651. domUtils.remove(nextNode);
  14652. } else {
  14653. fillNode(nextNode);
  14654. }
  14655. range.setEndAfter(hr).collapse(false);
  14656. }
  14657. range.select(true);
  14658. }
  14659. }
  14660. };
  14661. };
  14662. // plugins/wordimage.js
  14663. ///import core
  14664. ///commands 本地图片引导上传
  14665. ///commandsName WordImage
  14666. ///commandsTitle 本地图片引导上传
  14667. ///commandsDialog dialogs\wordimage
  14668. UE.plugin.register("wordimage", function() {
  14669. var me = this,
  14670. images = [];
  14671. return {
  14672. commands: {
  14673. wordimage: {
  14674. execCommand: function() {
  14675. var images = domUtils.getElementsByTagName(me.body, "img");
  14676. var urlList = [];
  14677. for (var i = 0, ci; (ci = images[i++]); ) {
  14678. var url = ci.getAttribute("word_img");
  14679. url && urlList.push(url);
  14680. }
  14681. return urlList;
  14682. },
  14683. queryCommandState: function() {
  14684. images = domUtils.getElementsByTagName(me.body, "img");
  14685. for (var i = 0, ci; (ci = images[i++]); ) {
  14686. if (ci.getAttribute("word_img")) {
  14687. return 1;
  14688. }
  14689. }
  14690. return -1;
  14691. },
  14692. notNeedUndo: true
  14693. }
  14694. },
  14695. inputRule: function(root) {
  14696. utils.each(root.getNodesByTagName("img"), function(img) {
  14697. var attrs = img.attrs,
  14698. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  14699. opt = me.options,
  14700. src = opt.UEDITOR_HOME_URL + "themes/default/images/spacer.gif";
  14701. if (attrs["src"] && /^(?:(file:\/+))/.test(attrs["src"])) {
  14702. img.setAttr({
  14703. width: attrs.width,
  14704. height: attrs.height,
  14705. alt: attrs.alt,
  14706. word_img: attrs.src,
  14707. src: src,
  14708. style:
  14709. "background:url(" +
  14710. (flag
  14711. ? opt.themePath + opt.theme + "/images/word.gif"
  14712. : opt.langPath + opt.lang + "/images/localimage.png") +
  14713. ") no-repeat center center;border:1px solid #ddd"
  14714. });
  14715. }
  14716. });
  14717. }
  14718. };
  14719. });
  14720. // plugins/dragdrop.js
  14721. UE.plugins["dragdrop"] = function() {
  14722. var me = this;
  14723. me.ready(function() {
  14724. domUtils.on(this.body, "dragend", function() {
  14725. var rng = me.selection.getRange();
  14726. var node = rng.getClosedNode() || me.selection.getStart();
  14727. if (node && node.tagName == "IMG") {
  14728. var pre = node.previousSibling,
  14729. next;
  14730. while ((next = node.nextSibling)) {
  14731. if (
  14732. next.nodeType == 1 &&
  14733. next.tagName == "SPAN" &&
  14734. !next.firstChild
  14735. ) {
  14736. domUtils.remove(next);
  14737. } else {
  14738. break;
  14739. }
  14740. }
  14741. if (
  14742. ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre)) || !pre) &&
  14743. (!next || (next && !domUtils.isEmptyBlock(next)))
  14744. ) {
  14745. if (pre && pre.tagName == "P" && !domUtils.isEmptyBlock(pre)) {
  14746. pre.appendChild(node);
  14747. domUtils.moveChild(next, pre);
  14748. domUtils.remove(next);
  14749. } else if (
  14750. next &&
  14751. next.tagName == "P" &&
  14752. !domUtils.isEmptyBlock(next)
  14753. ) {
  14754. next.insertBefore(node, next.firstChild);
  14755. }
  14756. if (pre && pre.tagName == "P" && domUtils.isEmptyBlock(pre)) {
  14757. domUtils.remove(pre);
  14758. }
  14759. if (next && next.tagName == "P" && domUtils.isEmptyBlock(next)) {
  14760. domUtils.remove(next);
  14761. }
  14762. rng.selectNode(node).select();
  14763. me.fireEvent("saveScene");
  14764. }
  14765. }
  14766. });
  14767. });
  14768. me.addListener("keyup", function(type, evt) {
  14769. var keyCode = evt.keyCode || evt.which;
  14770. if (keyCode == 13) {
  14771. var rng = me.selection.getRange(),
  14772. node;
  14773. if (
  14774. (node = domUtils.findParentByTagName(rng.startContainer, "p", true))
  14775. ) {
  14776. if (domUtils.getComputedStyle(node, "text-align") == "center") {
  14777. domUtils.removeStyle(node, "text-align");
  14778. }
  14779. }
  14780. }
  14781. });
  14782. };
  14783. // plugins/undo.js
  14784. /**
  14785. * undo redo
  14786. * @file
  14787. * @since 1.2.6.1
  14788. */
  14789. /**
  14790. * 撤销上一次执行的命令
  14791. * @command undo
  14792. * @method execCommand
  14793. * @param { String } cmd 命令字符串
  14794. * @example
  14795. * ```javascript
  14796. * editor.execCommand( 'undo' );
  14797. * ```
  14798. */
  14799. /**
  14800. * 重做上一次执行的命令
  14801. * @command redo
  14802. * @method execCommand
  14803. * @param { String } cmd 命令字符串
  14804. * @example
  14805. * ```javascript
  14806. * editor.execCommand( 'redo' );
  14807. * ```
  14808. */
  14809. UE.plugins["undo"] = function() {
  14810. var saveSceneTimer;
  14811. var me = this,
  14812. maxUndoCount = me.options.maxUndoCount || 20,
  14813. maxInputCount = me.options.maxInputCount || 20,
  14814. fillchar = new RegExp(domUtils.fillChar + "|</hr>", "gi"); // ie会产生多余的</hr>
  14815. var noNeedFillCharTags = {
  14816. ol: 1,
  14817. ul: 1,
  14818. table: 1,
  14819. tbody: 1,
  14820. tr: 1,
  14821. body: 1
  14822. };
  14823. var orgState = me.options.autoClearEmptyNode;
  14824. function compareAddr(indexA, indexB) {
  14825. if (indexA.length != indexB.length) return 0;
  14826. for (var i = 0, l = indexA.length; i < l; i++) {
  14827. if (indexA[i] != indexB[i]) return 0;
  14828. }
  14829. return 1;
  14830. }
  14831. function compareRangeAddress(rngAddrA, rngAddrB) {
  14832. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  14833. return 0;
  14834. }
  14835. if (
  14836. !compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) ||
  14837. !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)
  14838. ) {
  14839. return 0;
  14840. }
  14841. return 1;
  14842. }
  14843. function UndoManager() {
  14844. this.list = [];
  14845. this.index = 0;
  14846. this.hasUndo = false;
  14847. this.hasRedo = false;
  14848. this.undo = function() {
  14849. if (this.hasUndo) {
  14850. if (!this.list[this.index - 1] && this.list.length == 1) {
  14851. this.reset();
  14852. return;
  14853. }
  14854. while (
  14855. this.list[this.index].content == this.list[this.index - 1].content
  14856. ) {
  14857. this.index--;
  14858. if (this.index == 0) {
  14859. return this.restore(0);
  14860. }
  14861. }
  14862. this.restore(--this.index);
  14863. }
  14864. };
  14865. this.redo = function() {
  14866. if (this.hasRedo) {
  14867. while (
  14868. this.list[this.index].content == this.list[this.index + 1].content
  14869. ) {
  14870. this.index++;
  14871. if (this.index == this.list.length - 1) {
  14872. return this.restore(this.index);
  14873. }
  14874. }
  14875. this.restore(++this.index);
  14876. }
  14877. };
  14878. this.restore = function() {
  14879. var me = this.editor;
  14880. var scene = this.list[this.index];
  14881. var root = UE.htmlparser(scene.content.replace(fillchar, ""));
  14882. me.options.autoClearEmptyNode = false;
  14883. me.filterInputRule(root);
  14884. me.options.autoClearEmptyNode = orgState;
  14885. //trace:873
  14886. //去掉展位符
  14887. me.document.body.innerHTML = root.toHtml();
  14888. me.fireEvent("afterscencerestore");
  14889. //处理undo后空格不展位的问题
  14890. if (browser.ie) {
  14891. utils.each(
  14892. domUtils.getElementsByTagName(me.document, "td th caption p"),
  14893. function(node) {
  14894. if (domUtils.isEmptyNode(node)) {
  14895. domUtils.fillNode(me.document, node);
  14896. }
  14897. }
  14898. );
  14899. }
  14900. try {
  14901. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  14902. rng.select(
  14903. noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]
  14904. );
  14905. } catch (e) {}
  14906. this.update();
  14907. this.clearKey();
  14908. //不能把自己reset了
  14909. me.fireEvent("reset", true);
  14910. };
  14911. this.getScene = function() {
  14912. var me = this.editor;
  14913. var rng = me.selection.getRange(),
  14914. rngAddress = rng.createAddress(false, true);
  14915. me.fireEvent("beforegetscene");
  14916. var root = UE.htmlparser(me.body.innerHTML);
  14917. me.options.autoClearEmptyNode = false;
  14918. me.filterOutputRule(root);
  14919. me.options.autoClearEmptyNode = orgState;
  14920. var cont = root.toHtml();
  14921. //trace:3461
  14922. //这个会引起回退时导致空格丢失的情况
  14923. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  14924. me.fireEvent("aftergetscene");
  14925. return {
  14926. address: rngAddress,
  14927. content: cont
  14928. };
  14929. };
  14930. this.save = function(notCompareRange, notSetCursor) {
  14931. clearTimeout(saveSceneTimer);
  14932. var currentScene = this.getScene(notSetCursor),
  14933. lastScene = this.list[this.index];
  14934. if (lastScene && lastScene.content != currentScene.content) {
  14935. me.trigger("contentchange");
  14936. }
  14937. //内容相同位置相同不存
  14938. if (
  14939. lastScene &&
  14940. lastScene.content == currentScene.content &&
  14941. (notCompareRange
  14942. ? 1
  14943. : compareRangeAddress(lastScene.address, currentScene.address))
  14944. ) {
  14945. return;
  14946. }
  14947. this.list = this.list.slice(0, this.index + 1);
  14948. this.list.push(currentScene);
  14949. //如果大于最大数量了,就把最前的剔除
  14950. if (this.list.length > maxUndoCount) {
  14951. this.list.shift();
  14952. }
  14953. this.index = this.list.length - 1;
  14954. this.clearKey();
  14955. //跟新undo/redo状态
  14956. this.update();
  14957. };
  14958. this.update = function() {
  14959. this.hasRedo = !!this.list[this.index + 1];
  14960. this.hasUndo = !!this.list[this.index - 1];
  14961. };
  14962. this.reset = function() {
  14963. this.list = [];
  14964. this.index = 0;
  14965. this.hasUndo = false;
  14966. this.hasRedo = false;
  14967. this.clearKey();
  14968. };
  14969. this.clearKey = function() {
  14970. keycont = 0;
  14971. lastKeyCode = null;
  14972. };
  14973. }
  14974. me.undoManger = new UndoManager();
  14975. me.undoManger.editor = me;
  14976. function saveScene() {
  14977. this.undoManger.save();
  14978. }
  14979. me.addListener("saveScene", function() {
  14980. var args = Array.prototype.splice.call(arguments, 1);
  14981. this.undoManger.save.apply(this.undoManger, args);
  14982. });
  14983. // me.addListener('beforeexeccommand', saveScene);
  14984. // me.addListener('afterexeccommand', saveScene);
  14985. me.addListener("reset", function(type, exclude) {
  14986. if (!exclude) {
  14987. this.undoManger.reset();
  14988. }
  14989. });
  14990. me.commands["redo"] = me.commands["undo"] = {
  14991. execCommand: function(cmdName) {
  14992. this.undoManger[cmdName]();
  14993. },
  14994. queryCommandState: function(cmdName) {
  14995. return this.undoManger[
  14996. "has" + (cmdName.toLowerCase() == "undo" ? "Undo" : "Redo")
  14997. ]
  14998. ? 0
  14999. : -1;
  15000. },
  15001. notNeedUndo: 1
  15002. };
  15003. var keys = {
  15004. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  15005. /*Shift*/ 16: 1,
  15006. /*Ctrl*/ 17: 1,
  15007. /*Alt*/ 18: 1,
  15008. 37: 1,
  15009. 38: 1,
  15010. 39: 1,
  15011. 40: 1
  15012. },
  15013. keycont = 0,
  15014. lastKeyCode;
  15015. //输入法状态下不计算字符数
  15016. var inputType = false;
  15017. me.addListener("ready", function() {
  15018. domUtils.on(this.body, "compositionstart", function() {
  15019. inputType = true;
  15020. });
  15021. domUtils.on(this.body, "compositionend", function() {
  15022. inputType = false;
  15023. });
  15024. });
  15025. //快捷键
  15026. me.addshortcutkey({
  15027. Undo: "ctrl+90", //undo
  15028. Redo: "ctrl+89" //redo
  15029. });
  15030. var isCollapsed = true;
  15031. me.addListener("keydown", function(type, evt) {
  15032. var me = this;
  15033. var keyCode = evt.keyCode || evt.which;
  15034. if (
  15035. !keys[keyCode] &&
  15036. !evt.ctrlKey &&
  15037. !evt.metaKey &&
  15038. !evt.shiftKey &&
  15039. !evt.altKey
  15040. ) {
  15041. if (inputType) return;
  15042. if (!me.selection.getRange().collapsed) {
  15043. me.undoManger.save(false, true);
  15044. isCollapsed = false;
  15045. return;
  15046. }
  15047. if (me.undoManger.list.length == 0) {
  15048. me.undoManger.save(true);
  15049. }
  15050. clearTimeout(saveSceneTimer);
  15051. function save(cont) {
  15052. cont.undoManger.save(false, true);
  15053. cont.fireEvent("selectionchange");
  15054. }
  15055. saveSceneTimer = setTimeout(function() {
  15056. if (inputType) {
  15057. var interalTimer = setInterval(function() {
  15058. if (!inputType) {
  15059. save(me);
  15060. clearInterval(interalTimer);
  15061. }
  15062. }, 300);
  15063. return;
  15064. }
  15065. save(me);
  15066. }, 200);
  15067. lastKeyCode = keyCode;
  15068. keycont++;
  15069. if (keycont >= maxInputCount) {
  15070. save(me);
  15071. }
  15072. }
  15073. });
  15074. me.addListener("keyup", function(type, evt) {
  15075. var keyCode = evt.keyCode || evt.which;
  15076. if (
  15077. !keys[keyCode] &&
  15078. !evt.ctrlKey &&
  15079. !evt.metaKey &&
  15080. !evt.shiftKey &&
  15081. !evt.altKey
  15082. ) {
  15083. if (inputType) return;
  15084. if (!isCollapsed) {
  15085. this.undoManger.save(false, true);
  15086. isCollapsed = true;
  15087. }
  15088. }
  15089. });
  15090. //扩展实例,添加关闭和开启命令undo
  15091. me.stopCmdUndo = function() {
  15092. me.__hasEnterExecCommand = true;
  15093. };
  15094. me.startCmdUndo = function() {
  15095. me.__hasEnterExecCommand = false;
  15096. };
  15097. };
  15098. // plugins/copy.js
  15099. UE.plugin.register("copy", function() {
  15100. var me = this;
  15101. function initZeroClipboard() {
  15102. ZeroClipboard.config({
  15103. debug: false,
  15104. swfPath:
  15105. me.options.UEDITOR_HOME_URL +
  15106. "third-party/zeroclipboard/ZeroClipboard.swf"
  15107. });
  15108. var client = (me.zeroclipboard = new ZeroClipboard());
  15109. // 复制内容
  15110. client.on("copy", function(e) {
  15111. var client = e.client,
  15112. rng = me.selection.getRange(),
  15113. div = document.createElement("div");
  15114. div.appendChild(rng.cloneContents());
  15115. client.setText(div.innerText || div.textContent);
  15116. client.setHtml(div.innerHTML);
  15117. rng.select();
  15118. });
  15119. // hover事件传递到target
  15120. client.on("mouseover mouseout", function(e) {
  15121. var target = e.target;
  15122. if (target) {
  15123. if (e.type == "mouseover") {
  15124. domUtils.addClass(target, "edui-state-hover");
  15125. } else if (e.type == "mouseout") {
  15126. domUtils.removeClasses(target, "edui-state-hover");
  15127. }
  15128. }
  15129. });
  15130. // flash加载不成功
  15131. client.on("wrongflash noflash", function() {
  15132. ZeroClipboard.destroy();
  15133. });
  15134. // 触发事件
  15135. me.fireEvent("zeroclipboardready", client);
  15136. }
  15137. return {
  15138. bindEvents: {
  15139. ready: function() {
  15140. if (!browser.ie) {
  15141. if (window.ZeroClipboard) {
  15142. initZeroClipboard();
  15143. } else {
  15144. utils.loadFile(
  15145. document,
  15146. {
  15147. src:
  15148. me.options.UEDITOR_HOME_URL +
  15149. "third-party/zeroclipboard/ZeroClipboard.min.js",
  15150. tag: "script",
  15151. type: "text/javascript",
  15152. defer: "defer"
  15153. },
  15154. function() {
  15155. initZeroClipboard();
  15156. }
  15157. );
  15158. }
  15159. }
  15160. }
  15161. },
  15162. commands: {
  15163. copy: {
  15164. execCommand: function(cmd) {
  15165. if (!me.document.execCommand("copy")) {
  15166. alert(me.getLang("copymsg"));
  15167. }
  15168. }
  15169. }
  15170. }
  15171. };
  15172. });
  15173. // plugins/paste.js
  15174. ///import core
  15175. ///import plugins/inserthtml.js
  15176. ///import plugins/undo.js
  15177. ///import plugins/serialize.js
  15178. ///commands 粘贴
  15179. ///commandsName PastePlain
  15180. ///commandsTitle 纯文本粘贴模式
  15181. /**
  15182. * @description 粘贴
  15183. * @author zhanyi
  15184. */
  15185. UE.plugins["paste"] = function() {
  15186. function getClipboardData(callback) {
  15187. var doc = this.document;
  15188. if (doc.getElementById("baidu_pastebin")) {
  15189. return;
  15190. }
  15191. var range = this.selection.getRange(),
  15192. bk = range.createBookmark(),
  15193. //创建剪贴的容器div
  15194. pastebin = doc.createElement("div");
  15195. pastebin.id = "baidu_pastebin";
  15196. // Safari 要求div必须有内容,才能粘贴内容进来
  15197. browser.webkit &&
  15198. pastebin.appendChild(
  15199. doc.createTextNode(domUtils.fillChar + domUtils.fillChar)
  15200. );
  15201. doc.body.appendChild(pastebin);
  15202. //trace:717 隐藏的span不能得到top
  15203. //bk.start.innerHTML = '&nbsp;';
  15204. bk.start.style.display = "";
  15205. pastebin.style.cssText =
  15206. "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  15207. //要在现在光标平行的位置加入,否则会出现跳动的问题
  15208. domUtils.getXY(bk.start).y +
  15209. "px";
  15210. range.selectNodeContents(pastebin).select(true);
  15211. setTimeout(function() {
  15212. if (browser.webkit) {
  15213. for (
  15214. var i = 0, pastebins = doc.querySelectorAll("#baidu_pastebin"), pi;
  15215. (pi = pastebins[i++]);
  15216. ) {
  15217. if (domUtils.isEmptyNode(pi)) {
  15218. domUtils.remove(pi);
  15219. } else {
  15220. pastebin = pi;
  15221. break;
  15222. }
  15223. }
  15224. }
  15225. try {
  15226. pastebin.parentNode.removeChild(pastebin);
  15227. } catch (e) {}
  15228. range.moveToBookmark(bk).select(true);
  15229. callback(pastebin);
  15230. }, 0);
  15231. }
  15232. var me = this;
  15233. me.setOpt({
  15234. retainOnlyLabelPasted: false
  15235. });
  15236. var txtContent, htmlContent, address;
  15237. function getPureHtml(html) {
  15238. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function(
  15239. a,
  15240. b,
  15241. tagName,
  15242. attrs
  15243. ) {
  15244. tagName = tagName.toLowerCase();
  15245. if ({ img: 1 }[tagName]) {
  15246. return a;
  15247. }
  15248. attrs = attrs.replace(
  15249. /([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi,
  15250. function(str, atr, val) {
  15251. if (
  15252. {
  15253. src: 1,
  15254. href: 1,
  15255. name: 1
  15256. }[atr.toLowerCase()]
  15257. ) {
  15258. return atr + "=" + val + " ";
  15259. }
  15260. return "";
  15261. }
  15262. );
  15263. if (
  15264. {
  15265. span: 1,
  15266. div: 1
  15267. }[tagName]
  15268. ) {
  15269. return "";
  15270. } else {
  15271. return "<" + b + tagName + " " + utils.trim(attrs) + ">";
  15272. }
  15273. });
  15274. }
  15275. function filter(div) {
  15276. var html;
  15277. if (div.firstChild) {
  15278. //去掉cut中添加的边界值
  15279. var nodes = domUtils.getElementsByTagName(div, "span");
  15280. for (var i = 0, ni; (ni = nodes[i++]); ) {
  15281. if (ni.id == "_baidu_cut_start" || ni.id == "_baidu_cut_end") {
  15282. domUtils.remove(ni);
  15283. }
  15284. }
  15285. if (browser.webkit) {
  15286. var brs = div.querySelectorAll("div br");
  15287. for (var i = 0, bi; (bi = brs[i++]); ) {
  15288. var pN = bi.parentNode;
  15289. if (pN.tagName == "DIV" && pN.childNodes.length == 1) {
  15290. pN.innerHTML = "<p><br/></p>";
  15291. domUtils.remove(pN);
  15292. }
  15293. }
  15294. var divs = div.querySelectorAll("#baidu_pastebin");
  15295. for (var i = 0, di; (di = divs[i++]); ) {
  15296. var tmpP = me.document.createElement("p");
  15297. di.parentNode.insertBefore(tmpP, di);
  15298. while (di.firstChild) {
  15299. tmpP.appendChild(di.firstChild);
  15300. }
  15301. domUtils.remove(di);
  15302. }
  15303. var metas = div.querySelectorAll("meta");
  15304. for (var i = 0, ci; (ci = metas[i++]); ) {
  15305. domUtils.remove(ci);
  15306. }
  15307. var brs = div.querySelectorAll("br");
  15308. for (i = 0; (ci = brs[i++]); ) {
  15309. if (/^apple-/i.test(ci.className)) {
  15310. domUtils.remove(ci);
  15311. }
  15312. }
  15313. }
  15314. if (browser.gecko) {
  15315. var dirtyNodes = div.querySelectorAll("[_moz_dirty]");
  15316. for (i = 0; (ci = dirtyNodes[i++]); ) {
  15317. ci.removeAttribute("_moz_dirty");
  15318. }
  15319. }
  15320. if (!browser.ie) {
  15321. var spans = div.querySelectorAll("span.Apple-style-span");
  15322. for (var i = 0, ci; (ci = spans[i++]); ) {
  15323. domUtils.remove(ci, true);
  15324. }
  15325. }
  15326. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  15327. html = div.innerHTML; //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  15328. //过滤word粘贴过来的冗余属性
  15329. html = UE.filterWord(html);
  15330. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  15331. var root = UE.htmlparser(html);
  15332. //如果给了过滤规则就先进行过滤
  15333. if (me.options.filterRules) {
  15334. UE.filterNode(root, me.options.filterRules);
  15335. }
  15336. //执行默认的处理
  15337. me.filterInputRule(root);
  15338. //针对chrome的处理
  15339. if (browser.webkit) {
  15340. var br = root.lastChild();
  15341. if (br && br.type == "element" && br.tagName == "br") {
  15342. root.removeChild(br);
  15343. }
  15344. utils.each(me.body.querySelectorAll("div"), function(node) {
  15345. if (domUtils.isEmptyBlock(node)) {
  15346. domUtils.remove(node, true);
  15347. }
  15348. });
  15349. }
  15350. html = { html: root.toHtml() };
  15351. me.fireEvent("beforepaste", html, root);
  15352. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  15353. if (!html.html) {
  15354. return;
  15355. }
  15356. root = UE.htmlparser(html.html, true);
  15357. //如果开启了纯文本模式
  15358. if (me.queryCommandState("pasteplain") === 1) {
  15359. me.execCommand(
  15360. "insertHtml",
  15361. UE.filterNode(root, me.options.filterTxtRules).toHtml(),
  15362. true
  15363. );
  15364. } else {
  15365. //文本模式
  15366. UE.filterNode(root, me.options.filterTxtRules);
  15367. txtContent = root.toHtml();
  15368. //完全模式
  15369. htmlContent = html.html;
  15370. address = me.selection.getRange().createAddress(true);
  15371. me.execCommand(
  15372. "insertHtml",
  15373. me.getOpt("retainOnlyLabelPasted") === true
  15374. ? getPureHtml(htmlContent)
  15375. : htmlContent,
  15376. true
  15377. );
  15378. }
  15379. me.fireEvent("afterpaste", html);
  15380. }
  15381. }
  15382. me.addListener("pasteTransfer", function(cmd, plainType) {
  15383. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  15384. var range = me.selection.getRange();
  15385. range.moveToAddress(address, true);
  15386. if (!range.collapsed) {
  15387. while (!domUtils.isBody(range.startContainer)) {
  15388. var start = range.startContainer;
  15389. if (start.nodeType == 1) {
  15390. start = start.childNodes[range.startOffset];
  15391. if (!start) {
  15392. range.setStartBefore(range.startContainer);
  15393. continue;
  15394. }
  15395. var pre = start.previousSibling;
  15396. if (
  15397. pre &&
  15398. pre.nodeType == 3 &&
  15399. new RegExp("^[\n\r\t " + domUtils.fillChar + "]*$").test(
  15400. pre.nodeValue
  15401. )
  15402. ) {
  15403. range.setStartBefore(pre);
  15404. }
  15405. }
  15406. if (range.startOffset == 0) {
  15407. range.setStartBefore(range.startContainer);
  15408. } else {
  15409. break;
  15410. }
  15411. }
  15412. while (!domUtils.isBody(range.endContainer)) {
  15413. var end = range.endContainer;
  15414. if (end.nodeType == 1) {
  15415. end = end.childNodes[range.endOffset];
  15416. if (!end) {
  15417. range.setEndAfter(range.endContainer);
  15418. continue;
  15419. }
  15420. var next = end.nextSibling;
  15421. if (
  15422. next &&
  15423. next.nodeType == 3 &&
  15424. new RegExp("^[\n\r\t" + domUtils.fillChar + "]*$").test(
  15425. next.nodeValue
  15426. )
  15427. ) {
  15428. range.setEndAfter(next);
  15429. }
  15430. }
  15431. if (
  15432. range.endOffset ==
  15433. range.endContainer[
  15434. range.endContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  15435. ].length
  15436. ) {
  15437. range.setEndAfter(range.endContainer);
  15438. } else {
  15439. break;
  15440. }
  15441. }
  15442. }
  15443. range.deleteContents();
  15444. range.select(true);
  15445. me.__hasEnterExecCommand = true;
  15446. var html = htmlContent;
  15447. if (plainType === 2) {
  15448. html = getPureHtml(html);
  15449. } else if (plainType) {
  15450. html = txtContent;
  15451. }
  15452. me.execCommand("inserthtml", html, true);
  15453. me.__hasEnterExecCommand = false;
  15454. var rng = me.selection.getRange();
  15455. while (
  15456. !domUtils.isBody(rng.startContainer) &&
  15457. !rng.startOffset &&
  15458. rng.startContainer[
  15459. rng.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  15460. ].length
  15461. ) {
  15462. rng.setStartBefore(rng.startContainer);
  15463. }
  15464. var tmpAddress = rng.createAddress(true);
  15465. address.endAddress = tmpAddress.startAddress;
  15466. }
  15467. });
  15468. me.addListener("ready", function() {
  15469. domUtils.on(me.body, "cut", function() {
  15470. var range = me.selection.getRange();
  15471. if (!range.collapsed && me.undoManger) {
  15472. if (me.undoManger.list.length < 1) me.undoManger.save();
  15473. setTimeout(function() {
  15474. me.undoManger.save();
  15475. });
  15476. }
  15477. });
  15478. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  15479. domUtils.on(
  15480. me.body,
  15481. browser.ie || browser.opera ? "keydown" : "paste",
  15482. function(e) {
  15483. if (
  15484. (browser.ie || browser.opera) &&
  15485. ((!e.ctrlKey && !e.metaKey) || e.keyCode != "86")
  15486. ) {
  15487. return;
  15488. }
  15489. getClipboardData.call(me, function(div) {
  15490. filter(div);
  15491. });
  15492. }
  15493. );
  15494. });
  15495. me.commands["paste"] = {
  15496. execCommand: function(cmd) {
  15497. if (browser.ie) {
  15498. getClipboardData.call(me, function(div) {
  15499. filter(div);
  15500. });
  15501. me.document.execCommand("paste");
  15502. } else {
  15503. alert(me.getLang("pastemsg"));
  15504. }
  15505. }
  15506. };
  15507. };
  15508. // plugins/puretxtpaste.js
  15509. /**
  15510. * 纯文本粘贴插件
  15511. * @file
  15512. * @since 1.2.6.1
  15513. */
  15514. UE.plugins["pasteplain"] = function() {
  15515. var me = this;
  15516. me.setOpt({
  15517. pasteplain: false,
  15518. filterTxtRules: (function() {
  15519. function transP(node) {
  15520. node.tagName = "p";
  15521. node.setStyle();
  15522. }
  15523. function removeNode(node) {
  15524. node.parentNode.removeChild(node, true);
  15525. }
  15526. return {
  15527. //直接删除及其字节点内容
  15528. "-": "script style object iframe embed input select",
  15529. p: { $: {} },
  15530. br: { $: {} },
  15531. div: function(node) {
  15532. var tmpNode,
  15533. p = UE.uNode.createElement("p");
  15534. while ((tmpNode = node.firstChild())) {
  15535. if (tmpNode.type == "text" || !UE.dom.dtd.$block[tmpNode.tagName]) {
  15536. p.appendChild(tmpNode);
  15537. } else {
  15538. if (p.firstChild()) {
  15539. node.parentNode.insertBefore(p, node);
  15540. p = UE.uNode.createElement("p");
  15541. } else {
  15542. node.parentNode.insertBefore(tmpNode, node);
  15543. }
  15544. }
  15545. }
  15546. if (p.firstChild()) {
  15547. node.parentNode.insertBefore(p, node);
  15548. }
  15549. node.parentNode.removeChild(node);
  15550. },
  15551. ol: removeNode,
  15552. ul: removeNode,
  15553. dl: removeNode,
  15554. dt: removeNode,
  15555. dd: removeNode,
  15556. li: removeNode,
  15557. caption: transP,
  15558. th: transP,
  15559. tr: transP,
  15560. h1: transP,
  15561. h2: transP,
  15562. h3: transP,
  15563. h4: transP,
  15564. h5: transP,
  15565. h6: transP,
  15566. td: function(node) {
  15567. //没有内容的td直接删掉
  15568. var txt = !!node.innerText();
  15569. if (txt) {
  15570. node.parentNode.insertAfter(
  15571. UE.uNode.createText(" &nbsp; &nbsp;"),
  15572. node
  15573. );
  15574. }
  15575. node.parentNode.removeChild(node, node.innerText());
  15576. }
  15577. };
  15578. })()
  15579. });
  15580. //暂时这里支持一下老版本的属性
  15581. var pasteplain = me.options.pasteplain;
  15582. /**
  15583. * 启用或取消纯文本粘贴模式
  15584. * @command pasteplain
  15585. * @method execCommand
  15586. * @param { String } cmd 命令字符串
  15587. * @example
  15588. * ```javascript
  15589. * editor.queryCommandState( 'pasteplain' );
  15590. * ```
  15591. */
  15592. /**
  15593. * 查询当前是否处于纯文本粘贴模式
  15594. * @command pasteplain
  15595. * @method queryCommandState
  15596. * @param { String } cmd 命令字符串
  15597. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  15598. * @example
  15599. * ```javascript
  15600. * editor.queryCommandState( 'pasteplain' );
  15601. * ```
  15602. */
  15603. me.commands["pasteplain"] = {
  15604. queryCommandState: function() {
  15605. return pasteplain ? 1 : 0;
  15606. },
  15607. execCommand: function() {
  15608. pasteplain = !pasteplain | 0;
  15609. },
  15610. notNeedUndo: 1
  15611. };
  15612. };
  15613. // plugins/list.js
  15614. /**
  15615. * 有序列表,无序列表插件
  15616. * @file
  15617. * @since 1.2.6.1
  15618. */
  15619. UE.plugins["list"] = function() {
  15620. var me = this,
  15621. notExchange = {
  15622. TD: 1,
  15623. PRE: 1,
  15624. BLOCKQUOTE: 1
  15625. };
  15626. var customStyle = {
  15627. cn: "cn-1-",
  15628. cn1: "cn-2-",
  15629. cn2: "cn-3-",
  15630. num: "num-1-",
  15631. num1: "num-2-",
  15632. num2: "num-3-",
  15633. dash: "dash",
  15634. dot: "dot"
  15635. };
  15636. me.setOpt({
  15637. autoTransWordToList: false,
  15638. insertorderedlist: {
  15639. num: "",
  15640. num1: "",
  15641. num2: "",
  15642. cn: "",
  15643. cn1: "",
  15644. cn2: "",
  15645. decimal: "",
  15646. "lower-alpha": "",
  15647. "lower-roman": "",
  15648. "upper-alpha": "",
  15649. "upper-roman": ""
  15650. },
  15651. insertunorderedlist: {
  15652. circle: "",
  15653. disc: "",
  15654. square: "",
  15655. dash: "",
  15656. dot: ""
  15657. },
  15658. listDefaultPaddingLeft: "30",
  15659. listiconpath: me.options.UEDITOR_HOME_URL + "themes/ueditor-list/",
  15660. maxListLevel: -1, //-1不限制
  15661. disablePInList: false
  15662. });
  15663. function listToArray(list) {
  15664. var arr = [];
  15665. for (var p in list) {
  15666. arr.push(p);
  15667. }
  15668. return arr;
  15669. }
  15670. var listStyle = {
  15671. OL: listToArray(me.options.insertorderedlist),
  15672. UL: listToArray(me.options.insertunorderedlist)
  15673. };
  15674. var liiconpath = me.options.listiconpath;
  15675. //根据用户配置,调整customStyle
  15676. for (var s in customStyle) {
  15677. if (
  15678. !me.options.insertorderedlist.hasOwnProperty(s) &&
  15679. !me.options.insertunorderedlist.hasOwnProperty(s)
  15680. ) {
  15681. delete customStyle[s];
  15682. }
  15683. }
  15684. me.ready(function() {
  15685. var customCss = [];
  15686. for (var p in customStyle) {
  15687. if (p == "dash" || p == "dot") {
  15688. customCss.push(
  15689. "li.list-" +
  15690. customStyle[p] +
  15691. "{background-image:url(" +
  15692. liiconpath +
  15693. customStyle[p] +
  15694. ".gif)}"
  15695. );
  15696. customCss.push(
  15697. "ul.custom_" +
  15698. p +
  15699. "{list-style:none;}ul.custom_" +
  15700. p +
  15701. " li{background-position:0 3px;background-repeat:no-repeat}"
  15702. );
  15703. } else {
  15704. for (var i = 0; i < 99; i++) {
  15705. customCss.push(
  15706. "li.list-" +
  15707. customStyle[p] +
  15708. i +
  15709. "{background-image:url(" +
  15710. liiconpath +
  15711. "list-" +
  15712. customStyle[p] +
  15713. i +
  15714. ".gif)}"
  15715. );
  15716. }
  15717. customCss.push(
  15718. "ol.custom_" +
  15719. p +
  15720. "{list-style:none;}ol.custom_" +
  15721. p +
  15722. " li{background-position:0 3px;background-repeat:no-repeat}"
  15723. );
  15724. }
  15725. switch (p) {
  15726. case "cn":
  15727. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:25px}");
  15728. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15729. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:55px}");
  15730. break;
  15731. case "cn1":
  15732. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:30px}");
  15733. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15734. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:55px}");
  15735. break;
  15736. case "cn2":
  15737. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:40px}");
  15738. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:55px}");
  15739. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:68px}");
  15740. break;
  15741. case "num":
  15742. case "num1":
  15743. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:25px}");
  15744. break;
  15745. case "num2":
  15746. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:35px}");
  15747. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15748. break;
  15749. case "dash":
  15750. customCss.push("li.list-" + p + "-paddingleft{padding-left:35px}");
  15751. break;
  15752. case "dot":
  15753. customCss.push("li.list-" + p + "-paddingleft{padding-left:20px}");
  15754. }
  15755. }
  15756. customCss.push(".list-paddingleft-1{padding-left:0}");
  15757. customCss.push(
  15758. ".list-paddingleft-2{padding-left:" +
  15759. me.options.listDefaultPaddingLeft +
  15760. "px}"
  15761. );
  15762. customCss.push(
  15763. ".list-paddingleft-3{padding-left:" +
  15764. me.options.listDefaultPaddingLeft * 2 +
  15765. "px}"
  15766. );
  15767. //如果不给宽度会在自定应样式里出现滚动条
  15768. utils.cssRule(
  15769. "list",
  15770. "ol,ul{margin:0;pading:0;" +
  15771. (browser.ie ? "" : "width:95%") +
  15772. "}li{clear:both;}" +
  15773. customCss.join("\n"),
  15774. me.document
  15775. );
  15776. });
  15777. //单独处理剪切的问题
  15778. me.ready(function() {
  15779. domUtils.on(me.body, "cut", function() {
  15780. setTimeout(function() {
  15781. var rng = me.selection.getRange(),
  15782. li;
  15783. //trace:3416
  15784. if (!rng.collapsed) {
  15785. if (
  15786. (li = domUtils.findParentByTagName(rng.startContainer, "li", true))
  15787. ) {
  15788. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  15789. var pn = li.parentNode,
  15790. node;
  15791. if ((node = pn.previousSibling)) {
  15792. domUtils.remove(pn);
  15793. rng.setStartAtLast(node).collapse(true);
  15794. rng.select(true);
  15795. } else if ((node = pn.nextSibling)) {
  15796. domUtils.remove(pn);
  15797. rng.setStartAtFirst(node).collapse(true);
  15798. rng.select(true);
  15799. } else {
  15800. var tmpNode = me.document.createElement("p");
  15801. domUtils.fillNode(me.document, tmpNode);
  15802. pn.parentNode.insertBefore(tmpNode, pn);
  15803. domUtils.remove(pn);
  15804. rng.setStart(tmpNode, 0).collapse(true);
  15805. rng.select(true);
  15806. }
  15807. }
  15808. }
  15809. }
  15810. });
  15811. });
  15812. });
  15813. function getStyle(node) {
  15814. var cls = node.className;
  15815. if (domUtils.hasClass(node, /custom_/)) {
  15816. return cls.match(/custom_(\w+)/)[1];
  15817. }
  15818. return domUtils.getStyle(node, "list-style-type");
  15819. }
  15820. me.addListener("beforepaste", function(type, html) {
  15821. var me = this,
  15822. rng = me.selection.getRange(),
  15823. li;
  15824. var root = UE.htmlparser(html.html, true);
  15825. if ((li = domUtils.findParentByTagName(rng.startContainer, "li", true))) {
  15826. var list = li.parentNode,
  15827. tagName = list.tagName == "OL" ? "ul" : "ol";
  15828. utils.each(root.getNodesByTagName(tagName), function(n) {
  15829. n.tagName = list.tagName;
  15830. n.setAttr();
  15831. if (n.parentNode === root) {
  15832. type = getStyle(list) || (list.tagName == "OL" ? "decimal" : "disc");
  15833. } else {
  15834. var className = n.parentNode.getAttr("class");
  15835. if (className && /custom_/.test(className)) {
  15836. type = className.match(/custom_(\w+)/)[1];
  15837. } else {
  15838. type = n.parentNode.getStyle("list-style-type");
  15839. }
  15840. if (!type) {
  15841. type = list.tagName == "OL" ? "decimal" : "disc";
  15842. }
  15843. }
  15844. var index = utils.indexOf(listStyle[list.tagName], type);
  15845. if (n.parentNode !== root)
  15846. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  15847. var currentStyle = listStyle[list.tagName][index];
  15848. if (customStyle[currentStyle]) {
  15849. n.setAttr("class", "custom_" + currentStyle);
  15850. } else {
  15851. n.setStyle("list-style-type", currentStyle);
  15852. }
  15853. });
  15854. }
  15855. html.html = root.toHtml();
  15856. });
  15857. //导出时,去掉p标签
  15858. me.getOpt("disablePInList") === true &&
  15859. me.addOutputRule(function(root) {
  15860. utils.each(root.getNodesByTagName("li"), function(li) {
  15861. var newChildrens = [],
  15862. index = 0;
  15863. utils.each(li.children, function(n) {
  15864. if (n.tagName == "p") {
  15865. var tmpNode;
  15866. while ((tmpNode = n.children.pop())) {
  15867. newChildrens.splice(index, 0, tmpNode);
  15868. tmpNode.parentNode = li;
  15869. lastNode = tmpNode;
  15870. }
  15871. tmpNode = newChildrens[newChildrens.length - 1];
  15872. if (
  15873. !tmpNode ||
  15874. tmpNode.type != "element" ||
  15875. tmpNode.tagName != "br"
  15876. ) {
  15877. var br = UE.uNode.createElement("br");
  15878. br.parentNode = li;
  15879. newChildrens.push(br);
  15880. }
  15881. index = newChildrens.length;
  15882. }
  15883. });
  15884. if (newChildrens.length) {
  15885. li.children = newChildrens;
  15886. }
  15887. });
  15888. });
  15889. //进入编辑器的li要套p标签
  15890. me.addInputRule(function(root) {
  15891. utils.each(root.getNodesByTagName("li"), function(li) {
  15892. var tmpP = UE.uNode.createElement("p");
  15893. for (var i = 0, ci; (ci = li.children[i]); ) {
  15894. if (ci.type == "text" || dtd.p[ci.tagName]) {
  15895. tmpP.appendChild(ci);
  15896. } else {
  15897. if (tmpP.firstChild()) {
  15898. li.insertBefore(tmpP, ci);
  15899. tmpP = UE.uNode.createElement("p");
  15900. i = i + 2;
  15901. } else {
  15902. i++;
  15903. }
  15904. }
  15905. }
  15906. if ((tmpP.firstChild() && !tmpP.parentNode) || !li.firstChild()) {
  15907. li.appendChild(tmpP);
  15908. }
  15909. //trace:3357
  15910. //p不能为空
  15911. if (!tmpP.firstChild()) {
  15912. tmpP.innerHTML(browser.ie ? "&nbsp;" : "<br/>");
  15913. }
  15914. //去掉末尾的空白
  15915. var p = li.firstChild();
  15916. var lastChild = p.lastChild();
  15917. if (
  15918. lastChild &&
  15919. lastChild.type == "text" &&
  15920. /^\s*$/.test(lastChild.data)
  15921. ) {
  15922. p.removeChild(lastChild);
  15923. }
  15924. });
  15925. if (me.options.autoTransWordToList) {
  15926. var orderlisttype = {
  15927. num1: /^\d+\)/,
  15928. decimal: /^\d+\./,
  15929. "lower-alpha": /^[a-z]+\)/,
  15930. "upper-alpha": /^[A-Z]+\./,
  15931. cn: /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  15932. cn2: /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  15933. },
  15934. unorderlisttype = {
  15935. square: "n"
  15936. };
  15937. function checkListType(content, container) {
  15938. var span = container.firstChild();
  15939. if (
  15940. span &&
  15941. span.type == "element" &&
  15942. span.tagName == "span" &&
  15943. /Wingdings|Symbol/.test(span.getStyle("font-family"))
  15944. ) {
  15945. for (var p in unorderlisttype) {
  15946. if (unorderlisttype[p] == span.data) {
  15947. return p;
  15948. }
  15949. }
  15950. return "disc";
  15951. }
  15952. for (var p in orderlisttype) {
  15953. if (orderlisttype[p].test(content)) {
  15954. return p;
  15955. }
  15956. }
  15957. }
  15958. utils.each(root.getNodesByTagName("p"), function(node) {
  15959. if (node.getAttr("class") != "MsoListParagraph") {
  15960. return;
  15961. }
  15962. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  15963. node.setStyle("margin", "");
  15964. node.setStyle("margin-left", "");
  15965. node.setAttr("class", "");
  15966. function appendLi(list, p, type) {
  15967. if (list.tagName == "ol") {
  15968. if (browser.ie) {
  15969. var first = p.firstChild();
  15970. if (
  15971. first.type == "element" &&
  15972. first.tagName == "span" &&
  15973. orderlisttype[type].test(first.innerText())
  15974. ) {
  15975. p.removeChild(first);
  15976. }
  15977. } else {
  15978. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ""));
  15979. }
  15980. } else {
  15981. p.removeChild(p.firstChild());
  15982. }
  15983. var li = UE.uNode.createElement("li");
  15984. li.appendChild(p);
  15985. list.appendChild(li);
  15986. }
  15987. var tmp = node,
  15988. type,
  15989. cacheNode = node;
  15990. if (
  15991. node.parentNode.tagName != "li" &&
  15992. (type = checkListType(node.innerText(), node))
  15993. ) {
  15994. var list = UE.uNode.createElement(
  15995. me.options.insertorderedlist.hasOwnProperty(type) ? "ol" : "ul"
  15996. );
  15997. if (customStyle[type]) {
  15998. list.setAttr("class", "custom_" + type);
  15999. } else {
  16000. list.setStyle("list-style-type", type);
  16001. }
  16002. while (
  16003. node &&
  16004. node.parentNode.tagName != "li" &&
  16005. checkListType(node.innerText(), node)
  16006. ) {
  16007. tmp = node.nextSibling();
  16008. if (!tmp) {
  16009. node.parentNode.insertBefore(list, node);
  16010. }
  16011. appendLi(list, node, type);
  16012. node = tmp;
  16013. }
  16014. if (!list.parentNode && node && node.parentNode) {
  16015. node.parentNode.insertBefore(list, node);
  16016. }
  16017. }
  16018. var span = cacheNode.firstChild();
  16019. if (
  16020. span &&
  16021. span.type == "element" &&
  16022. span.tagName == "span" &&
  16023. /^\s*(&nbsp;)+\s*$/.test(span.innerText())
  16024. ) {
  16025. span.parentNode.removeChild(span);
  16026. }
  16027. });
  16028. }
  16029. });
  16030. //调整索引标签
  16031. me.addListener("contentchange", function() {
  16032. adjustListStyle(me.document);
  16033. });
  16034. function adjustListStyle(doc, ignore) {
  16035. utils.each(domUtils.getElementsByTagName(doc, "ol ul"), function(node) {
  16036. if (!domUtils.inDoc(node, doc)) return;
  16037. var parent = node.parentNode;
  16038. if (parent.tagName == node.tagName) {
  16039. var nodeStyleType =
  16040. getStyle(node) || (node.tagName == "OL" ? "decimal" : "disc"),
  16041. parentStyleType =
  16042. getStyle(parent) || (parent.tagName == "OL" ? "decimal" : "disc");
  16043. if (nodeStyleType == parentStyleType) {
  16044. var styleIndex = utils.indexOf(
  16045. listStyle[node.tagName],
  16046. nodeStyleType
  16047. );
  16048. styleIndex = styleIndex + 1 == listStyle[node.tagName].length
  16049. ? 0
  16050. : styleIndex + 1;
  16051. setListStyle(node, listStyle[node.tagName][styleIndex]);
  16052. }
  16053. }
  16054. var index = 0,
  16055. type = 2;
  16056. if (domUtils.hasClass(node, /custom_/)) {
  16057. if (
  16058. !(
  16059. /[ou]l/i.test(parent.tagName) &&
  16060. domUtils.hasClass(parent, /custom_/)
  16061. )
  16062. ) {
  16063. type = 1;
  16064. }
  16065. } else {
  16066. if (
  16067. /[ou]l/i.test(parent.tagName) &&
  16068. domUtils.hasClass(parent, /custom_/)
  16069. ) {
  16070. type = 3;
  16071. }
  16072. }
  16073. var style = domUtils.getStyle(node, "list-style-type");
  16074. style && (node.style.cssText = "list-style-type:" + style);
  16075. node.className =
  16076. utils.trim(node.className.replace(/list-paddingleft-\w+/, "")) +
  16077. " list-paddingleft-" +
  16078. type;
  16079. utils.each(domUtils.getElementsByTagName(node, "li"), function(li) {
  16080. li.style.cssText && (li.style.cssText = "");
  16081. if (!li.firstChild) {
  16082. domUtils.remove(li);
  16083. return;
  16084. }
  16085. if (li.parentNode !== node) {
  16086. return;
  16087. }
  16088. index++;
  16089. if (domUtils.hasClass(node, /custom_/)) {
  16090. var paddingLeft = 1,
  16091. currentStyle = getStyle(node);
  16092. if (node.tagName == "OL") {
  16093. if (currentStyle) {
  16094. switch (currentStyle) {
  16095. case "cn":
  16096. case "cn1":
  16097. case "cn2":
  16098. if (
  16099. index > 10 &&
  16100. (index % 10 == 0 || (index > 10 && index < 20))
  16101. ) {
  16102. paddingLeft = 2;
  16103. } else if (index > 20) {
  16104. paddingLeft = 3;
  16105. }
  16106. break;
  16107. case "num2":
  16108. if (index > 9) {
  16109. paddingLeft = 2;
  16110. }
  16111. }
  16112. }
  16113. li.className =
  16114. "list-" +
  16115. customStyle[currentStyle] +
  16116. index +
  16117. " " +
  16118. "list-" +
  16119. currentStyle +
  16120. "-paddingleft-" +
  16121. paddingLeft;
  16122. } else {
  16123. li.className =
  16124. "list-" +
  16125. customStyle[currentStyle] +
  16126. " " +
  16127. "list-" +
  16128. currentStyle +
  16129. "-paddingleft";
  16130. }
  16131. } else {
  16132. li.className = li.className.replace(/list-[\w\-]+/gi, "");
  16133. }
  16134. var className = li.getAttribute("class");
  16135. if (className !== null && !className.replace(/\s/g, "")) {
  16136. domUtils.removeAttributes(li, "class");
  16137. }
  16138. });
  16139. !ignore &&
  16140. adjustList(
  16141. node,
  16142. node.tagName.toLowerCase(),
  16143. getStyle(node) || domUtils.getStyle(node, "list-style-type"),
  16144. true
  16145. );
  16146. });
  16147. }
  16148. function adjustList(list, tag, style, ignoreEmpty) {
  16149. var nextList = list.nextSibling;
  16150. if (
  16151. nextList &&
  16152. nextList.nodeType == 1 &&
  16153. nextList.tagName.toLowerCase() == tag &&
  16154. (getStyle(nextList) ||
  16155. domUtils.getStyle(nextList, "list-style-type") ||
  16156. (tag == "ol" ? "decimal" : "disc")) == style
  16157. ) {
  16158. domUtils.moveChild(nextList, list);
  16159. if (nextList.childNodes.length == 0) {
  16160. domUtils.remove(nextList);
  16161. }
  16162. }
  16163. if (nextList && domUtils.isFillChar(nextList)) {
  16164. domUtils.remove(nextList);
  16165. }
  16166. var preList = list.previousSibling;
  16167. if (
  16168. preList &&
  16169. preList.nodeType == 1 &&
  16170. preList.tagName.toLowerCase() == tag &&
  16171. (getStyle(preList) ||
  16172. domUtils.getStyle(preList, "list-style-type") ||
  16173. (tag == "ol" ? "decimal" : "disc")) == style
  16174. ) {
  16175. domUtils.moveChild(list, preList);
  16176. }
  16177. if (preList && domUtils.isFillChar(preList)) {
  16178. domUtils.remove(preList);
  16179. }
  16180. !ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list);
  16181. if (getStyle(list)) {
  16182. adjustListStyle(list.ownerDocument, true);
  16183. }
  16184. }
  16185. function setListStyle(list, style) {
  16186. if (customStyle[style]) {
  16187. list.className = "custom_" + style;
  16188. }
  16189. try {
  16190. domUtils.setStyle(list, "list-style-type", style);
  16191. } catch (e) {}
  16192. }
  16193. function clearEmptySibling(node) {
  16194. var tmpNode = node.previousSibling;
  16195. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16196. domUtils.remove(tmpNode);
  16197. }
  16198. tmpNode = node.nextSibling;
  16199. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16200. domUtils.remove(tmpNode);
  16201. }
  16202. }
  16203. me.addListener("keydown", function(type, evt) {
  16204. function preventAndSave() {
  16205. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  16206. me.fireEvent("contentchange");
  16207. me.undoManger && me.undoManger.save();
  16208. }
  16209. function findList(node, filterFn) {
  16210. while (node && !domUtils.isBody(node)) {
  16211. if (filterFn(node)) {
  16212. return null;
  16213. }
  16214. if (node.nodeType == 1 && /[ou]l/i.test(node.tagName)) {
  16215. return node;
  16216. }
  16217. node = node.parentNode;
  16218. }
  16219. return null;
  16220. }
  16221. var keyCode = evt.keyCode || evt.which;
  16222. if (keyCode == 13 && !evt.shiftKey) {
  16223. //回车
  16224. var rng = me.selection.getRange(),
  16225. parent = domUtils.findParent(
  16226. rng.startContainer,
  16227. function(node) {
  16228. return domUtils.isBlockElm(node);
  16229. },
  16230. true
  16231. ),
  16232. li = domUtils.findParentByTagName(rng.startContainer, "li", true);
  16233. if (parent && parent.tagName != "PRE" && !li) {
  16234. var html = parent.innerHTML.replace(
  16235. new RegExp(domUtils.fillChar, "g"),
  16236. ""
  16237. );
  16238. if (/^\s*1\s*\.[^\d]/.test(html)) {
  16239. parent.innerHTML = html.replace(/^\s*1\s*\./, "");
  16240. rng.setStartAtLast(parent).collapse(true).select();
  16241. me.__hasEnterExecCommand = true;
  16242. me.execCommand("insertorderedlist");
  16243. me.__hasEnterExecCommand = false;
  16244. }
  16245. }
  16246. var range = me.selection.getRange(),
  16247. start = findList(range.startContainer, function(node) {
  16248. return node.tagName == "TABLE";
  16249. }),
  16250. end = range.collapsed
  16251. ? start
  16252. : findList(range.endContainer, function(node) {
  16253. return node.tagName == "TABLE";
  16254. });
  16255. if (start && end && start === end) {
  16256. if (!range.collapsed) {
  16257. start = domUtils.findParentByTagName(
  16258. range.startContainer,
  16259. "li",
  16260. true
  16261. );
  16262. end = domUtils.findParentByTagName(range.endContainer, "li", true);
  16263. if (start && end && start === end) {
  16264. range.deleteContents();
  16265. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16266. if (li && domUtils.isEmptyBlock(li)) {
  16267. pre = li.previousSibling;
  16268. next = li.nextSibling;
  16269. p = me.document.createElement("p");
  16270. domUtils.fillNode(me.document, p);
  16271. parentList = li.parentNode;
  16272. if (pre && next) {
  16273. range.setStart(next, 0).collapse(true).select(true);
  16274. domUtils.remove(li);
  16275. } else {
  16276. if ((!pre && !next) || !pre) {
  16277. parentList.parentNode.insertBefore(p, parentList);
  16278. } else {
  16279. li.parentNode.parentNode.insertBefore(
  16280. p,
  16281. parentList.nextSibling
  16282. );
  16283. }
  16284. domUtils.remove(li);
  16285. if (!parentList.firstChild) {
  16286. domUtils.remove(parentList);
  16287. }
  16288. range.setStart(p, 0).setCursor();
  16289. }
  16290. preventAndSave();
  16291. return;
  16292. }
  16293. } else {
  16294. var tmpRange = range.cloneRange(),
  16295. bk = tmpRange.collapse(false).createBookmark();
  16296. range.deleteContents();
  16297. tmpRange.moveToBookmark(bk);
  16298. var li = domUtils.findParentByTagName(
  16299. tmpRange.startContainer,
  16300. "li",
  16301. true
  16302. );
  16303. clearEmptySibling(li);
  16304. tmpRange.select();
  16305. preventAndSave();
  16306. return;
  16307. }
  16308. }
  16309. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16310. if (li) {
  16311. if (domUtils.isEmptyBlock(li)) {
  16312. bk = range.createBookmark();
  16313. var parentList = li.parentNode;
  16314. if (li !== parentList.lastChild) {
  16315. domUtils.breakParent(li, parentList);
  16316. clearEmptySibling(li);
  16317. } else {
  16318. parentList.parentNode.insertBefore(li, parentList.nextSibling);
  16319. if (domUtils.isEmptyNode(parentList)) {
  16320. domUtils.remove(parentList);
  16321. }
  16322. }
  16323. //嵌套不处理
  16324. if (!dtd.$list[li.parentNode.tagName]) {
  16325. if (!domUtils.isBlockElm(li.firstChild)) {
  16326. p = me.document.createElement("p");
  16327. li.parentNode.insertBefore(p, li);
  16328. while (li.firstChild) {
  16329. p.appendChild(li.firstChild);
  16330. }
  16331. domUtils.remove(li);
  16332. } else {
  16333. domUtils.remove(li, true);
  16334. }
  16335. }
  16336. range.moveToBookmark(bk).select();
  16337. } else {
  16338. var first = li.firstChild;
  16339. if (!first || !domUtils.isBlockElm(first)) {
  16340. var p = me.document.createElement("p");
  16341. !li.firstChild && domUtils.fillNode(me.document, p);
  16342. while (li.firstChild) {
  16343. p.appendChild(li.firstChild);
  16344. }
  16345. li.appendChild(p);
  16346. first = p;
  16347. }
  16348. var span = me.document.createElement("span");
  16349. range.insertNode(span);
  16350. domUtils.breakParent(span, li);
  16351. var nextLi = span.nextSibling;
  16352. first = nextLi.firstChild;
  16353. if (!first) {
  16354. p = me.document.createElement("p");
  16355. domUtils.fillNode(me.document, p);
  16356. nextLi.appendChild(p);
  16357. first = p;
  16358. }
  16359. if (domUtils.isEmptyNode(first)) {
  16360. first.innerHTML = "";
  16361. domUtils.fillNode(me.document, first);
  16362. }
  16363. range.setStart(first, 0).collapse(true).shrinkBoundary().select();
  16364. domUtils.remove(span);
  16365. var pre = nextLi.previousSibling;
  16366. if (pre && domUtils.isEmptyBlock(pre)) {
  16367. pre.innerHTML = "<p></p>";
  16368. domUtils.fillNode(me.document, pre.firstChild);
  16369. }
  16370. }
  16371. // }
  16372. preventAndSave();
  16373. }
  16374. }
  16375. }
  16376. if (keyCode == 8) {
  16377. //修中ie中li下的问题
  16378. range = me.selection.getRange();
  16379. if (range.collapsed && domUtils.isStartInblock(range)) {
  16380. tmpRange = range.cloneRange().trimBoundary();
  16381. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16382. //要在li的最左边,才能处理
  16383. if (li && domUtils.isStartInblock(tmpRange)) {
  16384. start = domUtils.findParentByTagName(range.startContainer, "p", true);
  16385. if (start && start !== li.firstChild) {
  16386. var parentList = domUtils.findParentByTagName(start, ["ol", "ul"]);
  16387. domUtils.breakParent(start, parentList);
  16388. clearEmptySibling(start);
  16389. me.fireEvent("contentchange");
  16390. range.setStart(start, 0).setCursor(false, true);
  16391. me.fireEvent("saveScene");
  16392. domUtils.preventDefault(evt);
  16393. return;
  16394. }
  16395. if (li && (pre = li.previousSibling)) {
  16396. if (keyCode == 46 && li.childNodes.length) {
  16397. return;
  16398. }
  16399. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  16400. if (dtd.$list[pre.tagName]) {
  16401. pre = pre.lastChild;
  16402. }
  16403. me.undoManger && me.undoManger.save();
  16404. first = li.firstChild;
  16405. if (domUtils.isBlockElm(first)) {
  16406. if (domUtils.isEmptyNode(first)) {
  16407. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16408. pre.appendChild(first);
  16409. range.setStart(first, 0).setCursor(false, true);
  16410. //first不是唯一的节点
  16411. while (li.firstChild) {
  16412. pre.appendChild(li.firstChild);
  16413. }
  16414. } else {
  16415. span = me.document.createElement("span");
  16416. range.insertNode(span);
  16417. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  16418. if (domUtils.isEmptyBlock(pre)) {
  16419. pre.innerHTML = "";
  16420. }
  16421. domUtils.moveChild(li, pre);
  16422. range.setStartBefore(span).collapse(true).select(true);
  16423. domUtils.remove(span);
  16424. }
  16425. } else {
  16426. if (domUtils.isEmptyNode(li)) {
  16427. var p = me.document.createElement("p");
  16428. pre.appendChild(p);
  16429. range.setStart(p, 0).setCursor();
  16430. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16431. } else {
  16432. range
  16433. .setEnd(pre, pre.childNodes.length)
  16434. .collapse()
  16435. .select(true);
  16436. while (li.firstChild) {
  16437. pre.appendChild(li.firstChild);
  16438. }
  16439. }
  16440. }
  16441. domUtils.remove(li);
  16442. me.fireEvent("contentchange");
  16443. me.fireEvent("saveScene");
  16444. domUtils.preventDefault(evt);
  16445. return;
  16446. }
  16447. //trace:980
  16448. if (li && !li.previousSibling) {
  16449. var parentList = li.parentNode;
  16450. var bk = range.createBookmark();
  16451. if (domUtils.isTagNode(parentList.parentNode, "ol ul")) {
  16452. parentList.parentNode.insertBefore(li, parentList);
  16453. if (domUtils.isEmptyNode(parentList)) {
  16454. domUtils.remove(parentList);
  16455. }
  16456. } else {
  16457. while (li.firstChild) {
  16458. parentList.parentNode.insertBefore(li.firstChild, parentList);
  16459. }
  16460. domUtils.remove(li);
  16461. if (domUtils.isEmptyNode(parentList)) {
  16462. domUtils.remove(parentList);
  16463. }
  16464. }
  16465. range.moveToBookmark(bk).setCursor(false, true);
  16466. me.fireEvent("contentchange");
  16467. me.fireEvent("saveScene");
  16468. domUtils.preventDefault(evt);
  16469. return;
  16470. }
  16471. }
  16472. }
  16473. }
  16474. });
  16475. me.addListener("keyup", function(type, evt) {
  16476. var keyCode = evt.keyCode || evt.which;
  16477. if (keyCode == 8) {
  16478. var rng = me.selection.getRange(),
  16479. list;
  16480. if (
  16481. (list = domUtils.findParentByTagName(
  16482. rng.startContainer,
  16483. ["ol", "ul"],
  16484. true
  16485. ))
  16486. ) {
  16487. adjustList(
  16488. list,
  16489. list.tagName.toLowerCase(),
  16490. getStyle(list) || domUtils.getComputedStyle(list, "list-style-type"),
  16491. true
  16492. );
  16493. }
  16494. }
  16495. });
  16496. //处理tab键
  16497. me.addListener("tabkeydown", function() {
  16498. var range = me.selection.getRange();
  16499. //控制级数
  16500. function checkLevel(li) {
  16501. if (me.options.maxListLevel != -1) {
  16502. var level = li.parentNode,
  16503. levelNum = 0;
  16504. while (/[ou]l/i.test(level.tagName)) {
  16505. levelNum++;
  16506. level = level.parentNode;
  16507. }
  16508. if (levelNum >= me.options.maxListLevel) {
  16509. return true;
  16510. }
  16511. }
  16512. }
  16513. //只以开始为准
  16514. //todo 后续改进
  16515. var li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16516. if (li) {
  16517. var bk;
  16518. if (range.collapsed) {
  16519. if (checkLevel(li)) return true;
  16520. var parentLi = li.parentNode,
  16521. list = me.document.createElement(parentLi.tagName),
  16522. index = utils.indexOf(
  16523. listStyle[list.tagName],
  16524. getStyle(parentLi) ||
  16525. domUtils.getComputedStyle(parentLi, "list-style-type")
  16526. );
  16527. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  16528. var currentStyle = listStyle[list.tagName][index];
  16529. setListStyle(list, currentStyle);
  16530. if (domUtils.isStartInblock(range)) {
  16531. me.fireEvent("saveScene");
  16532. bk = range.createBookmark();
  16533. parentLi.insertBefore(list, li);
  16534. list.appendChild(li);
  16535. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  16536. me.fireEvent("contentchange");
  16537. range.moveToBookmark(bk).select(true);
  16538. return true;
  16539. }
  16540. } else {
  16541. me.fireEvent("saveScene");
  16542. bk = range.createBookmark();
  16543. for (
  16544. var i = 0, closeList, parents = domUtils.findParents(li), ci;
  16545. (ci = parents[i++]);
  16546. ) {
  16547. if (domUtils.isTagNode(ci, "ol ul")) {
  16548. closeList = ci;
  16549. break;
  16550. }
  16551. }
  16552. var current = li;
  16553. if (bk.end) {
  16554. while (
  16555. current &&
  16556. !(
  16557. domUtils.getPosition(current, bk.end) &
  16558. domUtils.POSITION_FOLLOWING
  16559. )
  16560. ) {
  16561. if (checkLevel(current)) {
  16562. current = domUtils.getNextDomNode(current, false, null, function(
  16563. node
  16564. ) {
  16565. return node !== closeList;
  16566. });
  16567. continue;
  16568. }
  16569. var parentLi = current.parentNode,
  16570. list = me.document.createElement(parentLi.tagName),
  16571. index = utils.indexOf(
  16572. listStyle[list.tagName],
  16573. getStyle(parentLi) ||
  16574. domUtils.getComputedStyle(parentLi, "list-style-type")
  16575. );
  16576. var currentIndex = index + 1 == listStyle[list.tagName].length
  16577. ? 0
  16578. : index + 1;
  16579. var currentStyle = listStyle[list.tagName][currentIndex];
  16580. setListStyle(list, currentStyle);
  16581. parentLi.insertBefore(list, current);
  16582. while (
  16583. current &&
  16584. !(
  16585. domUtils.getPosition(current, bk.end) &
  16586. domUtils.POSITION_FOLLOWING
  16587. )
  16588. ) {
  16589. li = current.nextSibling;
  16590. list.appendChild(current);
  16591. if (!li || domUtils.isTagNode(li, "ol ul")) {
  16592. if (li) {
  16593. while ((li = li.firstChild)) {
  16594. if (li.tagName == "LI") {
  16595. break;
  16596. }
  16597. }
  16598. } else {
  16599. li = domUtils.getNextDomNode(current, false, null, function(
  16600. node
  16601. ) {
  16602. return node !== closeList;
  16603. });
  16604. }
  16605. break;
  16606. }
  16607. current = li;
  16608. }
  16609. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  16610. current = li;
  16611. }
  16612. }
  16613. me.fireEvent("contentchange");
  16614. range.moveToBookmark(bk).select();
  16615. return true;
  16616. }
  16617. }
  16618. });
  16619. function getLi(start) {
  16620. while (start && !domUtils.isBody(start)) {
  16621. if (start.nodeName == "TABLE") {
  16622. return null;
  16623. }
  16624. if (start.nodeName == "LI") {
  16625. return start;
  16626. }
  16627. start = start.parentNode;
  16628. }
  16629. }
  16630. /**
  16631. * 有序列表,与“insertunorderedlist”命令互斥
  16632. * @command insertorderedlist
  16633. * @method execCommand
  16634. * @param { String } command 命令字符串
  16635. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16636. * @example
  16637. * ```javascript
  16638. * editor.execCommand( 'insertorderedlist','decimal');
  16639. * ```
  16640. */
  16641. /**
  16642. * 查询当前选区内容是否有序列表
  16643. * @command insertorderedlist
  16644. * @method queryCommandState
  16645. * @param { String } cmd 命令字符串
  16646. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  16647. * @example
  16648. * ```javascript
  16649. * editor.queryCommandState( 'insertorderedlist' );
  16650. * ```
  16651. */
  16652. /**
  16653. * 查询当前选区内容是否有序列表
  16654. * @command insertorderedlist
  16655. * @method queryCommandValue
  16656. * @param { String } cmd 命令字符串
  16657. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16658. * @example
  16659. * ```javascript
  16660. * editor.queryCommandValue( 'insertorderedlist' );
  16661. * ```
  16662. */
  16663. /**
  16664. * 无序列表,与“insertorderedlist”命令互斥
  16665. * @command insertunorderedlist
  16666. * @method execCommand
  16667. * @param { String } command 命令字符串
  16668. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  16669. * @example
  16670. * ```javascript
  16671. * editor.execCommand( 'insertunorderedlist','circle');
  16672. * ```
  16673. */
  16674. /**
  16675. * 查询当前是否有word文档粘贴进来的图片
  16676. * @command insertunorderedlist
  16677. * @method insertunorderedlist
  16678. * @param { String } command 命令字符串
  16679. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  16680. * @example
  16681. * ```javascript
  16682. * editor.queryCommandState( 'insertunorderedlist' );
  16683. * ```
  16684. */
  16685. /**
  16686. * 查询当前选区内容是否有序列表
  16687. * @command insertunorderedlist
  16688. * @method queryCommandValue
  16689. * @param { String } command 命令字符串
  16690. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  16691. * @example
  16692. * ```javascript
  16693. * editor.queryCommandValue( 'insertunorderedlist' );
  16694. * ```
  16695. */
  16696. me.commands["insertorderedlist"] = me.commands["insertunorderedlist"] = {
  16697. execCommand: function(command, style) {
  16698. if (!style) {
  16699. style = command.toLowerCase() == "insertorderedlist"
  16700. ? "decimal"
  16701. : "disc";
  16702. }
  16703. var me = this,
  16704. range = this.selection.getRange(),
  16705. filterFn = function(node) {
  16706. return node.nodeType == 1
  16707. ? node.tagName.toLowerCase() != "br"
  16708. : !domUtils.isWhitespace(node);
  16709. },
  16710. tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul",
  16711. frag = me.document.createDocumentFragment();
  16712. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  16713. //range.shrinkBoundary();//.adjustmentBoundary();
  16714. range.adjustmentBoundary().shrinkBoundary();
  16715. var bko = range.createBookmark(true),
  16716. start = getLi(me.document.getElementById(bko.start)),
  16717. modifyStart = 0,
  16718. end = getLi(me.document.getElementById(bko.end)),
  16719. modifyEnd = 0,
  16720. startParent,
  16721. endParent,
  16722. list,
  16723. tmp;
  16724. if (start || end) {
  16725. start && (startParent = start.parentNode);
  16726. if (!bko.end) {
  16727. end = start;
  16728. }
  16729. end && (endParent = end.parentNode);
  16730. if (startParent === endParent) {
  16731. while (start !== end) {
  16732. tmp = start;
  16733. start = start.nextSibling;
  16734. if (!domUtils.isBlockElm(tmp.firstChild)) {
  16735. var p = me.document.createElement("p");
  16736. while (tmp.firstChild) {
  16737. p.appendChild(tmp.firstChild);
  16738. }
  16739. tmp.appendChild(p);
  16740. }
  16741. frag.appendChild(tmp);
  16742. }
  16743. tmp = me.document.createElement("span");
  16744. startParent.insertBefore(tmp, end);
  16745. if (!domUtils.isBlockElm(end.firstChild)) {
  16746. p = me.document.createElement("p");
  16747. while (end.firstChild) {
  16748. p.appendChild(end.firstChild);
  16749. }
  16750. end.appendChild(p);
  16751. }
  16752. frag.appendChild(end);
  16753. domUtils.breakParent(tmp, startParent);
  16754. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  16755. domUtils.remove(tmp.previousSibling);
  16756. }
  16757. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  16758. domUtils.remove(tmp.nextSibling);
  16759. }
  16760. var nodeStyle =
  16761. getStyle(startParent) ||
  16762. domUtils.getComputedStyle(startParent, "list-style-type") ||
  16763. (command.toLowerCase() == "insertorderedlist" ? "decimal" : "disc");
  16764. if (startParent.tagName.toLowerCase() == tag && nodeStyle == style) {
  16765. for (
  16766. var i = 0, ci, tmpFrag = me.document.createDocumentFragment();
  16767. (ci = frag.firstChild);
  16768. ) {
  16769. if (domUtils.isTagNode(ci, "ol ul")) {
  16770. // 删除时,子列表不处理
  16771. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  16772. // while(li.firstChild){
  16773. // tmpFrag.appendChild(li.firstChild);
  16774. // }
  16775. //
  16776. // });
  16777. tmpFrag.appendChild(ci);
  16778. } else {
  16779. while (ci.firstChild) {
  16780. tmpFrag.appendChild(ci.firstChild);
  16781. domUtils.remove(ci);
  16782. }
  16783. }
  16784. }
  16785. tmp.parentNode.insertBefore(tmpFrag, tmp);
  16786. } else {
  16787. list = me.document.createElement(tag);
  16788. setListStyle(list, style);
  16789. list.appendChild(frag);
  16790. tmp.parentNode.insertBefore(list, tmp);
  16791. }
  16792. domUtils.remove(tmp);
  16793. list && adjustList(list, tag, style);
  16794. range.moveToBookmark(bko).select();
  16795. return;
  16796. }
  16797. //开始
  16798. if (start) {
  16799. while (start) {
  16800. tmp = start.nextSibling;
  16801. if (domUtils.isTagNode(start, "ol ul")) {
  16802. frag.appendChild(start);
  16803. } else {
  16804. var tmpfrag = me.document.createDocumentFragment(),
  16805. hasBlock = 0;
  16806. while (start.firstChild) {
  16807. if (domUtils.isBlockElm(start.firstChild)) {
  16808. hasBlock = 1;
  16809. }
  16810. tmpfrag.appendChild(start.firstChild);
  16811. }
  16812. if (!hasBlock) {
  16813. var tmpP = me.document.createElement("p");
  16814. tmpP.appendChild(tmpfrag);
  16815. frag.appendChild(tmpP);
  16816. } else {
  16817. frag.appendChild(tmpfrag);
  16818. }
  16819. domUtils.remove(start);
  16820. }
  16821. start = tmp;
  16822. }
  16823. startParent.parentNode.insertBefore(frag, startParent.nextSibling);
  16824. if (domUtils.isEmptyNode(startParent)) {
  16825. range.setStartBefore(startParent);
  16826. domUtils.remove(startParent);
  16827. } else {
  16828. range.setStartAfter(startParent);
  16829. }
  16830. modifyStart = 1;
  16831. }
  16832. if (end && domUtils.inDoc(endParent, me.document)) {
  16833. //结束
  16834. start = endParent.firstChild;
  16835. while (start && start !== end) {
  16836. tmp = start.nextSibling;
  16837. if (domUtils.isTagNode(start, "ol ul")) {
  16838. frag.appendChild(start);
  16839. } else {
  16840. tmpfrag = me.document.createDocumentFragment();
  16841. hasBlock = 0;
  16842. while (start.firstChild) {
  16843. if (domUtils.isBlockElm(start.firstChild)) {
  16844. hasBlock = 1;
  16845. }
  16846. tmpfrag.appendChild(start.firstChild);
  16847. }
  16848. if (!hasBlock) {
  16849. tmpP = me.document.createElement("p");
  16850. tmpP.appendChild(tmpfrag);
  16851. frag.appendChild(tmpP);
  16852. } else {
  16853. frag.appendChild(tmpfrag);
  16854. }
  16855. domUtils.remove(start);
  16856. }
  16857. start = tmp;
  16858. }
  16859. var tmpDiv = domUtils.createElement(me.document, "div", {
  16860. tmpDiv: 1
  16861. });
  16862. domUtils.moveChild(end, tmpDiv);
  16863. frag.appendChild(tmpDiv);
  16864. domUtils.remove(end);
  16865. endParent.parentNode.insertBefore(frag, endParent);
  16866. range.setEndBefore(endParent);
  16867. if (domUtils.isEmptyNode(endParent)) {
  16868. domUtils.remove(endParent);
  16869. }
  16870. modifyEnd = 1;
  16871. }
  16872. }
  16873. if (!modifyStart) {
  16874. range.setStartBefore(me.document.getElementById(bko.start));
  16875. }
  16876. if (bko.end && !modifyEnd) {
  16877. range.setEndAfter(me.document.getElementById(bko.end));
  16878. }
  16879. range.enlarge(true, function(node) {
  16880. return notExchange[node.tagName];
  16881. });
  16882. frag = me.document.createDocumentFragment();
  16883. var bk = range.createBookmark(),
  16884. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  16885. tmpRange = range.cloneRange(),
  16886. tmpNode,
  16887. block = domUtils.isBlockElm;
  16888. while (
  16889. current &&
  16890. current !== bk.end &&
  16891. domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING
  16892. ) {
  16893. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  16894. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  16895. while (current.firstChild) {
  16896. frag.appendChild(current.firstChild);
  16897. }
  16898. tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  16899. domUtils.remove(current);
  16900. current = tmpNode;
  16901. continue;
  16902. }
  16903. tmpNode = current;
  16904. tmpRange.setStartBefore(current);
  16905. while (
  16906. current &&
  16907. current !== bk.end &&
  16908. (!block(current) || domUtils.isBookmarkNode(current))
  16909. ) {
  16910. tmpNode = current;
  16911. current = domUtils.getNextDomNode(current, false, null, function(
  16912. node
  16913. ) {
  16914. return !notExchange[node.tagName];
  16915. });
  16916. }
  16917. if (current && block(current)) {
  16918. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn);
  16919. if (tmp && domUtils.isBookmarkNode(tmp)) {
  16920. current = domUtils.getNextDomNode(tmp, false, filterFn);
  16921. tmpNode = tmp;
  16922. }
  16923. }
  16924. tmpRange.setEndAfter(tmpNode);
  16925. current = domUtils.getNextDomNode(tmpNode, false, filterFn);
  16926. var li = range.document.createElement("li");
  16927. li.appendChild(tmpRange.extractContents());
  16928. if (domUtils.isEmptyNode(li)) {
  16929. var tmpNode = range.document.createElement("p");
  16930. while (li.firstChild) {
  16931. tmpNode.appendChild(li.firstChild);
  16932. }
  16933. li.appendChild(tmpNode);
  16934. }
  16935. frag.appendChild(li);
  16936. } else {
  16937. current = domUtils.getNextDomNode(current, true, filterFn);
  16938. }
  16939. }
  16940. range.moveToBookmark(bk).collapse(true);
  16941. list = me.document.createElement(tag);
  16942. setListStyle(list, style);
  16943. list.appendChild(frag);
  16944. range.insertNode(list);
  16945. //当前list上下看能否合并
  16946. adjustList(list, tag, style);
  16947. //去掉冗余的tmpDiv
  16948. for (
  16949. var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, "div");
  16950. (ci = tmpDivs[i++]);
  16951. ) {
  16952. if (ci.getAttribute("tmpDiv")) {
  16953. domUtils.remove(ci, true);
  16954. }
  16955. }
  16956. range.moveToBookmark(bko).select();
  16957. },
  16958. queryCommandState: function(command) {
  16959. var tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul";
  16960. var path = this.selection.getStartElementPath();
  16961. for (var i = 0, ci; (ci = path[i++]); ) {
  16962. if (ci.nodeName == "TABLE") {
  16963. return 0;
  16964. }
  16965. if (tag == ci.nodeName.toLowerCase()) {
  16966. return 1;
  16967. }
  16968. }
  16969. return 0;
  16970. },
  16971. queryCommandValue: function(command) {
  16972. var tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul";
  16973. var path = this.selection.getStartElementPath(),
  16974. node;
  16975. for (var i = 0, ci; (ci = path[i++]); ) {
  16976. if (ci.nodeName == "TABLE") {
  16977. node = null;
  16978. break;
  16979. }
  16980. if (tag == ci.nodeName.toLowerCase()) {
  16981. node = ci;
  16982. break;
  16983. }
  16984. }
  16985. return node
  16986. ? getStyle(node) || domUtils.getComputedStyle(node, "list-style-type")
  16987. : null;
  16988. }
  16989. };
  16990. };
  16991. // plugins/source.js
  16992. /**
  16993. * 源码编辑插件
  16994. * @file
  16995. * @since 1.2.6.1
  16996. */
  16997. ;(function() {
  16998. var sourceEditors = {
  16999. textarea: function(editor, holder) {
  17000. var textarea = holder.ownerDocument.createElement("textarea");
  17001. textarea.style.cssText =
  17002. "position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;";
  17003. // todo: IE下只有onresize属性可用... 很纠结
  17004. if (browser.ie && browser.version < 8) {
  17005. textarea.style.width = holder.offsetWidth + "px";
  17006. textarea.style.height = holder.offsetHeight + "px";
  17007. holder.onresize = function() {
  17008. textarea.style.width = holder.offsetWidth + "px";
  17009. textarea.style.height = holder.offsetHeight + "px";
  17010. };
  17011. }
  17012. holder.appendChild(textarea);
  17013. return {
  17014. setContent: function(content) {
  17015. textarea.value = content;
  17016. },
  17017. getContent: function() {
  17018. return textarea.value;
  17019. },
  17020. select: function() {
  17021. var range;
  17022. if (browser.ie) {
  17023. range = textarea.createTextRange();
  17024. range.collapse(true);
  17025. range.select();
  17026. } else {
  17027. //todo: chrome下无法设置焦点
  17028. textarea.setSelectionRange(0, 0);
  17029. textarea.focus();
  17030. }
  17031. },
  17032. dispose: function() {
  17033. holder.removeChild(textarea);
  17034. // todo
  17035. holder.onresize = null;
  17036. textarea = null;
  17037. holder = null;
  17038. },
  17039. focus: function (){
  17040. textarea.focus();
  17041. },
  17042. blur: function (){
  17043. textarea.blur();
  17044. }
  17045. };
  17046. },
  17047. codemirror: function(editor, holder) {
  17048. var codeEditor = window.CodeMirror(holder, {
  17049. mode: "text/html",
  17050. tabMode: "indent",
  17051. lineNumbers: true,
  17052. lineWrapping: true
  17053. });
  17054. var dom = codeEditor.getWrapperElement();
  17055. dom.style.cssText =
  17056. 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
  17057. codeEditor.getScrollerElement().style.cssText =
  17058. "position:absolute;left:0;top:0;width:100%;height:100%;";
  17059. codeEditor.refresh();
  17060. return {
  17061. getCodeMirror: function() {
  17062. return codeEditor;
  17063. },
  17064. setContent: function(content) {
  17065. codeEditor.setValue(content);
  17066. },
  17067. getContent: function() {
  17068. return codeEditor.getValue();
  17069. },
  17070. select: function() {
  17071. codeEditor.focus();
  17072. },
  17073. dispose: function() {
  17074. holder.removeChild(dom);
  17075. dom = null;
  17076. codeEditor = null;
  17077. },
  17078. focus: function (){
  17079. codeEditor.focus();
  17080. },
  17081. blur: function (){
  17082. // codeEditor.blur();
  17083. // since codemirror not support blur()
  17084. codeEditor.setOption('readOnly', true);
  17085. codeEditor.setOption('readOnly', false);
  17086. }
  17087. };
  17088. }
  17089. };
  17090. UE.plugins["source"] = function() {
  17091. var me = this;
  17092. var opt = this.options;
  17093. var sourceMode = false;
  17094. var sourceEditor;
  17095. var orgSetContent;
  17096. var orgFocus;
  17097. var orgBlur;
  17098. opt.sourceEditor = browser.ie
  17099. ? "textarea"
  17100. : opt.sourceEditor || "codemirror";
  17101. me.setOpt({
  17102. sourceEditorFirst: false
  17103. });
  17104. function createSourceEditor(holder) {
  17105. return sourceEditors[
  17106. opt.sourceEditor == "codemirror" && window.CodeMirror
  17107. ? "codemirror"
  17108. : "textarea"
  17109. ](me, holder);
  17110. }
  17111. var bakCssText;
  17112. //解决在源码模式下getContent不能得到最新的内容问题
  17113. var oldGetContent, bakAddress;
  17114. /**
  17115. * 切换源码模式和编辑模式
  17116. * @command source
  17117. * @method execCommand
  17118. * @param { String } cmd 命令字符串
  17119. * @example
  17120. * ```javascript
  17121. * editor.execCommand( 'source');
  17122. * ```
  17123. */
  17124. /**
  17125. * 查询当前编辑区域的状态是源码模式还是可视化模式
  17126. * @command source
  17127. * @method queryCommandState
  17128. * @param { String } cmd 命令字符串
  17129. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  17130. * @example
  17131. * ```javascript
  17132. * editor.queryCommandState( 'source' );
  17133. * ```
  17134. */
  17135. me.commands["source"] = {
  17136. execCommand: function() {
  17137. sourceMode = !sourceMode;
  17138. if (sourceMode) {
  17139. bakAddress = me.selection.getRange().createAddress(false, true);
  17140. me.undoManger && me.undoManger.save(true);
  17141. if (browser.gecko) {
  17142. me.body.contentEditable = false;
  17143. }
  17144. bakCssText = me.iframe.style.cssText;
  17145. me.iframe.style.cssText +=
  17146. "position:absolute;left:-32768px;top:-32768px;";
  17147. me.fireEvent("beforegetcontent");
  17148. var root = UE.htmlparser(me.body.innerHTML);
  17149. me.filterOutputRule(root);
  17150. root.traversal(function(node) {
  17151. if (node.type == "element") {
  17152. switch (node.tagName) {
  17153. case "td":
  17154. case "th":
  17155. case "caption":
  17156. if (node.children && node.children.length == 1) {
  17157. if (node.firstChild().tagName == "br") {
  17158. node.removeChild(node.firstChild());
  17159. }
  17160. }
  17161. break;
  17162. case "pre":
  17163. node.innerText(node.innerText().replace(/&nbsp;/g, " "));
  17164. }
  17165. }
  17166. });
  17167. me.fireEvent("aftergetcontent");
  17168. var content = root.toHtml(true);
  17169. sourceEditor = createSourceEditor(me.iframe.parentNode);
  17170. sourceEditor.setContent(content);
  17171. orgSetContent = me.setContent;
  17172. me.setContent = function(html) {
  17173. //这里暂时不触发事件,防止报错
  17174. var root = UE.htmlparser(html);
  17175. me.filterInputRule(root);
  17176. html = root.toHtml();
  17177. sourceEditor.setContent(html);
  17178. };
  17179. setTimeout(function() {
  17180. sourceEditor.select();
  17181. me.addListener("fullscreenchanged", function() {
  17182. try {
  17183. sourceEditor.getCodeMirror().refresh();
  17184. } catch (e) {}
  17185. });
  17186. });
  17187. //重置getContent,源码模式下取值也能是最新的数据
  17188. oldGetContent = me.getContent;
  17189. me.getContent = function() {
  17190. return (
  17191. sourceEditor.getContent() ||
  17192. "<p>" + (browser.ie ? "" : "<br/>") + "</p>"
  17193. );
  17194. };
  17195. orgFocus = me.focus;
  17196. orgBlur = me.blur;
  17197. me.focus = function(){
  17198. sourceEditor.focus();
  17199. };
  17200. me.blur = function(){
  17201. orgBlur.call(me);
  17202. sourceEditor.blur();
  17203. };
  17204. } else {
  17205. me.iframe.style.cssText = bakCssText;
  17206. var cont =
  17207. sourceEditor.getContent() ||
  17208. "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17209. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  17210. cont = cont.replace(
  17211. new RegExp("[\\r\\t\\n ]*</?(\\w+)\\s*(?:[^>]*)>", "g"),
  17212. function(a, b) {
  17213. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  17214. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, "");
  17215. }
  17216. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, "");
  17217. }
  17218. );
  17219. me.setContent = orgSetContent;
  17220. me.setContent(cont);
  17221. sourceEditor.dispose();
  17222. sourceEditor = null;
  17223. //还原getContent方法
  17224. me.getContent = oldGetContent;
  17225. me.focus = orgFocus;
  17226. me.blur = orgBlur;
  17227. var first = me.body.firstChild;
  17228. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  17229. if (!first) {
  17230. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17231. first = me.body.firstChild;
  17232. }
  17233. //要在ifm为显示时ff才能取到selection,否则报错
  17234. //这里不能比较位置了
  17235. me.undoManger && me.undoManger.save(true);
  17236. if (browser.gecko) {
  17237. var input = document.createElement("input");
  17238. input.style.cssText = "position:absolute;left:0;top:-32768px";
  17239. document.body.appendChild(input);
  17240. me.body.contentEditable = false;
  17241. setTimeout(function() {
  17242. domUtils.setViewportOffset(input, { left: -32768, top: 0 });
  17243. input.focus();
  17244. setTimeout(function() {
  17245. me.body.contentEditable = true;
  17246. me.selection.getRange().moveToAddress(bakAddress).select(true);
  17247. domUtils.remove(input);
  17248. });
  17249. });
  17250. } else {
  17251. //ie下有可能报错,比如在代码顶头的情况
  17252. try {
  17253. me.selection.getRange().moveToAddress(bakAddress).select(true);
  17254. } catch (e) {}
  17255. }
  17256. }
  17257. this.fireEvent("sourcemodechanged", sourceMode);
  17258. },
  17259. queryCommandState: function() {
  17260. return sourceMode | 0;
  17261. },
  17262. notNeedUndo: 1
  17263. };
  17264. var oldQueryCommandState = me.queryCommandState;
  17265. me.queryCommandState = function(cmdName) {
  17266. cmdName = cmdName.toLowerCase();
  17267. if (sourceMode) {
  17268. //源码模式下可以开启的命令
  17269. return cmdName in
  17270. {
  17271. source: 1,
  17272. fullscreen: 1
  17273. }
  17274. ? 1
  17275. : -1;
  17276. }
  17277. return oldQueryCommandState.apply(this, arguments);
  17278. };
  17279. if (opt.sourceEditor == "codemirror") {
  17280. me.addListener("ready", function() {
  17281. utils.loadFile(
  17282. document,
  17283. {
  17284. src:
  17285. opt.codeMirrorJsUrl ||
  17286. opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.js",
  17287. tag: "script",
  17288. type: "text/javascript",
  17289. defer: "defer"
  17290. },
  17291. function() {
  17292. if (opt.sourceEditorFirst) {
  17293. setTimeout(function() {
  17294. me.execCommand("source");
  17295. }, 0);
  17296. }
  17297. }
  17298. );
  17299. utils.loadFile(document, {
  17300. tag: "link",
  17301. rel: "stylesheet",
  17302. type: "text/css",
  17303. href:
  17304. opt.codeMirrorCssUrl ||
  17305. opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.css"
  17306. });
  17307. });
  17308. }
  17309. };
  17310. })();
  17311. // plugins/enterkey.js
  17312. ///import core
  17313. ///import plugins/undo.js
  17314. ///commands 设置回车标签p或br
  17315. ///commandsName EnterKey
  17316. ///commandsTitle 设置回车标签p或br
  17317. /**
  17318. * @description 处理回车
  17319. * @author zhanyi
  17320. */
  17321. UE.plugins["enterkey"] = function() {
  17322. var hTag,
  17323. me = this,
  17324. tag = me.options.enterTag;
  17325. me.addListener("keyup", function(type, evt) {
  17326. var keyCode = evt.keyCode || evt.which;
  17327. if (keyCode == 13) {
  17328. var range = me.selection.getRange(),
  17329. start = range.startContainer,
  17330. doSave;
  17331. //修正在h1-h6里边回车后不能嵌套p的问题
  17332. if (!browser.ie) {
  17333. if (/h\d/i.test(hTag)) {
  17334. if (browser.gecko) {
  17335. var h = domUtils.findParentByTagName(
  17336. start,
  17337. [
  17338. "h1",
  17339. "h2",
  17340. "h3",
  17341. "h4",
  17342. "h5",
  17343. "h6",
  17344. "blockquote",
  17345. "caption",
  17346. "table"
  17347. ],
  17348. true
  17349. );
  17350. if (!h) {
  17351. me.document.execCommand("formatBlock", false, "<p>");
  17352. doSave = 1;
  17353. }
  17354. } else {
  17355. //chrome remove div
  17356. if (start.nodeType == 1) {
  17357. var tmp = me.document.createTextNode(""),
  17358. div;
  17359. range.insertNode(tmp);
  17360. div = domUtils.findParentByTagName(tmp, "div", true);
  17361. if (div) {
  17362. var p = me.document.createElement("p");
  17363. while (div.firstChild) {
  17364. p.appendChild(div.firstChild);
  17365. }
  17366. div.parentNode.insertBefore(p, div);
  17367. domUtils.remove(div);
  17368. range.setStartBefore(tmp).setCursor();
  17369. doSave = 1;
  17370. }
  17371. domUtils.remove(tmp);
  17372. }
  17373. }
  17374. if (me.undoManger && doSave) {
  17375. me.undoManger.save();
  17376. }
  17377. }
  17378. //没有站位符,会出现多行的问题
  17379. browser.opera && range.select();
  17380. } else {
  17381. me.fireEvent("saveScene", true, true);
  17382. }
  17383. }
  17384. });
  17385. me.addListener("keydown", function(type, evt) {
  17386. var keyCode = evt.keyCode || evt.which;
  17387. if (keyCode == 13) {
  17388. //回车
  17389. if (me.fireEvent("beforeenterkeydown")) {
  17390. domUtils.preventDefault(evt);
  17391. return;
  17392. }
  17393. me.fireEvent("saveScene", true, true);
  17394. hTag = "";
  17395. var range = me.selection.getRange();
  17396. if (!range.collapsed) {
  17397. //跨td不能删
  17398. var start = range.startContainer,
  17399. end = range.endContainer,
  17400. startTd = domUtils.findParentByTagName(start, "td", true),
  17401. endTd = domUtils.findParentByTagName(end, "td", true);
  17402. if (
  17403. (startTd && endTd && startTd !== endTd) ||
  17404. (!startTd && endTd) ||
  17405. (startTd && !endTd)
  17406. ) {
  17407. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  17408. return;
  17409. }
  17410. }
  17411. if (tag == "p") {
  17412. if (!browser.ie) {
  17413. start = domUtils.findParentByTagName(
  17414. range.startContainer,
  17415. [
  17416. "ol",
  17417. "ul",
  17418. "p",
  17419. "h1",
  17420. "h2",
  17421. "h3",
  17422. "h4",
  17423. "h5",
  17424. "h6",
  17425. "blockquote",
  17426. "caption"
  17427. ],
  17428. true
  17429. );
  17430. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  17431. //trace:2431
  17432. if (!start && !browser.opera) {
  17433. me.document.execCommand("formatBlock", false, "<p>");
  17434. if (browser.gecko) {
  17435. range = me.selection.getRange();
  17436. start = domUtils.findParentByTagName(
  17437. range.startContainer,
  17438. "p",
  17439. true
  17440. );
  17441. start && domUtils.removeDirtyAttr(start);
  17442. }
  17443. } else {
  17444. hTag = start.tagName;
  17445. start.tagName.toLowerCase() == "p" &&
  17446. browser.gecko &&
  17447. domUtils.removeDirtyAttr(start);
  17448. }
  17449. }
  17450. } else {
  17451. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  17452. if (!range.collapsed) {
  17453. range.deleteContents();
  17454. start = range.startContainer;
  17455. if (
  17456. start.nodeType == 1 &&
  17457. (start = start.childNodes[range.startOffset])
  17458. ) {
  17459. while (start.nodeType == 1) {
  17460. if (dtd.$empty[start.tagName]) {
  17461. range.setStartBefore(start).setCursor();
  17462. if (me.undoManger) {
  17463. me.undoManger.save();
  17464. }
  17465. return false;
  17466. }
  17467. if (!start.firstChild) {
  17468. var br = range.document.createElement("br");
  17469. start.appendChild(br);
  17470. range.setStart(start, 0).setCursor();
  17471. if (me.undoManger) {
  17472. me.undoManger.save();
  17473. }
  17474. return false;
  17475. }
  17476. start = start.firstChild;
  17477. }
  17478. if (start === range.startContainer.childNodes[range.startOffset]) {
  17479. br = range.document.createElement("br");
  17480. range.insertNode(br).setCursor();
  17481. } else {
  17482. range.setStart(start, 0).setCursor();
  17483. }
  17484. } else {
  17485. br = range.document.createElement("br");
  17486. range.insertNode(br).setStartAfter(br).setCursor();
  17487. }
  17488. } else {
  17489. br = range.document.createElement("br");
  17490. range.insertNode(br);
  17491. var parent = br.parentNode;
  17492. if (parent.lastChild === br) {
  17493. br.parentNode.insertBefore(br.cloneNode(true), br);
  17494. range.setStartBefore(br);
  17495. } else {
  17496. range.setStartAfter(br);
  17497. }
  17498. range.setCursor();
  17499. }
  17500. }
  17501. }
  17502. });
  17503. };
  17504. // plugins/keystrokes.js
  17505. /* 处理特殊键的兼容性问题 */
  17506. UE.plugins["keystrokes"] = function() {
  17507. var me = this;
  17508. var collapsed = true;
  17509. me.addListener("keydown", function(type, evt) {
  17510. var keyCode = evt.keyCode || evt.which,
  17511. rng = me.selection.getRange();
  17512. //处理全选的情况
  17513. if (
  17514. !rng.collapsed &&
  17515. !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) &&
  17516. ((keyCode >= 65 && keyCode <= 90) ||
  17517. (keyCode >= 48 && keyCode <= 57) ||
  17518. (keyCode >= 96 && keyCode <= 111) ||
  17519. {
  17520. 13: 1,
  17521. 8: 1,
  17522. 46: 1
  17523. }[keyCode])
  17524. ) {
  17525. var tmpNode = rng.startContainer;
  17526. if (domUtils.isFillChar(tmpNode)) {
  17527. rng.setStartBefore(tmpNode);
  17528. }
  17529. tmpNode = rng.endContainer;
  17530. if (domUtils.isFillChar(tmpNode)) {
  17531. rng.setEndAfter(tmpNode);
  17532. }
  17533. rng.txtToElmBoundary();
  17534. //结束边界可能放到了br的前边,要把br包含进来
  17535. // x[xxx]<br/>
  17536. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  17537. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  17538. if (tmpNode && domUtils.isBr(tmpNode)) {
  17539. rng.setEndAfter(tmpNode);
  17540. }
  17541. }
  17542. if (rng.startOffset == 0) {
  17543. tmpNode = rng.startContainer;
  17544. if (domUtils.isBoundaryNode(tmpNode, "firstChild")) {
  17545. tmpNode = rng.endContainer;
  17546. if (
  17547. rng.endOffset ==
  17548. (tmpNode.nodeType == 3
  17549. ? tmpNode.nodeValue.length
  17550. : tmpNode.childNodes.length) &&
  17551. domUtils.isBoundaryNode(tmpNode, "lastChild")
  17552. ) {
  17553. me.fireEvent("saveScene");
  17554. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17555. rng.setStart(me.body.firstChild, 0).setCursor(false, true);
  17556. me._selectionChange();
  17557. return;
  17558. }
  17559. }
  17560. }
  17561. }
  17562. //处理backspace
  17563. if (keyCode == keymap.Backspace) {
  17564. rng = me.selection.getRange();
  17565. collapsed = rng.collapsed;
  17566. if (me.fireEvent("delkeydown", evt)) {
  17567. return;
  17568. }
  17569. var start, end;
  17570. //避免按两次删除才能生效的问题
  17571. if (rng.collapsed && rng.inFillChar()) {
  17572. start = rng.startContainer;
  17573. if (domUtils.isFillChar(start)) {
  17574. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  17575. domUtils.remove(start);
  17576. } else {
  17577. start.nodeValue = start.nodeValue.replace(
  17578. new RegExp("^" + domUtils.fillChar),
  17579. ""
  17580. );
  17581. rng.startOffset--;
  17582. rng.collapse(true).select(true);
  17583. }
  17584. }
  17585. //解决选中control元素不能删除的问题
  17586. if ((start = rng.getClosedNode())) {
  17587. me.fireEvent("saveScene");
  17588. rng.setStartBefore(start);
  17589. domUtils.remove(start);
  17590. rng.setCursor();
  17591. me.fireEvent("saveScene");
  17592. domUtils.preventDefault(evt);
  17593. return;
  17594. }
  17595. //阻止在table上的删除
  17596. if (!browser.ie) {
  17597. start = domUtils.findParentByTagName(rng.startContainer, "table", true);
  17598. end = domUtils.findParentByTagName(rng.endContainer, "table", true);
  17599. if ((start && !end) || (!start && end) || start !== end) {
  17600. evt.preventDefault();
  17601. return;
  17602. }
  17603. }
  17604. }
  17605. //处理tab键的逻辑
  17606. if (keyCode == keymap.Tab) {
  17607. //不处理以下标签
  17608. var excludeTagNameForTabKey = {
  17609. ol: 1,
  17610. ul: 1,
  17611. table: 1
  17612. };
  17613. //处理组件里的tab按下事件
  17614. if (me.fireEvent("tabkeydown", evt)) {
  17615. domUtils.preventDefault(evt);
  17616. return;
  17617. }
  17618. var range = me.selection.getRange();
  17619. me.fireEvent("saveScene");
  17620. for (
  17621. var i = 0,
  17622. txt = "",
  17623. tabSize = me.options.tabSize || 4,
  17624. tabNode = me.options.tabNode || "&nbsp;";
  17625. i < tabSize;
  17626. i++
  17627. ) {
  17628. txt += tabNode;
  17629. }
  17630. var span = me.document.createElement("span");
  17631. span.innerHTML = txt + domUtils.fillChar;
  17632. if (range.collapsed) {
  17633. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  17634. } else {
  17635. var filterFn = function(node) {
  17636. return (
  17637. domUtils.isBlockElm(node) &&
  17638. !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  17639. );
  17640. };
  17641. //普通的情况
  17642. start = domUtils.findParent(range.startContainer, filterFn, true);
  17643. end = domUtils.findParent(range.endContainer, filterFn, true);
  17644. if (start && end && start === end) {
  17645. range.deleteContents();
  17646. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  17647. } else {
  17648. var bookmark = range.createBookmark();
  17649. range.enlarge(true);
  17650. var bookmark2 = range.createBookmark(),
  17651. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
  17652. while (
  17653. current &&
  17654. !(
  17655. domUtils.getPosition(current, bookmark2.end) &
  17656. domUtils.POSITION_FOLLOWING
  17657. )
  17658. ) {
  17659. current.insertBefore(
  17660. span.cloneNode(true).firstChild,
  17661. current.firstChild
  17662. );
  17663. current = domUtils.getNextDomNode(current, false, filterFn);
  17664. }
  17665. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
  17666. }
  17667. }
  17668. domUtils.preventDefault(evt);
  17669. }
  17670. //trace:1634
  17671. //ff的del键在容器空的时候,也会删除
  17672. if (browser.gecko && keyCode == 46) {
  17673. range = me.selection.getRange();
  17674. if (range.collapsed) {
  17675. start = range.startContainer;
  17676. if (domUtils.isEmptyBlock(start)) {
  17677. var parent = start.parentNode;
  17678. while (
  17679. domUtils.getChildCount(parent) == 1 &&
  17680. !domUtils.isBody(parent)
  17681. ) {
  17682. start = parent;
  17683. parent = parent.parentNode;
  17684. }
  17685. if (start === parent.lastChild) evt.preventDefault();
  17686. return;
  17687. }
  17688. }
  17689. }
  17690. /* 修复在编辑区域快捷键 (Mac:meta+alt+I; Win:ctrl+shift+I) 打不开 chrome 控制台的问题 */
  17691. browser.chrome &&
  17692. me.on("keydown", function(type, e) {
  17693. var keyCode = e.keyCode || e.which;
  17694. if (
  17695. ((e.metaKey && e.altKey) || (e.ctrlKey && e.shiftKey)) &&
  17696. keyCode == 73
  17697. ) {
  17698. return true;
  17699. }
  17700. });
  17701. });
  17702. me.addListener("keyup", function(type, evt) {
  17703. var keyCode = evt.keyCode || evt.which,
  17704. rng,
  17705. me = this;
  17706. if (keyCode == keymap.Backspace) {
  17707. if (me.fireEvent("delkeyup")) {
  17708. return;
  17709. }
  17710. rng = me.selection.getRange();
  17711. if (rng.collapsed) {
  17712. var tmpNode,
  17713. autoClearTagName = ["h1", "h2", "h3", "h4", "h5", "h6"];
  17714. if (
  17715. (tmpNode = domUtils.findParentByTagName(
  17716. rng.startContainer,
  17717. autoClearTagName,
  17718. true
  17719. ))
  17720. ) {
  17721. if (domUtils.isEmptyBlock(tmpNode)) {
  17722. var pre = tmpNode.previousSibling;
  17723. if (pre && pre.nodeName != "TABLE") {
  17724. domUtils.remove(tmpNode);
  17725. rng.setStartAtLast(pre).setCursor(false, true);
  17726. return;
  17727. } else {
  17728. var next = tmpNode.nextSibling;
  17729. if (next && next.nodeName != "TABLE") {
  17730. domUtils.remove(tmpNode);
  17731. rng.setStartAtFirst(next).setCursor(false, true);
  17732. return;
  17733. }
  17734. }
  17735. }
  17736. }
  17737. //处理当删除到body时,要重新给p标签展位
  17738. if (domUtils.isBody(rng.startContainer)) {
  17739. var tmpNode = domUtils.createElement(me.document, "p", {
  17740. innerHTML: browser.ie ? domUtils.fillChar : "<br/>"
  17741. });
  17742. rng.insertNode(tmpNode).setStart(tmpNode, 0).setCursor(false, true);
  17743. }
  17744. }
  17745. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  17746. if (
  17747. !collapsed &&
  17748. (rng.startContainer.nodeType == 3 ||
  17749. (rng.startContainer.nodeType == 1 &&
  17750. domUtils.isEmptyBlock(rng.startContainer)))
  17751. ) {
  17752. if (browser.ie) {
  17753. var span = rng.document.createElement("span");
  17754. rng.insertNode(span).setStartBefore(span).collapse(true);
  17755. rng.select();
  17756. domUtils.remove(span);
  17757. } else {
  17758. rng.select();
  17759. }
  17760. }
  17761. }
  17762. });
  17763. };
  17764. // plugins/fiximgclick.js
  17765. ///import core
  17766. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17767. ///commandsName FixImgClick
  17768. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17769. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  17770. UE.plugins["fiximgclick"] = (function() {
  17771. var elementUpdated = false;
  17772. function Scale() {
  17773. this.editor = null;
  17774. this.resizer = null;
  17775. this.cover = null;
  17776. this.doc = document;
  17777. this.prePos = { x: 0, y: 0 };
  17778. this.startPos = { x: 0, y: 0 };
  17779. }
  17780. (function() {
  17781. var rect = [
  17782. //[left, top, width, height]
  17783. [0, 0, -1, -1],
  17784. [0, 0, 0, -1],
  17785. [0, 0, 1, -1],
  17786. [0, 0, -1, 0],
  17787. [0, 0, 1, 0],
  17788. [0, 0, -1, 1],
  17789. [0, 0, 0, 1],
  17790. [0, 0, 1, 1]
  17791. ];
  17792. Scale.prototype = {
  17793. init: function(editor) {
  17794. var me = this;
  17795. me.editor = editor;
  17796. me.startPos = this.prePos = { x: 0, y: 0 };
  17797. me.dragId = -1;
  17798. var hands = [],
  17799. cover = (me.cover = document.createElement("div")),
  17800. resizer = (me.resizer = document.createElement("div"));
  17801. cover.id = me.editor.ui.id + "_imagescale_cover";
  17802. cover.style.cssText =
  17803. "position:absolute;display:none;z-index:" +
  17804. me.editor.options.zIndex +
  17805. ";filter:alpha(opacity=0); opacity:0;background:#CCC;";
  17806. domUtils.on(cover, "mousedown click", function() {
  17807. me.hide();
  17808. });
  17809. for (i = 0; i < 8; i++) {
  17810. hands.push(
  17811. '<span class="edui-editor-imagescale-hand' + i + '"></span>'
  17812. );
  17813. }
  17814. resizer.id = me.editor.ui.id + "_imagescale";
  17815. resizer.className = "edui-editor-imagescale";
  17816. resizer.innerHTML = hands.join("");
  17817. resizer.style.cssText +=
  17818. ";display:none;border:1px solid #3b77ff;z-index:" +
  17819. me.editor.options.zIndex +
  17820. ";";
  17821. me.editor.ui.getDom().appendChild(cover);
  17822. me.editor.ui.getDom().appendChild(resizer);
  17823. me.initStyle();
  17824. me.initEvents();
  17825. },
  17826. initStyle: function() {
  17827. utils.cssRule(
  17828. "imagescale",
  17829. ".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;}" +
  17830. ".edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}" +
  17831. ".edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}" +
  17832. ".edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}" +
  17833. ".edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}" +
  17834. ".edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}" +
  17835. ".edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}" +
  17836. ".edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}" +
  17837. ".edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}" +
  17838. ".edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}"
  17839. );
  17840. },
  17841. initEvents: function() {
  17842. var me = this;
  17843. me.startPos.x = me.startPos.y = 0;
  17844. me.isDraging = false;
  17845. },
  17846. _eventHandler: function(e) {
  17847. var me = this,
  17848. pressMouseLeft = e.buttons === undefined ? e.which === 1 : e.buttons === 1;
  17849. switch (e.type) {
  17850. case "mousedown":
  17851. var hand = e.target || e.srcElement,
  17852. hand;
  17853. if (
  17854. hand.className.indexOf("edui-editor-imagescale-hand") != -1 &&
  17855. me.dragId == -1
  17856. ) {
  17857. me.dragId = hand.className.slice(-1);
  17858. me.startPos.x = me.prePos.x = e.clientX;
  17859. me.startPos.y = me.prePos.y = e.clientY;
  17860. domUtils.on(me.doc, "mousemove", me.proxy(me._eventHandler, me));
  17861. }
  17862. break;
  17863. case "mousemove":
  17864. if (me.dragId != -1 && pressMouseLeft) {
  17865. me.updateContainerStyle(me.dragId, {
  17866. x: e.clientX - me.prePos.x,
  17867. y: e.clientY - me.prePos.y
  17868. });
  17869. me.prePos.x = e.clientX;
  17870. me.prePos.y = e.clientY;
  17871. elementUpdated = true;
  17872. me.updateTargetElement();
  17873. }
  17874. break;
  17875. case "mouseup":
  17876. if (me.dragId != -1) {
  17877. me.updateContainerStyle(me.dragId, {
  17878. x: e.clientX - me.prePos.x,
  17879. y: e.clientY - me.prePos.y
  17880. });
  17881. me.updateTargetElement();
  17882. if (me.target.parentNode) me.attachTo(me.target);
  17883. me.dragId = -1;
  17884. }
  17885. domUtils.un(me.doc, "mousemove", me.proxy(me._eventHandler, me));
  17886. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  17887. if (elementUpdated) {
  17888. elementUpdated = false;
  17889. me.editor.fireEvent("contentchange");
  17890. }
  17891. break;
  17892. default:
  17893. break;
  17894. }
  17895. },
  17896. updateTargetElement: function() {
  17897. var me = this;
  17898. domUtils.setStyles(me.target, {
  17899. width: me.resizer.style.width,
  17900. height: me.resizer.style.height
  17901. });
  17902. me.target.width = parseInt(me.resizer.style.width);
  17903. me.target.height = parseInt(me.resizer.style.height);
  17904. me.attachTo(me.target);
  17905. },
  17906. updateContainerStyle: function(dir, offset) {
  17907. var me = this,
  17908. dom = me.resizer,
  17909. tmp;
  17910. if (rect[dir][0] != 0) {
  17911. tmp = parseInt(dom.style.left) + offset.x;
  17912. dom.style.left = me._validScaledProp("left", tmp) + "px";
  17913. }
  17914. if (rect[dir][1] != 0) {
  17915. tmp = parseInt(dom.style.top) + offset.y;
  17916. dom.style.top = me._validScaledProp("top", tmp) + "px";
  17917. }
  17918. if (rect[dir][2] != 0) {
  17919. tmp = dom.clientWidth + rect[dir][2] * offset.x;
  17920. dom.style.width = me._validScaledProp("width", tmp) + "px";
  17921. }
  17922. if (rect[dir][3] != 0) {
  17923. tmp = dom.clientHeight + rect[dir][3] * offset.y;
  17924. dom.style.height = me._validScaledProp("height", tmp) + "px";
  17925. }
  17926. },
  17927. _validScaledProp: function(prop, value) {
  17928. var ele = this.resizer,
  17929. wrap = document;
  17930. value = isNaN(value) ? 0 : value;
  17931. switch (prop) {
  17932. case "left":
  17933. return value < 0
  17934. ? 0
  17935. : value + ele.clientWidth > wrap.clientWidth
  17936. ? wrap.clientWidth - ele.clientWidth
  17937. : value;
  17938. case "top":
  17939. return value < 0
  17940. ? 0
  17941. : value + ele.clientHeight > wrap.clientHeight
  17942. ? wrap.clientHeight - ele.clientHeight
  17943. : value;
  17944. case "width":
  17945. return value <= 0
  17946. ? 1
  17947. : value + ele.offsetLeft > wrap.clientWidth
  17948. ? wrap.clientWidth - ele.offsetLeft
  17949. : value;
  17950. case "height":
  17951. return value <= 0
  17952. ? 1
  17953. : value + ele.offsetTop > wrap.clientHeight
  17954. ? wrap.clientHeight - ele.offsetTop
  17955. : value;
  17956. }
  17957. },
  17958. hideCover: function() {
  17959. this.cover.style.display = "none";
  17960. },
  17961. showCover: function() {
  17962. var me = this,
  17963. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  17964. iframePos = domUtils.getXY(me.editor.iframe);
  17965. domUtils.setStyles(me.cover, {
  17966. width: me.editor.iframe.offsetWidth + "px",
  17967. height: me.editor.iframe.offsetHeight + "px",
  17968. top: iframePos.y - editorPos.y + "px",
  17969. left: iframePos.x - editorPos.x + "px",
  17970. position: "absolute",
  17971. display: ""
  17972. });
  17973. },
  17974. show: function(targetObj) {
  17975. var me = this;
  17976. me.resizer.style.display = "block";
  17977. if (targetObj) me.attachTo(targetObj);
  17978. domUtils.on(this.resizer, "mousedown", me.proxy(me._eventHandler, me));
  17979. domUtils.on(me.doc, "mouseup", me.proxy(me._eventHandler, me));
  17980. me.showCover();
  17981. me.editor.fireEvent("afterscaleshow", me);
  17982. me.editor.fireEvent("saveScene");
  17983. },
  17984. hide: function() {
  17985. var me = this;
  17986. me.hideCover();
  17987. me.resizer.style.display = "none";
  17988. domUtils.un(me.resizer, "mousedown", me.proxy(me._eventHandler, me));
  17989. domUtils.un(me.doc, "mouseup", me.proxy(me._eventHandler, me));
  17990. me.editor.fireEvent("afterscalehide", me);
  17991. },
  17992. proxy: function(fn, context) {
  17993. return function(e) {
  17994. return fn.apply(context || this, arguments);
  17995. };
  17996. },
  17997. attachTo: function(targetObj) {
  17998. var me = this,
  17999. target = (me.target = targetObj),
  18000. resizer = this.resizer,
  18001. imgPos = domUtils.getXY(target),
  18002. iframePos = domUtils.getXY(me.editor.iframe),
  18003. editorPos = domUtils.getXY(resizer.parentNode);
  18004. var doc = me.editor.document;
  18005. domUtils.setStyles(resizer, {
  18006. width: target.width + "px",
  18007. height: target.height + "px",
  18008. left:
  18009. iframePos.x +
  18010. imgPos.x -
  18011. (doc.documentElement.scrollLeft || doc.body.scrollLeft || 0) -
  18012. editorPos.x -
  18013. parseInt(resizer.style.borderLeftWidth) +
  18014. "px",
  18015. top:
  18016. iframePos.y +
  18017. imgPos.y -
  18018. (doc.documentElement.scrollTop || doc.body.scrollTop || 0) -
  18019. editorPos.y -
  18020. parseInt(resizer.style.borderTopWidth) +
  18021. "px"
  18022. });
  18023. }
  18024. };
  18025. })();
  18026. return function() {
  18027. var me = this,
  18028. imageScale;
  18029. me.setOpt("imageScaleEnabled", true);
  18030. if (!browser.ie && me.options.imageScaleEnabled) {
  18031. me.addListener("click", function(type, e) {
  18032. var range = me.selection.getRange(),
  18033. img = range.getClosedNode();
  18034. if (img && img.tagName == "IMG" && me.body.contentEditable != "false") {
  18035. if (
  18036. img.className.indexOf("edui-faked-music") != -1 ||
  18037. img.getAttribute("anchorname") ||
  18038. domUtils.hasClass(img, "loadingclass") ||
  18039. domUtils.hasClass(img, "loaderrorclass")
  18040. ) {
  18041. return;
  18042. }
  18043. if (!imageScale) {
  18044. imageScale = new Scale();
  18045. imageScale.init(me);
  18046. me.ui.getDom().appendChild(imageScale.resizer);
  18047. var _keyDownHandler = function(e) {
  18048. imageScale.hide();
  18049. if (imageScale.target)
  18050. me.selection.getRange().selectNode(imageScale.target).select();
  18051. },
  18052. _mouseDownHandler = function(e) {
  18053. var ele = e.target || e.srcElement;
  18054. if (
  18055. ele &&
  18056. (ele.className === undefined ||
  18057. ele.className.indexOf("edui-editor-imagescale") == -1)
  18058. ) {
  18059. _keyDownHandler(e);
  18060. }
  18061. },
  18062. timer;
  18063. me.addListener("afterscaleshow", function(e) {
  18064. me.addListener("beforekeydown", _keyDownHandler);
  18065. me.addListener("beforemousedown", _mouseDownHandler);
  18066. domUtils.on(document, "keydown", _keyDownHandler);
  18067. domUtils.on(document, "mousedown", _mouseDownHandler);
  18068. me.selection.getNative().removeAllRanges();
  18069. });
  18070. me.addListener("afterscalehide", function(e) {
  18071. me.removeListener("beforekeydown", _keyDownHandler);
  18072. me.removeListener("beforemousedown", _mouseDownHandler);
  18073. domUtils.un(document, "keydown", _keyDownHandler);
  18074. domUtils.un(document, "mousedown", _mouseDownHandler);
  18075. var target = imageScale.target;
  18076. if (target.parentNode) {
  18077. me.selection.getRange().selectNode(target).select();
  18078. }
  18079. });
  18080. //TODO 有iframe的情况,mousedown不能往下传。。
  18081. domUtils.on(imageScale.resizer, "mousedown", function(e) {
  18082. me.selection.getNative().removeAllRanges();
  18083. var ele = e.target || e.srcElement;
  18084. if (
  18085. ele &&
  18086. ele.className.indexOf("edui-editor-imagescale-hand") == -1
  18087. ) {
  18088. timer = setTimeout(function() {
  18089. imageScale.hide();
  18090. if (imageScale.target)
  18091. me.selection.getRange().selectNode(ele).select();
  18092. }, 200);
  18093. }
  18094. });
  18095. domUtils.on(imageScale.resizer, "mouseup", function(e) {
  18096. var ele = e.target || e.srcElement;
  18097. if (
  18098. ele &&
  18099. ele.className.indexOf("edui-editor-imagescale-hand") == -1
  18100. ) {
  18101. clearTimeout(timer);
  18102. }
  18103. });
  18104. }
  18105. imageScale.show(img);
  18106. } else {
  18107. if (imageScale && imageScale.resizer.style.display != "none")
  18108. imageScale.hide();
  18109. }
  18110. });
  18111. }
  18112. if (browser.webkit) {
  18113. me.addListener("click", function(type, e) {
  18114. if (e.target.tagName == "IMG" && me.body.contentEditable != "false") {
  18115. var range = new dom.Range(me.document);
  18116. range.selectNode(e.target).select();
  18117. }
  18118. });
  18119. }
  18120. };
  18121. })();
  18122. // plugins/autolink.js
  18123. ///import core
  18124. ///commands 为非ie浏览器自动添加a标签
  18125. ///commandsName AutoLink
  18126. ///commandsTitle 自动增加链接
  18127. /**
  18128. * @description 为非ie浏览器自动添加a标签
  18129. * @author zhanyi
  18130. */
  18131. UE.plugin.register(
  18132. "autolink",
  18133. function() {
  18134. var cont = 0;
  18135. return !browser.ie
  18136. ? {
  18137. bindEvents: {
  18138. reset: function() {
  18139. cont = 0;
  18140. },
  18141. keydown: function(type, evt) {
  18142. var me = this;
  18143. var keyCode = evt.keyCode || evt.which;
  18144. if (keyCode == 32 || keyCode == 13) {
  18145. var sel = me.selection.getNative(),
  18146. range = sel.getRangeAt(0).cloneRange(),
  18147. offset,
  18148. charCode;
  18149. var start = range.startContainer;
  18150. while (start.nodeType == 1 && range.startOffset > 0) {
  18151. start =
  18152. range.startContainer.childNodes[range.startOffset - 1];
  18153. if (!start) {
  18154. break;
  18155. }
  18156. range.setStart(
  18157. start,
  18158. start.nodeType == 1
  18159. ? start.childNodes.length
  18160. : start.nodeValue.length
  18161. );
  18162. range.collapse(true);
  18163. start = range.startContainer;
  18164. }
  18165. do {
  18166. if (range.startOffset == 0) {
  18167. start = range.startContainer.previousSibling;
  18168. while (start && start.nodeType == 1) {
  18169. start = start.lastChild;
  18170. }
  18171. if (!start || domUtils.isFillChar(start)) {
  18172. break;
  18173. }
  18174. offset = start.nodeValue.length;
  18175. } else {
  18176. start = range.startContainer;
  18177. offset = range.startOffset;
  18178. }
  18179. range.setStart(start, offset - 1);
  18180. charCode = range.toString().charCodeAt(0);
  18181. } while (charCode != 160 && charCode != 32);
  18182. if (
  18183. range
  18184. .toString()
  18185. .replace(new RegExp(domUtils.fillChar, "g"), "")
  18186. .match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)
  18187. ) {
  18188. while (range.toString().length) {
  18189. if (
  18190. /^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(
  18191. range.toString()
  18192. )
  18193. ) {
  18194. break;
  18195. }
  18196. try {
  18197. range.setStart(
  18198. range.startContainer,
  18199. range.startOffset + 1
  18200. );
  18201. } catch (e) {
  18202. //trace:2121
  18203. var start = range.startContainer;
  18204. while (!(next = start.nextSibling)) {
  18205. if (domUtils.isBody(start)) {
  18206. return;
  18207. }
  18208. start = start.parentNode;
  18209. }
  18210. range.setStart(next, 0);
  18211. }
  18212. }
  18213. //range的开始边界已经在a标签里的不再处理
  18214. if (
  18215. domUtils.findParentByTagName(
  18216. range.startContainer,
  18217. "a",
  18218. true
  18219. )
  18220. ) {
  18221. return;
  18222. }
  18223. var a = me.document.createElement("a"),
  18224. text = me.document.createTextNode(" "),
  18225. href;
  18226. me.undoManger && me.undoManger.save();
  18227. a.appendChild(range.extractContents());
  18228. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, "");
  18229. href = a
  18230. .getAttribute("href")
  18231. .replace(new RegExp(domUtils.fillChar, "g"), "");
  18232. href = /^(?:https?:\/\/)/gi.test(href)
  18233. ? href
  18234. : "http://" + href;
  18235. a.setAttribute("_src", utils.html(href));
  18236. a.href = utils.html(href);
  18237. range.insertNode(a);
  18238. a.parentNode.insertBefore(text, a.nextSibling);
  18239. range.setStart(text, 0);
  18240. range.collapse(true);
  18241. sel.removeAllRanges();
  18242. sel.addRange(range);
  18243. me.undoManger && me.undoManger.save();
  18244. }
  18245. }
  18246. }
  18247. }
  18248. }
  18249. : {};
  18250. },
  18251. function() {
  18252. var keyCodes = {
  18253. 37: 1,
  18254. 38: 1,
  18255. 39: 1,
  18256. 40: 1,
  18257. 13: 1,
  18258. 32: 1
  18259. };
  18260. function checkIsCludeLink(node) {
  18261. if (node.nodeType == 3) {
  18262. return null;
  18263. }
  18264. if (node.nodeName == "A") {
  18265. return node;
  18266. }
  18267. var lastChild = node.lastChild;
  18268. while (lastChild) {
  18269. if (lastChild.nodeName == "A") {
  18270. return lastChild;
  18271. }
  18272. if (lastChild.nodeType == 3) {
  18273. if (domUtils.isWhitespace(lastChild)) {
  18274. lastChild = lastChild.previousSibling;
  18275. continue;
  18276. }
  18277. return null;
  18278. }
  18279. lastChild = lastChild.lastChild;
  18280. }
  18281. }
  18282. browser.ie &&
  18283. this.addListener("keyup", function(cmd, evt) {
  18284. var me = this,
  18285. keyCode = evt.keyCode;
  18286. if (keyCodes[keyCode]) {
  18287. var rng = me.selection.getRange();
  18288. var start = rng.startContainer;
  18289. if (keyCode == 13) {
  18290. while (
  18291. start &&
  18292. !domUtils.isBody(start) &&
  18293. !domUtils.isBlockElm(start)
  18294. ) {
  18295. start = start.parentNode;
  18296. }
  18297. if (start && !domUtils.isBody(start) && start.nodeName == "P") {
  18298. var pre = start.previousSibling;
  18299. if (pre && pre.nodeType == 1) {
  18300. var pre = checkIsCludeLink(pre);
  18301. if (pre && !pre.getAttribute("_href")) {
  18302. domUtils.remove(pre, true);
  18303. }
  18304. }
  18305. }
  18306. } else if (keyCode == 32) {
  18307. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  18308. start = start.previousSibling;
  18309. if (
  18310. start &&
  18311. start.nodeName == "A" &&
  18312. !start.getAttribute("_href")
  18313. ) {
  18314. domUtils.remove(start, true);
  18315. }
  18316. }
  18317. } else {
  18318. start = domUtils.findParentByTagName(start, "a", true);
  18319. if (start && !start.getAttribute("_href")) {
  18320. var bk = rng.createBookmark();
  18321. domUtils.remove(start, true);
  18322. rng.moveToBookmark(bk).select(true);
  18323. }
  18324. }
  18325. }
  18326. });
  18327. }
  18328. );
  18329. // plugins/autoheight.js
  18330. ///import core
  18331. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  18332. ///commandsName AutoHeight,autoHeightEnabled
  18333. ///commandsTitle 自动增高
  18334. /**
  18335. * @description 自动伸展
  18336. * @author zhanyi
  18337. */
  18338. UE.plugins["autoheight"] = function() {
  18339. var me = this;
  18340. //提供开关,就算加载也可以关闭
  18341. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  18342. if (!me.autoHeightEnabled) {
  18343. return;
  18344. }
  18345. var bakOverflow,
  18346. lastHeight = 0,
  18347. options = me.options,
  18348. currentHeight,
  18349. timer;
  18350. function adjustHeight() {
  18351. var me = this;
  18352. clearTimeout(timer);
  18353. if (isFullscreen) return;
  18354. if (
  18355. !me.queryCommandState ||
  18356. (me.queryCommandState && me.queryCommandState("source") != 1)
  18357. ) {
  18358. timer = setTimeout(function() {
  18359. var node = me.body.lastChild;
  18360. while (node && node.nodeType != 1) {
  18361. node = node.previousSibling;
  18362. }
  18363. if (node && node.nodeType == 1) {
  18364. node.style.clear = "both";
  18365. currentHeight = Math.max(
  18366. domUtils.getXY(node).y + node.offsetHeight + 25,
  18367. Math.max(options.minFrameHeight, options.initialFrameHeight)
  18368. );
  18369. if (currentHeight != lastHeight) {
  18370. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  18371. me.iframe.parentNode.style.height = currentHeight + "px";
  18372. }
  18373. me.body.style.height = currentHeight + "px";
  18374. lastHeight = currentHeight;
  18375. }
  18376. domUtils.removeStyle(node, "clear");
  18377. }
  18378. }, 50);
  18379. }
  18380. }
  18381. var isFullscreen;
  18382. me.addListener("fullscreenchanged", function(cmd, f) {
  18383. isFullscreen = f;
  18384. });
  18385. me.addListener("destroy", function() {
  18386. domUtils.un(me.window, "scroll", fixedScrollTop);
  18387. me.removeListener(
  18388. "contentchange afterinserthtml keyup mouseup",
  18389. adjustHeight
  18390. );
  18391. });
  18392. me.enableAutoHeight = function() {
  18393. var me = this;
  18394. if (!me.autoHeightEnabled) {
  18395. return;
  18396. }
  18397. var doc = me.document;
  18398. me.autoHeightEnabled = true;
  18399. bakOverflow = doc.body.style.overflowY;
  18400. doc.body.style.overflowY = "hidden";
  18401. me.addListener("contentchange afterinserthtml keyup mouseup", adjustHeight);
  18402. //ff不给事件算得不对
  18403. setTimeout(function() {
  18404. adjustHeight.call(me);
  18405. }, browser.gecko ? 100 : 0);
  18406. me.fireEvent("autoheightchanged", me.autoHeightEnabled);
  18407. };
  18408. me.disableAutoHeight = function() {
  18409. me.body.style.overflowY = bakOverflow || "";
  18410. me.removeListener("contentchange", adjustHeight);
  18411. me.removeListener("keyup", adjustHeight);
  18412. me.removeListener("mouseup", adjustHeight);
  18413. me.autoHeightEnabled = false;
  18414. me.fireEvent("autoheightchanged", me.autoHeightEnabled);
  18415. };
  18416. me.on("setHeight", function() {
  18417. me.disableAutoHeight();
  18418. });
  18419. me.addListener("ready", function() {
  18420. me.enableAutoHeight();
  18421. //trace:1764
  18422. var timer;
  18423. domUtils.on(
  18424. browser.ie ? me.body : me.document,
  18425. browser.webkit ? "dragover" : "drop",
  18426. function() {
  18427. clearTimeout(timer);
  18428. timer = setTimeout(function() {
  18429. //trace:3681
  18430. adjustHeight.call(me);
  18431. }, 100);
  18432. }
  18433. );
  18434. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  18435. domUtils.on(me.window, "scroll", fixedScrollTop);
  18436. });
  18437. var lastScrollY;
  18438. function fixedScrollTop() {
  18439. if (!me.window) return;
  18440. if (lastScrollY === null) {
  18441. lastScrollY = me.window.scrollY;
  18442. } else if (me.window.scrollY == 0 && lastScrollY != 0) {
  18443. me.window.scrollTo(0, 0);
  18444. lastScrollY = null;
  18445. }
  18446. }
  18447. };
  18448. // plugins/autofloat.js
  18449. ///import core
  18450. ///commands 悬浮工具栏
  18451. ///commandsName AutoFloat,autoFloatEnabled
  18452. ///commandsTitle 悬浮工具栏
  18453. /**
  18454. * modified by chengchao01
  18455. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  18456. */
  18457. UE.plugins["autofloat"] = function() {
  18458. var me = this,
  18459. lang = me.getLang();
  18460. me.setOpt({
  18461. topOffset: 0
  18462. });
  18463. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  18464. topOffset = me.options.topOffset;
  18465. //如果不固定toolbar的位置,则直接退出
  18466. if (!optsAutoFloatEnabled) {
  18467. return;
  18468. }
  18469. var uiUtils = UE.ui.uiUtils,
  18470. LteIE6 = browser.ie && browser.version <= 6,
  18471. quirks = browser.quirks;
  18472. function checkHasUI() {
  18473. if (!UE.ui) {
  18474. alert(lang.autofloatMsg);
  18475. return 0;
  18476. }
  18477. return 1;
  18478. }
  18479. function fixIE6FixedPos() {
  18480. var docStyle = document.body.style;
  18481. docStyle.backgroundImage = 'url("about:blank")';
  18482. docStyle.backgroundAttachment = "fixed";
  18483. }
  18484. var bakCssText,
  18485. placeHolder = document.createElement("div"),
  18486. toolbarBox,
  18487. orgTop,
  18488. getPosition,
  18489. flag = true; //ie7模式下需要偏移
  18490. function setFloating() {
  18491. var toobarBoxPos = domUtils.getXY(toolbarBox),
  18492. origalFloat = domUtils.getComputedStyle(toolbarBox, "position"),
  18493. origalLeft = domUtils.getComputedStyle(toolbarBox, "left");
  18494. toolbarBox.style.width = toolbarBox.offsetWidth + "px";
  18495. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  18496. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  18497. if (LteIE6 || (quirks && browser.ie)) {
  18498. if (toolbarBox.style.position != "absolute") {
  18499. toolbarBox.style.position = "absolute";
  18500. }
  18501. toolbarBox.style.top =
  18502. (document.body.scrollTop || document.documentElement.scrollTop) -
  18503. orgTop +
  18504. topOffset +
  18505. "px";
  18506. } else {
  18507. if (browser.ie7Compat && flag) {
  18508. flag = false;
  18509. toolbarBox.style.left =
  18510. domUtils.getXY(toolbarBox).x -
  18511. document.documentElement.getBoundingClientRect().left +
  18512. 2 +
  18513. "px";
  18514. }
  18515. if (toolbarBox.style.position != "fixed") {
  18516. toolbarBox.style.position = "fixed";
  18517. toolbarBox.style.top = topOffset + "px";
  18518. (origalFloat == "absolute" || origalFloat == "relative") &&
  18519. parseFloat(origalLeft) &&
  18520. (toolbarBox.style.left = toobarBoxPos.x + "px");
  18521. }
  18522. }
  18523. }
  18524. function unsetFloating() {
  18525. flag = true;
  18526. if (placeHolder.parentNode) {
  18527. placeHolder.parentNode.removeChild(placeHolder);
  18528. }
  18529. toolbarBox.style.cssText = bakCssText;
  18530. }
  18531. function updateFloating() {
  18532. var rect3 = getPosition(me.container);
  18533. var offset = me.options.toolbarTopOffset || 0;
  18534. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  18535. setFloating();
  18536. } else {
  18537. unsetFloating();
  18538. }
  18539. }
  18540. var defer_updateFloating = utils.defer(
  18541. function() {
  18542. updateFloating();
  18543. },
  18544. browser.ie ? 200 : 100,
  18545. true
  18546. );
  18547. me.addListener("destroy", function() {
  18548. domUtils.un(window, ["scroll", "resize"], updateFloating);
  18549. me.removeListener("keydown", defer_updateFloating);
  18550. //适用于在DIV scrollbox中滚动,但页面不滚动的浮动toolbar
  18551. var scrollBox = document.getElementById("scrollBox");
  18552. if (scrollBox) {
  18553. domUtils.un(scrollBox, ['scroll','resize'], updateFloating);
  18554. }
  18555. });
  18556. me.addListener("ready", function() {
  18557. if (checkHasUI(me)) {
  18558. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  18559. if (!me.ui) {
  18560. return;
  18561. }
  18562. getPosition = uiUtils.getClientRect;
  18563. toolbarBox = me.ui.getDom("toolbarbox");
  18564. orgTop = getPosition(toolbarBox).top;
  18565. bakCssText = toolbarBox.style.cssText;
  18566. placeHolder.style.height = me.ui.getDom("iframeholder").offsetHeight + "px";
  18567. if (LteIE6) {
  18568. fixIE6FixedPos();
  18569. }
  18570. domUtils.on(window, ["scroll", "resize"], updateFloating);
  18571. me.addListener("keydown", defer_updateFloating);
  18572. //适用于在DIV scrollbox中滚动,但页面不滚动的浮动toolbar
  18573. var scrollBox = document.getElementById("scrollBox");
  18574. if (scrollBox) {
  18575. domUtils.on(scrollBox, ['scroll','resize'], updateFloating);
  18576. }
  18577. me.addListener("beforefullscreenchange", function(t, enabled) {
  18578. if (enabled) {
  18579. unsetFloating();
  18580. }
  18581. });
  18582. me.addListener("fullscreenchanged", function(t, enabled) {
  18583. if (!enabled) {
  18584. updateFloating();
  18585. }
  18586. });
  18587. me.addListener("sourcemodechanged", function(t, enabled) {
  18588. setTimeout(function() {
  18589. updateFloating();
  18590. }, 0);
  18591. });
  18592. me.addListener("clearDoc", function() {
  18593. setTimeout(function() {
  18594. updateFloating();
  18595. }, 0);
  18596. });
  18597. }
  18598. });
  18599. };
  18600. // plugins/video.js
  18601. /**
  18602. * video插件, 为UEditor提供视频插入支持
  18603. * @file
  18604. * @since 1.2.6.1
  18605. */
  18606. UE.plugins["video"] = function() {
  18607. var me = this;
  18608. /**
  18609. * 创建插入视频字符窜
  18610. * @param url 视频地址
  18611. * @param width 视频宽度
  18612. * @param height 视频高度
  18613. * @param align 视频对齐
  18614. * @param toEmbed 是否以flash代替显示
  18615. * @param addParagraph 是否需要添加P 标签
  18616. */
  18617. function creatInsertStr(url, width, height, id, align, classname, type) {
  18618. var str;
  18619. switch (type) {
  18620. case "image":
  18621. str =
  18622. "<img " +
  18623. (id ? 'id="' + id + '"' : "") +
  18624. ' width="' +
  18625. width +
  18626. '" height="' +
  18627. height +
  18628. '" _url="' +
  18629. url +
  18630. '" class="' +
  18631. classname.replace(/\bvideo-js\b/, "") +
  18632. '"' +
  18633. ' src="' +
  18634. me.options.UEDITOR_HOME_URL +
  18635. 'themes/default/images/spacer.gif" style="background:url(' +
  18636. me.options.UEDITOR_HOME_URL +
  18637. "themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;" +
  18638. (align ? "float:" + align + ";" : "") +
  18639. '" />';
  18640. break;
  18641. case "embed":
  18642. str =
  18643. '<embed type="application/x-shockwave-flash" class="' +
  18644. classname +
  18645. '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  18646. ' src="' +
  18647. utils.html(url) +
  18648. '" width="' +
  18649. width +
  18650. '" height="' +
  18651. height +
  18652. '"' +
  18653. (align ? ' style="float:' + align + '"' : "") +
  18654. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  18655. break;
  18656. case "video":
  18657. var ext = url.substr(url.lastIndexOf(".") + 1);
  18658. if (ext == "ogv") ext = "ogg";
  18659. str =
  18660. "<video" +
  18661. (id ? ' id="' + id + '"' : "") +
  18662. ' class="' +
  18663. classname +
  18664. ' video-js" ' +
  18665. (align ? ' style="float:' + align + '"' : "") +
  18666. ' controls preload="none" width="' +
  18667. width +
  18668. '" height="' +
  18669. height +
  18670. '" src="' +
  18671. url +
  18672. '" data-setup="{}">' +
  18673. '<source src="' +
  18674. url +
  18675. '" type="video/' +
  18676. ext +
  18677. '" /></video>';
  18678. break;
  18679. }
  18680. return str;
  18681. }
  18682. function switchImgAndVideo(root, img2video) {
  18683. utils.each(
  18684. root.getNodesByTagName(img2video ? "img" : "embed video"),
  18685. function(node) {
  18686. var className = node.getAttr("class");
  18687. if (className && className.indexOf("edui-faked-video") != -1) {
  18688. var html = creatInsertStr(
  18689. img2video ? node.getAttr("_url") : node.getAttr("src"),
  18690. node.getAttr("width"),
  18691. node.getAttr("height"),
  18692. null,
  18693. node.getStyle("float") || "",
  18694. className,
  18695. img2video ? "embed" : "image"
  18696. );
  18697. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  18698. }
  18699. if (className && className.indexOf("edui-upload-video") != -1) {
  18700. var html = creatInsertStr(
  18701. img2video ? node.getAttr("_url") : node.getAttr("src"),
  18702. node.getAttr("width"),
  18703. node.getAttr("height"),
  18704. null,
  18705. node.getStyle("float") || "",
  18706. className,
  18707. img2video ? "video" : "image"
  18708. );
  18709. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  18710. }
  18711. }
  18712. );
  18713. }
  18714. me.addOutputRule(function(root) {
  18715. switchImgAndVideo(root, true);
  18716. });
  18717. me.addInputRule(function(root) {
  18718. switchImgAndVideo(root);
  18719. });
  18720. /**
  18721. * 插入视频
  18722. * @command insertvideo
  18723. * @method execCommand
  18724. * @param { String } cmd 命令字符串
  18725. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  18726. * @example
  18727. * ```javascript
  18728. *
  18729. * var videoAttr = {
  18730. * //视频地址
  18731. * url: 'http://www.youku.com/xxx',
  18732. * //视频宽高值, 单位px
  18733. * width: 200,
  18734. * height: 100
  18735. * };
  18736. *
  18737. * //editor 是编辑器实例
  18738. * //向编辑器插入单个视频
  18739. * editor.execCommand( 'insertvideo', videoAttr );
  18740. * ```
  18741. */
  18742. /**
  18743. * 插入视频
  18744. * @command insertvideo
  18745. * @method execCommand
  18746. * @param { String } cmd 命令字符串
  18747. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  18748. * @example
  18749. * ```javascript
  18750. *
  18751. * var videoAttr1 = {
  18752. * //视频地址
  18753. * url: 'http://www.youku.com/xxx',
  18754. * //视频宽高值, 单位px
  18755. * width: 200,
  18756. * height: 100
  18757. * },
  18758. * videoAttr2 = {
  18759. * //视频地址
  18760. * url: 'http://www.youku.com/xxx',
  18761. * //视频宽高值, 单位px
  18762. * width: 200,
  18763. * height: 100
  18764. * }
  18765. *
  18766. * //editor 是编辑器实例
  18767. * //该方法将会向编辑器内插入两个视频
  18768. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  18769. * ```
  18770. */
  18771. /**
  18772. * 查询当前光标所在处是否是一个视频
  18773. * @command insertvideo
  18774. * @method queryCommandState
  18775. * @param { String } cmd 需要查询的命令字符串
  18776. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  18777. * @example
  18778. * ```javascript
  18779. *
  18780. * //editor 是编辑器实例
  18781. * editor.queryCommandState( 'insertvideo' );
  18782. * ```
  18783. */
  18784. me.commands["insertvideo"] = {
  18785. execCommand: function(cmd, videoObjs, type) {
  18786. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs];
  18787. if (me.fireEvent("beforeinsertvideo", videoObjs) === true) {
  18788. return;
  18789. }
  18790. var html = [],
  18791. id = "tmpVedio",
  18792. cl;
  18793. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  18794. vi = videoObjs[i];
  18795. cl = type == "upload"
  18796. ? "edui-upload-video video-js vjs-default-skin"
  18797. : "edui-faked-video";
  18798. html.push(
  18799. creatInsertStr(
  18800. vi.url,
  18801. vi.width || 420,
  18802. vi.height || 280,
  18803. id + i,
  18804. null,
  18805. cl,
  18806. "image"
  18807. )
  18808. );
  18809. }
  18810. me.execCommand("inserthtml", html.join(""), true);
  18811. var rng = this.selection.getRange();
  18812. for (var i = 0, len = videoObjs.length; i < len; i++) {
  18813. var img = this.document.getElementById("tmpVedio" + i);
  18814. domUtils.removeAttributes(img, "id");
  18815. rng.selectNode(img).select();
  18816. me.execCommand("imagefloat", videoObjs[i].align);
  18817. }
  18818. me.fireEvent("afterinsertvideo", videoObjs);
  18819. },
  18820. queryCommandState: function() {
  18821. var img = me.selection.getRange().getClosedNode(),
  18822. flag =
  18823. img &&
  18824. (img.className == "edui-faked-video" ||
  18825. img.className.indexOf("edui-upload-video") != -1);
  18826. return flag ? 1 : 0;
  18827. }
  18828. };
  18829. };
  18830. // plugins/table.core.js
  18831. /**
  18832. * Created with JetBrains WebStorm.
  18833. * User: taoqili
  18834. * Date: 13-1-18
  18835. * Time: 上午11:09
  18836. * To change this template use File | Settings | File Templates.
  18837. */
  18838. /**
  18839. * UE表格操作类
  18840. * @param table
  18841. * @constructor
  18842. */
  18843. ;(function() {
  18844. var UETable = (UE.UETable = function(table) {
  18845. this.table = table;
  18846. this.indexTable = [];
  18847. this.selectedTds = [];
  18848. this.cellsRange = {};
  18849. this.update(table);
  18850. });
  18851. //===以下为静态工具方法===
  18852. UETable.removeSelectedClass = function(cells) {
  18853. utils.each(cells, function(cell) {
  18854. domUtils.removeClasses(cell, "selectTdClass");
  18855. });
  18856. };
  18857. UETable.addSelectedClass = function(cells) {
  18858. utils.each(cells, function(cell) {
  18859. domUtils.addClass(cell, "selectTdClass");
  18860. });
  18861. };
  18862. UETable.isEmptyBlock = function(node) {
  18863. var reg = new RegExp(domUtils.fillChar, "g");
  18864. if (
  18865. node[browser.ie ? "innerText" : "textContent"]
  18866. .replace(/^\s*$/, "")
  18867. .replace(reg, "").length > 0
  18868. ) {
  18869. return 0;
  18870. }
  18871. for (var i in dtd.$isNotEmpty)
  18872. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  18873. if (node.getElementsByTagName(i).length) {
  18874. return 0;
  18875. }
  18876. }
  18877. return 1;
  18878. };
  18879. UETable.getWidth = function(cell) {
  18880. if (!cell) return 0;
  18881. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  18882. };
  18883. /**
  18884. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  18885. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  18886. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  18887. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  18888. */
  18889. UETable.getTableCellAlignState = function(cells) {
  18890. !utils.isArray(cells) && (cells = [cells]);
  18891. var result = {},
  18892. status = ["align", "valign"],
  18893. tempStatus = null,
  18894. isSame = true; //状态是否相同
  18895. utils.each(cells, function(cellNode) {
  18896. utils.each(status, function(currentState) {
  18897. tempStatus = cellNode.getAttribute(currentState);
  18898. if (!result[currentState] && tempStatus) {
  18899. result[currentState] = tempStatus;
  18900. } else if (
  18901. !result[currentState] ||
  18902. tempStatus !== result[currentState]
  18903. ) {
  18904. isSame = false;
  18905. return false;
  18906. }
  18907. });
  18908. return isSame;
  18909. });
  18910. return isSame ? result : null;
  18911. };
  18912. /**
  18913. * 根据当前选区获取相关的table信息
  18914. * @return {Object}
  18915. */
  18916. UETable.getTableItemsByRange = function(editor) {
  18917. var start = editor.selection.getStart();
  18918. //ff下会选中bookmark
  18919. if (
  18920. start &&
  18921. start.id &&
  18922. start.id.indexOf("_baidu_bookmark_start_") === 0 &&
  18923. start.nextSibling
  18924. ) {
  18925. start = start.nextSibling;
  18926. }
  18927. //在table或者td边缘有可能存在选中tr的情况
  18928. var cell = start && domUtils.findParentByTagName(start, ["td", "th"], true),
  18929. tr = cell && cell.parentNode,
  18930. table = tr && domUtils.findParentByTagName(tr, ["table"]),
  18931. caption = table && table.getElementsByTagName("caption")[0];
  18932. return {
  18933. cell: cell,
  18934. tr: tr,
  18935. table: table,
  18936. caption: caption
  18937. };
  18938. };
  18939. UETable.getUETableBySelected = function(editor) {
  18940. var table = UETable.getTableItemsByRange(editor).table;
  18941. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  18942. return table.ueTable;
  18943. }
  18944. return null;
  18945. };
  18946. UETable.getDefaultValue = function(editor, table) {
  18947. var borderMap = {
  18948. thin: "0px",
  18949. medium: "1px",
  18950. thick: "2px"
  18951. },
  18952. tableBorder,
  18953. tdPadding,
  18954. tdBorder,
  18955. tmpValue;
  18956. if (!table) {
  18957. table = editor.document.createElement("table");
  18958. table.insertRow(0).insertCell(0).innerHTML = "xxx";
  18959. editor.body.appendChild(table);
  18960. var td = table.getElementsByTagName("td")[0];
  18961. tmpValue = domUtils.getComputedStyle(table, "border-left-width");
  18962. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18963. tmpValue = domUtils.getComputedStyle(td, "padding-left");
  18964. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18965. tmpValue = domUtils.getComputedStyle(td, "border-left-width");
  18966. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18967. domUtils.remove(table);
  18968. return {
  18969. tableBorder: tableBorder,
  18970. tdPadding: tdPadding,
  18971. tdBorder: tdBorder
  18972. };
  18973. } else {
  18974. td = table.getElementsByTagName("td")[0];
  18975. tmpValue = domUtils.getComputedStyle(table, "border-left-width");
  18976. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18977. tmpValue = domUtils.getComputedStyle(td, "padding-left");
  18978. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18979. tmpValue = domUtils.getComputedStyle(td, "border-left-width");
  18980. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18981. return {
  18982. tableBorder: tableBorder,
  18983. tdPadding: tdPadding,
  18984. tdBorder: tdBorder
  18985. };
  18986. }
  18987. };
  18988. /**
  18989. * 根据当前点击的td或者table获取索引对象
  18990. * @param tdOrTable
  18991. */
  18992. UETable.getUETable = function(tdOrTable) {
  18993. var tag = tdOrTable.tagName.toLowerCase();
  18994. tdOrTable = tag == "td" || tag == "th" || tag == "caption"
  18995. ? domUtils.findParentByTagName(tdOrTable, "table", true)
  18996. : tdOrTable;
  18997. if (!tdOrTable.ueTable) {
  18998. tdOrTable.ueTable = new UETable(tdOrTable);
  18999. }
  19000. return tdOrTable.ueTable;
  19001. };
  19002. UETable.cloneCell = function(cell, ignoreMerge, keepPro) {
  19003. if (!cell || utils.isString(cell)) {
  19004. return this.table.ownerDocument.createElement(cell || "td");
  19005. }
  19006. var flag = domUtils.hasClass(cell, "selectTdClass");
  19007. flag && domUtils.removeClasses(cell, "selectTdClass");
  19008. var tmpCell = cell.cloneNode(true);
  19009. if (ignoreMerge) {
  19010. tmpCell.rowSpan = tmpCell.colSpan = 1;
  19011. }
  19012. //去掉宽高
  19013. !keepPro && domUtils.removeAttributes(tmpCell, "width height");
  19014. !keepPro && domUtils.removeAttributes(tmpCell, "style");
  19015. tmpCell.style.borderLeftStyle = "";
  19016. tmpCell.style.borderTopStyle = "";
  19017. tmpCell.style.borderLeftColor = cell.style.borderRightColor;
  19018. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth;
  19019. tmpCell.style.borderTopColor = cell.style.borderBottomColor;
  19020. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth;
  19021. flag && domUtils.addClass(cell, "selectTdClass");
  19022. return tmpCell;
  19023. };
  19024. UETable.prototype = {
  19025. getMaxRows: function() {
  19026. var rows = this.table.rows,
  19027. maxLen = 1;
  19028. for (var i = 0, row; (row = rows[i]); i++) {
  19029. var currentMax = 1;
  19030. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19031. currentMax = Math.max(cj.rowSpan || 1, currentMax);
  19032. }
  19033. maxLen = Math.max(currentMax + i, maxLen);
  19034. }
  19035. return maxLen;
  19036. },
  19037. /**
  19038. * 获取当前表格的最大列数
  19039. */
  19040. getMaxCols: function() {
  19041. var rows = this.table.rows,
  19042. maxLen = 0,
  19043. cellRows = {};
  19044. for (var i = 0, row; (row = rows[i]); i++) {
  19045. var cellsNum = 0;
  19046. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19047. cellsNum += cj.colSpan || 1;
  19048. if (cj.rowSpan && cj.rowSpan > 1) {
  19049. for (var k = 1; k < cj.rowSpan; k++) {
  19050. if (!cellRows["row_" + (i + k)]) {
  19051. cellRows["row_" + (i + k)] = cj.colSpan || 1;
  19052. } else {
  19053. cellRows["row_" + (i + k)]++;
  19054. }
  19055. }
  19056. }
  19057. }
  19058. cellsNum += cellRows["row_" + i] || 0;
  19059. maxLen = Math.max(cellsNum, maxLen);
  19060. }
  19061. return maxLen;
  19062. },
  19063. getCellColIndex: function(cell) {},
  19064. /**
  19065. * 获取当前cell旁边的单元格,
  19066. * @param cell
  19067. * @param right
  19068. */
  19069. getHSideCell: function(cell, right) {
  19070. try {
  19071. var cellInfo = this.getCellInfo(cell),
  19072. previewRowIndex,
  19073. previewColIndex;
  19074. var len = this.selectedTds.length,
  19075. range = this.cellsRange;
  19076. //首行或者首列没有前置单元格
  19077. if (
  19078. (!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19079. (right &&
  19080. (!len
  19081. ? cellInfo.colIndex == this.colsNum - 1
  19082. : range.endColIndex == this.colsNum - 1))
  19083. )
  19084. return null;
  19085. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex;
  19086. previewColIndex = !right
  19087. ? !len
  19088. ? cellInfo.colIndex < 1 ? 0 : cellInfo.colIndex - 1
  19089. : range.beginColIndex - 1
  19090. : !len ? cellInfo.colIndex + 1 : range.endColIndex + 1;
  19091. return this.getCell(
  19092. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19093. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19094. );
  19095. } catch (e) {
  19096. showError(e);
  19097. }
  19098. },
  19099. getTabNextCell: function(cell, preRowIndex) {
  19100. var cellInfo = this.getCellInfo(cell),
  19101. rowIndex = preRowIndex || cellInfo.rowIndex,
  19102. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  19103. nextCell;
  19104. try {
  19105. nextCell = this.getCell(
  19106. this.indexTable[rowIndex][colIndex].rowIndex,
  19107. this.indexTable[rowIndex][colIndex].cellIndex
  19108. );
  19109. } catch (e) {
  19110. try {
  19111. rowIndex = rowIndex * 1 + 1;
  19112. colIndex = 0;
  19113. nextCell = this.getCell(
  19114. this.indexTable[rowIndex][colIndex].rowIndex,
  19115. this.indexTable[rowIndex][colIndex].cellIndex
  19116. );
  19117. } catch (e) {}
  19118. }
  19119. return nextCell;
  19120. },
  19121. /**
  19122. * 获取视觉上的后置单元格
  19123. * @param cell
  19124. * @param bottom
  19125. */
  19126. getVSideCell: function(cell, bottom, ignoreRange) {
  19127. try {
  19128. var cellInfo = this.getCellInfo(cell),
  19129. nextRowIndex,
  19130. nextColIndex;
  19131. var len = this.selectedTds.length && !ignoreRange,
  19132. range = this.cellsRange;
  19133. //末行或者末列没有后置单元格
  19134. if (
  19135. (!bottom && cellInfo.rowIndex == 0) ||
  19136. (bottom &&
  19137. (!len
  19138. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19139. : range.endRowIndex == this.rowsNum - 1))
  19140. )
  19141. return null;
  19142. nextRowIndex = !bottom
  19143. ? !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1
  19144. : !len ? cellInfo.rowIndex + cellInfo.rowSpan : range.endRowIndex + 1;
  19145. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  19146. return this.getCell(
  19147. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19148. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19149. );
  19150. } catch (e) {
  19151. showError(e);
  19152. }
  19153. },
  19154. /**
  19155. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  19156. */
  19157. getSameEndPosCells: function(cell, xOrY) {
  19158. try {
  19159. var flag = xOrY.toLowerCase() === "x",
  19160. end =
  19161. domUtils.getXY(cell)[flag ? "x" : "y"] +
  19162. cell["offset" + (flag ? "Width" : "Height")],
  19163. rows = this.table.rows,
  19164. cells = null,
  19165. returns = [];
  19166. for (var i = 0; i < this.rowsNum; i++) {
  19167. cells = rows[i].cells;
  19168. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19169. var tmpEnd =
  19170. domUtils.getXY(tmpCell)[flag ? "x" : "y"] +
  19171. tmpCell["offset" + (flag ? "Width" : "Height")];
  19172. //对应行的td已经被上面行rowSpan了
  19173. if (tmpEnd > end && flag) break;
  19174. if (cell == tmpCell || end == tmpEnd) {
  19175. //只获取单一的单元格
  19176. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  19177. if (tmpCell[flag ? "colSpan" : "rowSpan"] == 1) {
  19178. returns.push(tmpCell);
  19179. }
  19180. if (flag) break;
  19181. }
  19182. }
  19183. }
  19184. return returns;
  19185. } catch (e) {
  19186. showError(e);
  19187. }
  19188. },
  19189. setCellContent: function(cell, content) {
  19190. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : "<br />");
  19191. },
  19192. cloneCell: UETable.cloneCell,
  19193. /**
  19194. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  19195. */
  19196. getSameStartPosXCells: function(cell) {
  19197. try {
  19198. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  19199. rows = this.table.rows,
  19200. cells,
  19201. returns = [];
  19202. for (var i = 0; i < this.rowsNum; i++) {
  19203. cells = rows[i].cells;
  19204. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19205. var tmpStart = domUtils.getXY(tmpCell).x;
  19206. if (tmpStart > start) break;
  19207. if (tmpStart == start && tmpCell.colSpan == 1) {
  19208. returns.push(tmpCell);
  19209. break;
  19210. }
  19211. }
  19212. }
  19213. return returns;
  19214. } catch (e) {
  19215. showError(e);
  19216. }
  19217. },
  19218. /**
  19219. * 更新table对应的索引表
  19220. */
  19221. update: function(table) {
  19222. this.table = table || this.table;
  19223. this.selectedTds = [];
  19224. this.cellsRange = {};
  19225. this.indexTable = [];
  19226. var rows = this.table.rows,
  19227. rowsNum = this.getMaxRows(),
  19228. dNum = rowsNum - rows.length,
  19229. colsNum = this.getMaxCols();
  19230. while (dNum--) {
  19231. this.table.insertRow(rows.length);
  19232. }
  19233. this.rowsNum = rowsNum;
  19234. this.colsNum = colsNum;
  19235. for (var i = 0, len = rows.length; i < len; i++) {
  19236. this.indexTable[i] = new Array(colsNum);
  19237. }
  19238. //填充索引表
  19239. for (var rowIndex = 0, row; (row = rows[rowIndex]); rowIndex++) {
  19240. for (
  19241. var cellIndex = 0, cell, cells = row.cells;
  19242. (cell = cells[cellIndex]);
  19243. cellIndex++
  19244. ) {
  19245. //修正整行被rowSpan时导致的行数计算错误
  19246. if (cell.rowSpan > rowsNum) {
  19247. cell.rowSpan = rowsNum;
  19248. }
  19249. var colIndex = cellIndex,
  19250. rowSpan = cell.rowSpan || 1,
  19251. colSpan = cell.colSpan || 1;
  19252. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  19253. while (this.indexTable[rowIndex][colIndex]) colIndex++;
  19254. for (var j = 0; j < rowSpan; j++) {
  19255. for (var k = 0; k < colSpan; k++) {
  19256. this.indexTable[rowIndex + j][colIndex + k] = {
  19257. rowIndex: rowIndex,
  19258. cellIndex: cellIndex,
  19259. colIndex: colIndex,
  19260. rowSpan: rowSpan,
  19261. colSpan: colSpan
  19262. };
  19263. }
  19264. }
  19265. }
  19266. }
  19267. //修复残缺td
  19268. for (j = 0; j < rowsNum; j++) {
  19269. for (k = 0; k < colsNum; k++) {
  19270. if (this.indexTable[j][k] === undefined) {
  19271. row = rows[j];
  19272. cell = row.cells[row.cells.length - 1];
  19273. cell = cell
  19274. ? cell.cloneNode(true)
  19275. : this.table.ownerDocument.createElement("td");
  19276. this.setCellContent(cell);
  19277. if (cell.colSpan !== 1) cell.colSpan = 1;
  19278. if (cell.rowSpan !== 1) cell.rowSpan = 1;
  19279. row.appendChild(cell);
  19280. this.indexTable[j][k] = {
  19281. rowIndex: j,
  19282. cellIndex: cell.cellIndex,
  19283. colIndex: k,
  19284. rowSpan: 1,
  19285. colSpan: 1
  19286. };
  19287. }
  19288. }
  19289. }
  19290. //当框选后删除行或者列后撤销,需要重建选区。
  19291. var tds = domUtils.getElementsByTagName(this.table, "td"),
  19292. selectTds = [];
  19293. utils.each(tds, function(td) {
  19294. if (domUtils.hasClass(td, "selectTdClass")) {
  19295. selectTds.push(td);
  19296. }
  19297. });
  19298. if (selectTds.length) {
  19299. var start = selectTds[0],
  19300. end = selectTds[selectTds.length - 1],
  19301. startInfo = this.getCellInfo(start),
  19302. endInfo = this.getCellInfo(end);
  19303. this.selectedTds = selectTds;
  19304. this.cellsRange = {
  19305. beginRowIndex: startInfo.rowIndex,
  19306. beginColIndex: startInfo.colIndex,
  19307. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  19308. endColIndex: endInfo.colIndex + endInfo.colSpan - 1
  19309. };
  19310. }
  19311. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  19312. if (!domUtils.hasClass(this.table.rows[0], "firstRow")) {
  19313. domUtils.addClass(this.table.rows[0], "firstRow");
  19314. for (var i = 1; i < this.table.rows.length; i++) {
  19315. domUtils.removeClasses(this.table.rows[i], "firstRow");
  19316. }
  19317. }
  19318. },
  19319. /**
  19320. * 获取单元格的索引信息
  19321. */
  19322. getCellInfo: function(cell) {
  19323. if (!cell) return;
  19324. var cellIndex = cell.cellIndex,
  19325. rowIndex = cell.parentNode.rowIndex,
  19326. rowInfo = this.indexTable[rowIndex],
  19327. numCols = this.colsNum;
  19328. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  19329. var cellInfo = rowInfo[colIndex];
  19330. if (
  19331. cellInfo.rowIndex === rowIndex &&
  19332. cellInfo.cellIndex === cellIndex
  19333. ) {
  19334. return cellInfo;
  19335. }
  19336. }
  19337. },
  19338. /**
  19339. * 根据行列号获取单元格
  19340. */
  19341. getCell: function(rowIndex, cellIndex) {
  19342. return (
  19343. (rowIndex < this.rowsNum &&
  19344. this.table.rows[rowIndex].cells[cellIndex]) ||
  19345. null
  19346. );
  19347. },
  19348. /**
  19349. * 删除单元格
  19350. */
  19351. deleteCell: function(cell, rowIndex) {
  19352. rowIndex = typeof rowIndex == "number"
  19353. ? rowIndex
  19354. : cell.parentNode.rowIndex;
  19355. var row = this.table.rows[rowIndex];
  19356. row.deleteCell(cell.cellIndex);
  19357. },
  19358. /**
  19359. * 根据始末两个单元格获取被框选的所有单元格范围
  19360. */
  19361. getCellsRange: function(cellA, cellB) {
  19362. function checkRange(
  19363. beginRowIndex,
  19364. beginColIndex,
  19365. endRowIndex,
  19366. endColIndex
  19367. ) {
  19368. var tmpBeginRowIndex = beginRowIndex,
  19369. tmpBeginColIndex = beginColIndex,
  19370. tmpEndRowIndex = endRowIndex,
  19371. tmpEndColIndex = endColIndex,
  19372. cellInfo,
  19373. colIndex,
  19374. rowIndex;
  19375. // 通过indexTable检查是否存在超出TableRange上边界的情况
  19376. if (beginRowIndex > 0) {
  19377. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19378. cellInfo = me.indexTable[beginRowIndex][colIndex];
  19379. rowIndex = cellInfo.rowIndex;
  19380. if (rowIndex < beginRowIndex) {
  19381. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex);
  19382. }
  19383. }
  19384. }
  19385. // 通过indexTable检查是否存在超出TableRange右边界的情况
  19386. if (endColIndex < me.colsNum) {
  19387. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19388. cellInfo = me.indexTable[rowIndex][endColIndex];
  19389. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1;
  19390. if (colIndex > endColIndex) {
  19391. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex);
  19392. }
  19393. }
  19394. }
  19395. // 检查是否有超出TableRange下边界的情况
  19396. if (endRowIndex < me.rowsNum) {
  19397. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19398. cellInfo = me.indexTable[endRowIndex][colIndex];
  19399. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1;
  19400. if (rowIndex > endRowIndex) {
  19401. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex);
  19402. }
  19403. }
  19404. }
  19405. // 检查是否有超出TableRange左边界的情况
  19406. if (beginColIndex > 0) {
  19407. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19408. cellInfo = me.indexTable[rowIndex][beginColIndex];
  19409. colIndex = cellInfo.colIndex;
  19410. if (colIndex < beginColIndex) {
  19411. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex);
  19412. }
  19413. }
  19414. }
  19415. //递归调用直至所有完成所有框选单元格的扩展
  19416. if (
  19417. tmpBeginRowIndex != beginRowIndex ||
  19418. tmpBeginColIndex != beginColIndex ||
  19419. tmpEndRowIndex != endRowIndex ||
  19420. tmpEndColIndex != endColIndex
  19421. ) {
  19422. return checkRange(
  19423. tmpBeginRowIndex,
  19424. tmpBeginColIndex,
  19425. tmpEndRowIndex,
  19426. tmpEndColIndex
  19427. );
  19428. } else {
  19429. // 不需要扩展TableRange的情况
  19430. return {
  19431. beginRowIndex: beginRowIndex,
  19432. beginColIndex: beginColIndex,
  19433. endRowIndex: endRowIndex,
  19434. endColIndex: endColIndex
  19435. };
  19436. }
  19437. }
  19438. try {
  19439. var me = this,
  19440. cellAInfo = me.getCellInfo(cellA);
  19441. if (cellA === cellB) {
  19442. return {
  19443. beginRowIndex: cellAInfo.rowIndex,
  19444. beginColIndex: cellAInfo.colIndex,
  19445. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19446. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1
  19447. };
  19448. }
  19449. var cellBInfo = me.getCellInfo(cellB);
  19450. // 计算TableRange的四个边
  19451. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  19452. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  19453. endRowIndex = Math.max(
  19454. cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19455. cellBInfo.rowIndex + cellBInfo.rowSpan - 1
  19456. ),
  19457. endColIndex = Math.max(
  19458. cellAInfo.colIndex + cellAInfo.colSpan - 1,
  19459. cellBInfo.colIndex + cellBInfo.colSpan - 1
  19460. );
  19461. return checkRange(
  19462. beginRowIndex,
  19463. beginColIndex,
  19464. endRowIndex,
  19465. endColIndex
  19466. );
  19467. } catch (e) {
  19468. //throw e;
  19469. }
  19470. },
  19471. /**
  19472. * 依据cellsRange获取对应的单元格集合
  19473. */
  19474. getCells: function(range) {
  19475. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  19476. this.clearSelected();
  19477. var beginRowIndex = range.beginRowIndex,
  19478. beginColIndex = range.beginColIndex,
  19479. endRowIndex = range.endRowIndex,
  19480. endColIndex = range.endColIndex,
  19481. cellInfo,
  19482. rowIndex,
  19483. colIndex,
  19484. tdHash = {},
  19485. returnTds = [];
  19486. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  19487. for (var j = beginColIndex; j <= endColIndex; j++) {
  19488. cellInfo = this.indexTable[i][j];
  19489. rowIndex = cellInfo.rowIndex;
  19490. colIndex = cellInfo.colIndex;
  19491. // 如果Cells里已经包含了此Cell则跳过
  19492. var key = rowIndex + "|" + colIndex;
  19493. if (tdHash[key]) continue;
  19494. tdHash[key] = 1;
  19495. if (
  19496. rowIndex < i ||
  19497. colIndex < j ||
  19498. rowIndex + cellInfo.rowSpan - 1 > endRowIndex ||
  19499. colIndex + cellInfo.colSpan - 1 > endColIndex
  19500. ) {
  19501. return null;
  19502. }
  19503. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex));
  19504. }
  19505. }
  19506. return returnTds;
  19507. },
  19508. /**
  19509. * 清理已经选中的单元格
  19510. */
  19511. clearSelected: function() {
  19512. UETable.removeSelectedClass(this.selectedTds);
  19513. this.selectedTds = [];
  19514. this.cellsRange = {};
  19515. },
  19516. /**
  19517. * 根据range设置已经选中的单元格
  19518. */
  19519. setSelected: function(range) {
  19520. var cells = this.getCells(range);
  19521. UETable.addSelectedClass(cells);
  19522. this.selectedTds = cells;
  19523. this.cellsRange = range;
  19524. },
  19525. isFullRow: function() {
  19526. var range = this.cellsRange;
  19527. return range.endColIndex - range.beginColIndex + 1 == this.colsNum;
  19528. },
  19529. isFullCol: function() {
  19530. var range = this.cellsRange,
  19531. table = this.table,
  19532. ths = table.getElementsByTagName("th"),
  19533. rows = range.endRowIndex - range.beginRowIndex + 1;
  19534. return !ths.length
  19535. ? rows == this.rowsNum
  19536. : rows == this.rowsNum || rows == this.rowsNum - 1;
  19537. },
  19538. /**
  19539. * 获取视觉上的前置单元格,默认是左边,top传入时
  19540. * @param cell
  19541. * @param top
  19542. */
  19543. getNextCell: function(cell, bottom, ignoreRange) {
  19544. try {
  19545. var cellInfo = this.getCellInfo(cell),
  19546. nextRowIndex,
  19547. nextColIndex;
  19548. var len = this.selectedTds.length && !ignoreRange,
  19549. range = this.cellsRange;
  19550. //末行或者末列没有后置单元格
  19551. if (
  19552. (!bottom && cellInfo.rowIndex == 0) ||
  19553. (bottom &&
  19554. (!len
  19555. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19556. : range.endRowIndex == this.rowsNum - 1))
  19557. )
  19558. return null;
  19559. nextRowIndex = !bottom
  19560. ? !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1
  19561. : !len ? cellInfo.rowIndex + cellInfo.rowSpan : range.endRowIndex + 1;
  19562. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  19563. return this.getCell(
  19564. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19565. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19566. );
  19567. } catch (e) {
  19568. showError(e);
  19569. }
  19570. },
  19571. getPreviewCell: function(cell, top) {
  19572. try {
  19573. var cellInfo = this.getCellInfo(cell),
  19574. previewRowIndex,
  19575. previewColIndex;
  19576. var len = this.selectedTds.length,
  19577. range = this.cellsRange;
  19578. //首行或者首列没有前置单元格
  19579. if (
  19580. (!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19581. (top &&
  19582. (!len
  19583. ? cellInfo.rowIndex > this.colsNum - 1
  19584. : range.endColIndex == this.colsNum - 1))
  19585. )
  19586. return null;
  19587. previewRowIndex = !top
  19588. ? !len ? cellInfo.rowIndex : range.beginRowIndex
  19589. : !len
  19590. ? cellInfo.rowIndex < 1 ? 0 : cellInfo.rowIndex - 1
  19591. : range.beginRowIndex;
  19592. previewColIndex = !top
  19593. ? !len
  19594. ? cellInfo.colIndex < 1 ? 0 : cellInfo.colIndex - 1
  19595. : range.beginColIndex - 1
  19596. : !len ? cellInfo.colIndex : range.endColIndex + 1;
  19597. return this.getCell(
  19598. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19599. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19600. );
  19601. } catch (e) {
  19602. showError(e);
  19603. }
  19604. },
  19605. /**
  19606. * 移动单元格中的内容
  19607. */
  19608. moveContent: function(cellTo, cellFrom) {
  19609. if (UETable.isEmptyBlock(cellFrom)) return;
  19610. if (UETable.isEmptyBlock(cellTo)) {
  19611. cellTo.innerHTML = cellFrom.innerHTML;
  19612. return;
  19613. }
  19614. var child = cellTo.lastChild;
  19615. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  19616. cellTo.appendChild(cellTo.ownerDocument.createElement("br"));
  19617. }
  19618. while ((child = cellFrom.firstChild)) {
  19619. cellTo.appendChild(child);
  19620. }
  19621. },
  19622. /**
  19623. * 向右合并单元格
  19624. */
  19625. mergeRight: function(cell) {
  19626. var cellInfo = this.getCellInfo(cell),
  19627. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  19628. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  19629. rightCell = this.getCell(
  19630. rightCellInfo.rowIndex,
  19631. rightCellInfo.cellIndex
  19632. );
  19633. //合并
  19634. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan;
  19635. //被合并的单元格不应存在宽度属性
  19636. cell.removeAttribute("width");
  19637. //移动内容
  19638. this.moveContent(cell, rightCell);
  19639. //删掉被合并的Cell
  19640. this.deleteCell(rightCell, rightCellInfo.rowIndex);
  19641. this.update();
  19642. },
  19643. /**
  19644. * 向下合并单元格
  19645. */
  19646. mergeDown: function(cell) {
  19647. var cellInfo = this.getCellInfo(cell),
  19648. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  19649. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  19650. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex);
  19651. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan;
  19652. cell.removeAttribute("height");
  19653. this.moveContent(cell, downCell);
  19654. this.deleteCell(downCell, downCellInfo.rowIndex);
  19655. this.update();
  19656. },
  19657. /**
  19658. * 合并整个range中的内容
  19659. */
  19660. mergeRange: function() {
  19661. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  19662. var range = this.cellsRange,
  19663. leftTopCell = this.getCell(
  19664. range.beginRowIndex,
  19665. this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex
  19666. );
  19667. // 这段关于行表头或者列表头的特殊处理会导致表头合并范围错误
  19668. // 为什么有这段代码的原因未明,暂且注释掉,希望原作者看到后出面说明下
  19669. // if (
  19670. // leftTopCell.tagName == "TH" &&
  19671. // range.endRowIndex !== range.beginRowIndex
  19672. // ) {
  19673. // var index = this.indexTable,
  19674. // info = this.getCellInfo(leftTopCell);
  19675. // leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex);
  19676. // range = this.getCellsRange(
  19677. // leftTopCell,
  19678. // this.getCell(
  19679. // index[this.rowsNum - 1][info.colIndex].rowIndex,
  19680. // index[this.rowsNum - 1][info.colIndex].cellIndex
  19681. // )
  19682. // );
  19683. // }
  19684. // 删除剩余的Cells
  19685. var cells = this.getCells(range);
  19686. for (var i = 0, ci; (ci = cells[i++]); ) {
  19687. if (ci !== leftTopCell) {
  19688. this.moveContent(leftTopCell, ci);
  19689. this.deleteCell(ci);
  19690. }
  19691. }
  19692. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  19693. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1;
  19694. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute("height");
  19695. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1;
  19696. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute("width");
  19697. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  19698. leftTopCell.colSpan = 1;
  19699. }
  19700. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  19701. var rowIndex = leftTopCell.parentNode.rowIndex;
  19702. //解决IE下的表格操作问题
  19703. if (this.table.deleteRow) {
  19704. for (
  19705. var i = rowIndex + 1,
  19706. curIndex = rowIndex + 1,
  19707. len = leftTopCell.rowSpan;
  19708. i < len;
  19709. i++
  19710. ) {
  19711. this.table.deleteRow(curIndex);
  19712. }
  19713. } else {
  19714. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  19715. var row = this.table.rows[rowIndex + 1];
  19716. row.parentNode.removeChild(row);
  19717. }
  19718. }
  19719. leftTopCell.rowSpan = 1;
  19720. }
  19721. this.update();
  19722. },
  19723. /**
  19724. * 插入一行单元格
  19725. */
  19726. insertRow: function(rowIndex, sourceCell) {
  19727. var numCols = this.colsNum,
  19728. table = this.table,
  19729. row = table.insertRow(rowIndex),
  19730. cell,
  19731. thead = null,
  19732. isInsertTitle =
  19733. typeof sourceCell == "string" && sourceCell.toUpperCase() == "TH";
  19734. function replaceTdToTh(colIndex, cell, tableRow) {
  19735. if (colIndex == 0) {
  19736. var tr = tableRow.nextSibling || tableRow.previousSibling,
  19737. th = tr.cells[colIndex];
  19738. if (th.tagName == "TH") {
  19739. th = cell.ownerDocument.createElement("th");
  19740. th.appendChild(cell.firstChild);
  19741. tableRow.insertBefore(th, cell);
  19742. domUtils.remove(cell);
  19743. }
  19744. } else {
  19745. if (cell.tagName == "TH") {
  19746. var td = cell.ownerDocument.createElement("td");
  19747. td.appendChild(cell.firstChild);
  19748. tableRow.insertBefore(td, cell);
  19749. domUtils.remove(cell);
  19750. }
  19751. }
  19752. }
  19753. //首行直接插入,无需考虑部分单元格被rowspan的情况
  19754. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  19755. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  19756. cell = this.cloneCell(sourceCell, true);
  19757. this.setCellContent(cell);
  19758. cell.getAttribute("vAlign") &&
  19759. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19760. row.appendChild(cell);
  19761. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  19762. }
  19763. if (isInsertTitle) {
  19764. thead = table.createTHead();
  19765. thead.insertBefore(row, thead.firstChild);
  19766. }
  19767. } else {
  19768. var infoRow = this.indexTable[rowIndex],
  19769. cellIndex = 0;
  19770. for (colIndex = 0; colIndex < numCols; colIndex++) {
  19771. var cellInfo = infoRow[colIndex];
  19772. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  19773. if (cellInfo.rowIndex < rowIndex) {
  19774. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19775. cell.rowSpan = cellInfo.rowSpan + 1;
  19776. } else {
  19777. cell = this.cloneCell(sourceCell, true);
  19778. this.setCellContent(cell);
  19779. row.appendChild(cell);
  19780. }
  19781. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  19782. }
  19783. }
  19784. //框选时插入不触发contentchange,需要手动更新索引。
  19785. this.update();
  19786. return row;
  19787. },
  19788. /**
  19789. * 删除一行单元格
  19790. * @param rowIndex
  19791. */
  19792. deleteRow: function(rowIndex) {
  19793. var row = this.table.rows[rowIndex],
  19794. infoRow = this.indexTable[rowIndex],
  19795. colsNum = this.colsNum,
  19796. count = 0; //处理计数
  19797. for (var colIndex = 0; colIndex < colsNum; ) {
  19798. var cellInfo = infoRow[colIndex],
  19799. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19800. if (cell.rowSpan > 1) {
  19801. if (cellInfo.rowIndex == rowIndex) {
  19802. var clone = cell.cloneNode(true);
  19803. clone.rowSpan = cell.rowSpan - 1;
  19804. clone.innerHTML = "";
  19805. cell.rowSpan = 1;
  19806. var nextRowIndex = rowIndex + 1,
  19807. nextRow = this.table.rows[nextRowIndex],
  19808. insertCellIndex,
  19809. preMerged =
  19810. this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count;
  19811. if (preMerged < colIndex) {
  19812. insertCellIndex = colIndex - preMerged - 1;
  19813. //nextRow.insertCell(insertCellIndex);
  19814. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone);
  19815. } else {
  19816. if (nextRow.cells.length)
  19817. nextRow.insertBefore(clone, nextRow.cells[0]);
  19818. }
  19819. count += 1;
  19820. //cell.parentNode.removeChild(cell);
  19821. }
  19822. }
  19823. colIndex += cell.colSpan || 1;
  19824. }
  19825. var deleteTds = [],
  19826. cacheMap = {};
  19827. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  19828. var tmpRowIndex = infoRow[colIndex].rowIndex,
  19829. tmpCellIndex = infoRow[colIndex].cellIndex,
  19830. key = tmpRowIndex + "_" + tmpCellIndex;
  19831. if (cacheMap[key]) continue;
  19832. cacheMap[key] = 1;
  19833. cell = this.getCell(tmpRowIndex, tmpCellIndex);
  19834. deleteTds.push(cell);
  19835. }
  19836. var mergeTds = [];
  19837. utils.each(deleteTds, function(td) {
  19838. if (td.rowSpan == 1) {
  19839. td.parentNode.removeChild(td);
  19840. } else {
  19841. mergeTds.push(td);
  19842. }
  19843. });
  19844. utils.each(mergeTds, function(td) {
  19845. td.rowSpan--;
  19846. });
  19847. row.parentNode.removeChild(row);
  19848. //浏览器方法本身存在bug,采用自定义方法删除
  19849. //this.table.deleteRow(rowIndex);
  19850. this.update();
  19851. },
  19852. insertCol: function(colIndex, sourceCell, defaultValue) {
  19853. var rowsNum = this.rowsNum,
  19854. rowIndex = 0,
  19855. tableRow,
  19856. cell,
  19857. backWidth = parseInt(
  19858. (this.table.offsetWidth -
  19859. (this.colsNum + 1) * 20 -
  19860. (this.colsNum + 1)) /
  19861. (this.colsNum + 1),
  19862. 10
  19863. ),
  19864. isInsertTitleCol =
  19865. typeof sourceCell == "string" && sourceCell.toUpperCase() == "TH";
  19866. function replaceTdToTh(rowIndex, cell, tableRow) {
  19867. if (rowIndex == 0) {
  19868. var th = cell.nextSibling || cell.previousSibling;
  19869. if (th.tagName == "TH") {
  19870. th = cell.ownerDocument.createElement("th");
  19871. th.appendChild(cell.firstChild);
  19872. tableRow.insertBefore(th, cell);
  19873. domUtils.remove(cell);
  19874. }
  19875. } else {
  19876. if (cell.tagName == "TH") {
  19877. var td = cell.ownerDocument.createElement("td");
  19878. td.appendChild(cell.firstChild);
  19879. tableRow.insertBefore(td, cell);
  19880. domUtils.remove(cell);
  19881. }
  19882. }
  19883. }
  19884. var preCell;
  19885. if (colIndex == 0 || colIndex == this.colsNum) {
  19886. for (; rowIndex < rowsNum; rowIndex++) {
  19887. tableRow = this.table.rows[rowIndex];
  19888. preCell =
  19889. tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length];
  19890. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  19891. this.setCellContent(cell);
  19892. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19893. preCell && cell.setAttribute("width", preCell.getAttribute("width"));
  19894. if (!colIndex) {
  19895. tableRow.insertBefore(cell, tableRow.cells[0]);
  19896. } else {
  19897. domUtils.insertAfter(
  19898. tableRow.cells[tableRow.cells.length - 1],
  19899. cell
  19900. );
  19901. }
  19902. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  19903. }
  19904. } else {
  19905. for (; rowIndex < rowsNum; rowIndex++) {
  19906. var cellInfo = this.indexTable[rowIndex][colIndex];
  19907. if (cellInfo.colIndex < colIndex) {
  19908. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19909. cell.colSpan = cellInfo.colSpan + 1;
  19910. } else {
  19911. tableRow = this.table.rows[rowIndex];
  19912. preCell = tableRow.cells[cellInfo.cellIndex];
  19913. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(cellInfo.cellIndex);
  19914. this.setCellContent(cell);
  19915. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19916. preCell &&
  19917. cell.setAttribute("width", preCell.getAttribute("width"));
  19918. //防止IE下报错
  19919. preCell
  19920. ? tableRow.insertBefore(cell, preCell)
  19921. : tableRow.appendChild(cell);
  19922. }
  19923. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  19924. }
  19925. }
  19926. //框选时插入不触发contentchange,需要手动更新索引
  19927. this.update();
  19928. this.updateWidth(
  19929. backWidth,
  19930. defaultValue || { tdPadding: 10, tdBorder: 1 }
  19931. );
  19932. },
  19933. updateWidth: function(width, defaultValue) {
  19934. var table = this.table,
  19935. tmpWidth =
  19936. UETable.getWidth(table) -
  19937. defaultValue.tdPadding * 2 -
  19938. defaultValue.tdBorder +
  19939. width;
  19940. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  19941. table.setAttribute("width", tmpWidth);
  19942. return;
  19943. }
  19944. var tds = domUtils.getElementsByTagName(this.table, "td th");
  19945. utils.each(tds, function(td) {
  19946. td.setAttribute("width", width);
  19947. });
  19948. },
  19949. deleteCol: function(colIndex) {
  19950. var indexTable = this.indexTable,
  19951. tableRows = this.table.rows,
  19952. backTableWidth = this.table.getAttribute("width"),
  19953. backTdWidth = 0,
  19954. rowsNum = this.rowsNum,
  19955. cacheMap = {};
  19956. for (var rowIndex = 0; rowIndex < rowsNum; ) {
  19957. var infoRow = indexTable[rowIndex],
  19958. cellInfo = infoRow[colIndex],
  19959. key = cellInfo.rowIndex + "_" + cellInfo.colIndex;
  19960. // 跳过已经处理过的Cell
  19961. if (cacheMap[key]) continue;
  19962. cacheMap[key] = 1;
  19963. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19964. if (!backTdWidth)
  19965. backTdWidth =
  19966. cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0);
  19967. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  19968. if (cell.colSpan > 1) {
  19969. cell.colSpan--;
  19970. } else {
  19971. tableRows[rowIndex].deleteCell(cellInfo.cellIndex);
  19972. }
  19973. rowIndex += cellInfo.rowSpan || 1;
  19974. }
  19975. this.table.setAttribute("width", backTableWidth - backTdWidth);
  19976. this.update();
  19977. },
  19978. splitToCells: function(cell) {
  19979. var me = this,
  19980. cells = this.splitToRows(cell);
  19981. utils.each(cells, function(cell) {
  19982. me.splitToCols(cell);
  19983. });
  19984. },
  19985. splitToRows: function(cell) {
  19986. var cellInfo = this.getCellInfo(cell),
  19987. rowIndex = cellInfo.rowIndex,
  19988. colIndex = cellInfo.colIndex,
  19989. results = [];
  19990. // 修改Cell的rowSpan
  19991. cell.rowSpan = 1;
  19992. results.push(cell);
  19993. // 补齐单元格
  19994. for (
  19995. var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan;
  19996. i < endRow;
  19997. i++
  19998. ) {
  19999. if (i == rowIndex) continue;
  20000. var tableRow = this.table.rows[i],
  20001. tmpCell = tableRow.insertCell(
  20002. colIndex - this.getPreviewMergedCellsNum(i, colIndex)
  20003. );
  20004. tmpCell.colSpan = cellInfo.colSpan;
  20005. this.setCellContent(tmpCell);
  20006. tmpCell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20007. tmpCell.setAttribute("align", cell.getAttribute("align"));
  20008. if (cell.style.cssText) {
  20009. tmpCell.style.cssText = cell.style.cssText;
  20010. }
  20011. results.push(tmpCell);
  20012. }
  20013. this.update();
  20014. return results;
  20015. },
  20016. getPreviewMergedCellsNum: function(rowIndex, colIndex) {
  20017. var indexRow = this.indexTable[rowIndex],
  20018. num = 0;
  20019. for (var i = 0; i < colIndex; ) {
  20020. var colSpan = indexRow[i].colSpan,
  20021. tmpRowIndex = indexRow[i].rowIndex;
  20022. num += colSpan - (tmpRowIndex == rowIndex ? 1 : 0);
  20023. i += colSpan;
  20024. }
  20025. return num;
  20026. },
  20027. splitToCols: function(cell) {
  20028. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  20029. cellInfo = this.getCellInfo(cell),
  20030. rowIndex = cellInfo.rowIndex,
  20031. colIndex = cellInfo.colIndex,
  20032. results = [];
  20033. // 修改Cell的rowSpan
  20034. cell.colSpan = 1;
  20035. cell.setAttribute("width", backWidth);
  20036. results.push(cell);
  20037. // 补齐单元格
  20038. for (
  20039. var j = colIndex, endCol = colIndex + cellInfo.colSpan;
  20040. j < endCol;
  20041. j++
  20042. ) {
  20043. if (j == colIndex) continue;
  20044. var tableRow = this.table.rows[rowIndex],
  20045. tmpCell = tableRow.insertCell(
  20046. this.indexTable[rowIndex][j].cellIndex + 1
  20047. );
  20048. tmpCell.rowSpan = cellInfo.rowSpan;
  20049. this.setCellContent(tmpCell);
  20050. tmpCell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20051. tmpCell.setAttribute("align", cell.getAttribute("align"));
  20052. tmpCell.setAttribute("width", backWidth);
  20053. if (cell.style.cssText) {
  20054. tmpCell.style.cssText = cell.style.cssText;
  20055. }
  20056. //处理th的情况
  20057. if (cell.tagName == "TH") {
  20058. var th = cell.ownerDocument.createElement("th");
  20059. th.appendChild(tmpCell.firstChild);
  20060. th.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20061. th.rowSpan = tmpCell.rowSpan;
  20062. tableRow.insertBefore(th, tmpCell);
  20063. domUtils.remove(tmpCell);
  20064. }
  20065. results.push(tmpCell);
  20066. }
  20067. this.update();
  20068. return results;
  20069. },
  20070. isLastCell: function(cell, rowsNum, colsNum) {
  20071. rowsNum = rowsNum || this.rowsNum;
  20072. colsNum = colsNum || this.colsNum;
  20073. var cellInfo = this.getCellInfo(cell);
  20074. return (
  20075. cellInfo.rowIndex + cellInfo.rowSpan == rowsNum &&
  20076. cellInfo.colIndex + cellInfo.colSpan == colsNum
  20077. );
  20078. },
  20079. getLastCell: function(cells) {
  20080. cells = cells || this.table.getElementsByTagName("td");
  20081. var firstInfo = this.getCellInfo(cells[0]);
  20082. var me = this,
  20083. last = cells[0],
  20084. tr = last.parentNode,
  20085. cellsNum = 0,
  20086. cols = 0,
  20087. rows;
  20088. utils.each(cells, function(cell) {
  20089. if (cell.parentNode == tr) cols += cell.colSpan || 1;
  20090. cellsNum += cell.rowSpan * cell.colSpan || 1;
  20091. });
  20092. rows = cellsNum / cols;
  20093. utils.each(cells, function(cell) {
  20094. if (me.isLastCell(cell, rows, cols)) {
  20095. last = cell;
  20096. return false;
  20097. }
  20098. });
  20099. return last;
  20100. },
  20101. selectRow: function(rowIndex) {
  20102. var indexRow = this.indexTable[rowIndex],
  20103. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  20104. end = this.getCell(
  20105. indexRow[this.colsNum - 1].rowIndex,
  20106. indexRow[this.colsNum - 1].cellIndex
  20107. ),
  20108. range = this.getCellsRange(start, end);
  20109. this.setSelected(range);
  20110. },
  20111. selectTable: function() {
  20112. var tds = this.table.getElementsByTagName("td"),
  20113. range = this.getCellsRange(tds[0], tds[tds.length - 1]);
  20114. this.setSelected(range);
  20115. },
  20116. setBackground: function(cells, value) {
  20117. if (typeof value === "string") {
  20118. utils.each(cells, function(cell) {
  20119. cell.style.backgroundColor = value;
  20120. });
  20121. } else if (typeof value === "object") {
  20122. value = utils.extend(
  20123. {
  20124. repeat: true,
  20125. colorList: ["#ddd", "#fff"]
  20126. },
  20127. value
  20128. );
  20129. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  20130. count = 0,
  20131. colors = value.colorList,
  20132. getColor = function(list, index, repeat) {
  20133. return list[index]
  20134. ? list[index]
  20135. : repeat ? list[index % list.length] : "";
  20136. };
  20137. for (var i = 0, cell; (cell = cells[i++]); ) {
  20138. var cellInfo = this.getCellInfo(cell);
  20139. cell.style.backgroundColor = getColor(
  20140. colors,
  20141. rowIndex + count == cellInfo.rowIndex ? count : ++count,
  20142. value.repeat
  20143. );
  20144. }
  20145. }
  20146. },
  20147. removeBackground: function(cells) {
  20148. utils.each(cells, function(cell) {
  20149. cell.style.backgroundColor = "";
  20150. });
  20151. }
  20152. };
  20153. function showError(e) {}
  20154. })();
  20155. // plugins/table.cmds.js
  20156. /**
  20157. * Created with JetBrains PhpStorm.
  20158. * User: taoqili
  20159. * Date: 13-2-20
  20160. * Time: 下午6:25
  20161. * To change this template use File | Settings | File Templates.
  20162. */
  20163. ;(function() {
  20164. var UT = UE.UETable,
  20165. getTableItemsByRange = function(editor) {
  20166. return UT.getTableItemsByRange(editor);
  20167. },
  20168. getUETableBySelected = function(editor) {
  20169. return UT.getUETableBySelected(editor);
  20170. },
  20171. getDefaultValue = function(editor, table) {
  20172. return UT.getDefaultValue(editor, table);
  20173. },
  20174. getUETable = function(tdOrTable) {
  20175. return UT.getUETable(tdOrTable);
  20176. };
  20177. UE.commands["inserttable"] = {
  20178. queryCommandState: function() {
  20179. return getTableItemsByRange(this).table ? -1 : 0;
  20180. },
  20181. execCommand: function(cmd, opt) {
  20182. function createTable(opt, tdWidth) {
  20183. var html = [],
  20184. rowsNum = opt.numRows,
  20185. colsNum = opt.numCols;
  20186. for (var r = 0; r < rowsNum; r++) {
  20187. html.push("<tr" + (r == 0 ? ' class="firstRow"' : "") + ">");
  20188. for (var c = 0; c < colsNum; c++) {
  20189. html.push(
  20190. '<td width="' +
  20191. tdWidth +
  20192. '" vAlign="' +
  20193. opt.tdvalign +
  20194. '" >' +
  20195. (browser.ie && browser.version < 11
  20196. ? domUtils.fillChar
  20197. : "<br/>") +
  20198. "</td>"
  20199. );
  20200. }
  20201. html.push("</tr>");
  20202. }
  20203. //禁止指定table-width
  20204. return "<table><tbody>" + html.join("") + "</tbody></table>";
  20205. }
  20206. if (!opt) {
  20207. opt = utils.extend(
  20208. {},
  20209. {
  20210. numCols: this.options.defaultCols,
  20211. numRows: this.options.defaultRows,
  20212. tdvalign: this.options.tdvalign
  20213. }
  20214. );
  20215. }
  20216. var me = this;
  20217. var range = this.selection.getRange(),
  20218. start = range.startContainer,
  20219. firstParentBlock =
  20220. domUtils.findParent(
  20221. start,
  20222. function(node) {
  20223. return domUtils.isBlockElm(node);
  20224. },
  20225. true
  20226. ) || me.body;
  20227. var defaultValue = getDefaultValue(me),
  20228. tableWidth = firstParentBlock.offsetWidth,
  20229. tdWidth = Math.floor(
  20230. tableWidth / opt.numCols -
  20231. defaultValue.tdPadding * 2 -
  20232. defaultValue.tdBorder
  20233. );
  20234. //todo其他属性
  20235. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign);
  20236. me.execCommand("inserthtml", createTable(opt, tdWidth));
  20237. }
  20238. };
  20239. UE.commands["insertparagraphbeforetable"] = {
  20240. queryCommandState: function() {
  20241. return getTableItemsByRange(this).cell ? 0 : -1;
  20242. },
  20243. execCommand: function() {
  20244. var table = getTableItemsByRange(this).table;
  20245. if (table) {
  20246. var p = this.document.createElement("p");
  20247. p.innerHTML = browser.ie ? "&nbsp;" : "<br />";
  20248. table.parentNode.insertBefore(p, table);
  20249. this.selection.getRange().setStart(p, 0).setCursor();
  20250. }
  20251. }
  20252. };
  20253. UE.commands["deletetable"] = {
  20254. queryCommandState: function() {
  20255. var rng = this.selection.getRange();
  20256. return domUtils.findParentByTagName(rng.startContainer, "table", true)
  20257. ? 0
  20258. : -1;
  20259. },
  20260. execCommand: function(cmd, table) {
  20261. var rng = this.selection.getRange();
  20262. table =
  20263. table ||
  20264. domUtils.findParentByTagName(rng.startContainer, "table", true);
  20265. if (table) {
  20266. var next = table.nextSibling;
  20267. if (!next) {
  20268. next = domUtils.createElement(this.document, "p", {
  20269. innerHTML: browser.ie ? domUtils.fillChar : "<br/>"
  20270. });
  20271. table.parentNode.insertBefore(next, table);
  20272. }
  20273. domUtils.remove(table);
  20274. rng = this.selection.getRange();
  20275. if (next.nodeType == 3) {
  20276. rng.setStartBefore(next);
  20277. } else {
  20278. rng.setStart(next, 0);
  20279. }
  20280. rng.setCursor(false, true);
  20281. this.fireEvent("tablehasdeleted");
  20282. }
  20283. }
  20284. };
  20285. UE.commands["cellalign"] = {
  20286. queryCommandState: function() {
  20287. return getSelectedArr(this).length ? 0 : -1;
  20288. },
  20289. execCommand: function(cmd, align) {
  20290. var selectedTds = getSelectedArr(this);
  20291. if (selectedTds.length) {
  20292. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20293. ci.setAttribute("align", align);
  20294. }
  20295. }
  20296. }
  20297. };
  20298. UE.commands["cellvalign"] = {
  20299. queryCommandState: function() {
  20300. return getSelectedArr(this).length ? 0 : -1;
  20301. },
  20302. execCommand: function(cmd, valign) {
  20303. var selectedTds = getSelectedArr(this);
  20304. if (selectedTds.length) {
  20305. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20306. ci.setAttribute("vAlign", valign);
  20307. }
  20308. }
  20309. }
  20310. };
  20311. UE.commands["insertcaption"] = {
  20312. queryCommandState: function() {
  20313. var table = getTableItemsByRange(this).table;
  20314. if (table) {
  20315. return table.getElementsByTagName("caption").length == 0 ? 1 : -1;
  20316. }
  20317. return -1;
  20318. },
  20319. execCommand: function() {
  20320. var table = getTableItemsByRange(this).table;
  20321. if (table) {
  20322. var caption = this.document.createElement("caption");
  20323. caption.innerHTML = browser.ie ? domUtils.fillChar : "<br/>";
  20324. table.insertBefore(caption, table.firstChild);
  20325. var range = this.selection.getRange();
  20326. range.setStart(caption, 0).setCursor();
  20327. }
  20328. }
  20329. };
  20330. UE.commands["deletecaption"] = {
  20331. queryCommandState: function() {
  20332. var rng = this.selection.getRange(),
  20333. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20334. if (table) {
  20335. return table.getElementsByTagName("caption").length == 0 ? -1 : 1;
  20336. }
  20337. return -1;
  20338. },
  20339. execCommand: function() {
  20340. var rng = this.selection.getRange(),
  20341. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20342. if (table) {
  20343. domUtils.remove(table.getElementsByTagName("caption")[0]);
  20344. var range = this.selection.getRange();
  20345. range.setStart(table.rows[0].cells[0], 0).setCursor();
  20346. }
  20347. }
  20348. };
  20349. UE.commands["inserttitle"] = {
  20350. queryCommandState: function() {
  20351. var table = getTableItemsByRange(this).table;
  20352. if (table) {
  20353. var firstRow = table.rows[0];
  20354. return firstRow.cells[
  20355. firstRow.cells.length - 1
  20356. ].tagName.toLowerCase() != "th"
  20357. ? 0
  20358. : -1;
  20359. }
  20360. return -1;
  20361. },
  20362. execCommand: function() {
  20363. var table = getTableItemsByRange(this).table;
  20364. if (table) {
  20365. getUETable(table).insertRow(0, "th");
  20366. }
  20367. var th = table.getElementsByTagName("th")[0];
  20368. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  20369. }
  20370. };
  20371. UE.commands["deletetitle"] = {
  20372. queryCommandState: function() {
  20373. var table = getTableItemsByRange(this).table;
  20374. if (table) {
  20375. var firstRow = table.rows[0];
  20376. return firstRow.cells[
  20377. firstRow.cells.length - 1
  20378. ].tagName.toLowerCase() == "th"
  20379. ? 0
  20380. : -1;
  20381. }
  20382. return -1;
  20383. },
  20384. execCommand: function() {
  20385. var table = getTableItemsByRange(this).table;
  20386. if (table) {
  20387. domUtils.remove(table.rows[0]);
  20388. }
  20389. var td = table.getElementsByTagName("td")[0];
  20390. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  20391. }
  20392. };
  20393. UE.commands["inserttitlecol"] = {
  20394. queryCommandState: function() {
  20395. var table = getTableItemsByRange(this).table;
  20396. if (table) {
  20397. var lastRow = table.rows[table.rows.length - 1];
  20398. return lastRow.getElementsByTagName("th").length ? -1 : 0;
  20399. }
  20400. return -1;
  20401. },
  20402. execCommand: function(cmd) {
  20403. var table = getTableItemsByRange(this).table;
  20404. if (table) {
  20405. getUETable(table).insertCol(0, "th");
  20406. }
  20407. resetTdWidth(table, this);
  20408. var th = table.getElementsByTagName("th")[0];
  20409. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  20410. }
  20411. };
  20412. UE.commands["deletetitlecol"] = {
  20413. queryCommandState: function() {
  20414. var table = getTableItemsByRange(this).table;
  20415. if (table) {
  20416. var lastRow = table.rows[table.rows.length - 1];
  20417. return lastRow.getElementsByTagName("th").length ? 0 : -1;
  20418. }
  20419. return -1;
  20420. },
  20421. execCommand: function() {
  20422. var table = getTableItemsByRange(this).table;
  20423. if (table) {
  20424. for (var i = 0; i < table.rows.length; i++) {
  20425. domUtils.remove(table.rows[i].children[0]);
  20426. }
  20427. }
  20428. resetTdWidth(table, this);
  20429. var td = table.getElementsByTagName("td")[0];
  20430. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  20431. }
  20432. };
  20433. UE.commands["mergeright"] = {
  20434. queryCommandState: function(cmd) {
  20435. var tableItems = getTableItemsByRange(this),
  20436. table = tableItems.table,
  20437. cell = tableItems.cell;
  20438. if (!table || !cell) return -1;
  20439. var ut = getUETable(table);
  20440. if (ut.selectedTds.length) return -1;
  20441. var cellInfo = ut.getCellInfo(cell),
  20442. rightColIndex = cellInfo.colIndex + cellInfo.colSpan;
  20443. if (rightColIndex >= ut.colsNum) return -1; // 如果处于最右边则不能向右合并
  20444. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  20445. rightCell =
  20446. table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex];
  20447. if (!rightCell || cell.tagName != rightCell.tagName) return -1; // TH和TD不能相互合并
  20448. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20449. return rightCellInfo.rowIndex == cellInfo.rowIndex &&
  20450. rightCellInfo.rowSpan == cellInfo.rowSpan
  20451. ? 0
  20452. : -1;
  20453. },
  20454. execCommand: function(cmd) {
  20455. var rng = this.selection.getRange(),
  20456. bk = rng.createBookmark(true);
  20457. var cell = getTableItemsByRange(this).cell,
  20458. ut = getUETable(cell);
  20459. ut.mergeRight(cell);
  20460. rng.moveToBookmark(bk).select();
  20461. }
  20462. };
  20463. UE.commands["mergedown"] = {
  20464. queryCommandState: function(cmd) {
  20465. var tableItems = getTableItemsByRange(this),
  20466. table = tableItems.table,
  20467. cell = tableItems.cell;
  20468. if (!table || !cell) return -1;
  20469. var ut = getUETable(table);
  20470. if (ut.selectedTds.length) return -1;
  20471. var cellInfo = ut.getCellInfo(cell),
  20472. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan;
  20473. if (downRowIndex >= ut.rowsNum) return -1; // 如果处于最下边则不能向下合并
  20474. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  20475. downCell =
  20476. table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex];
  20477. if (!downCell || cell.tagName != downCell.tagName) return -1; // TH和TD不能相互合并
  20478. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20479. return downCellInfo.colIndex == cellInfo.colIndex &&
  20480. downCellInfo.colSpan == cellInfo.colSpan
  20481. ? 0
  20482. : -1;
  20483. },
  20484. execCommand: function() {
  20485. var rng = this.selection.getRange(),
  20486. bk = rng.createBookmark(true);
  20487. var cell = getTableItemsByRange(this).cell,
  20488. ut = getUETable(cell);
  20489. ut.mergeDown(cell);
  20490. rng.moveToBookmark(bk).select();
  20491. }
  20492. };
  20493. UE.commands["mergecells"] = {
  20494. queryCommandState: function() {
  20495. return getUETableBySelected(this) ? 0 : -1;
  20496. },
  20497. execCommand: function() {
  20498. var ut = getUETableBySelected(this);
  20499. if (ut && ut.selectedTds.length) {
  20500. var cell = ut.selectedTds[0];
  20501. ut.mergeRange();
  20502. var rng = this.selection.getRange();
  20503. if (domUtils.isEmptyBlock(cell)) {
  20504. rng.setStart(cell, 0).collapse(true);
  20505. } else {
  20506. rng.selectNodeContents(cell);
  20507. }
  20508. rng.select();
  20509. }
  20510. }
  20511. };
  20512. UE.commands["insertrow"] = {
  20513. queryCommandState: function() {
  20514. var tableItems = getTableItemsByRange(this),
  20515. cell = tableItems.cell;
  20516. return cell &&
  20517. (cell.tagName == "TD" ||
  20518. (cell.tagName == "TH" &&
  20519. tableItems.tr !== tableItems.table.rows[0])) &&
  20520. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20521. ? 0
  20522. : -1;
  20523. },
  20524. execCommand: function() {
  20525. var rng = this.selection.getRange(),
  20526. bk = rng.createBookmark(true);
  20527. var tableItems = getTableItemsByRange(this),
  20528. cell = tableItems.cell,
  20529. table = tableItems.table,
  20530. ut = getUETable(table),
  20531. cellInfo = ut.getCellInfo(cell);
  20532. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  20533. if (!ut.selectedTds.length) {
  20534. ut.insertRow(cellInfo.rowIndex, cell);
  20535. } else {
  20536. var range = ut.cellsRange;
  20537. for (
  20538. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20539. i < len;
  20540. i++
  20541. ) {
  20542. ut.insertRow(range.beginRowIndex, cell);
  20543. }
  20544. }
  20545. rng.moveToBookmark(bk).select();
  20546. if (table.getAttribute("interlaced") === "enabled")
  20547. this.fireEvent("interlacetable", table);
  20548. }
  20549. };
  20550. //后插入行
  20551. UE.commands["insertrownext"] = {
  20552. queryCommandState: function() {
  20553. var tableItems = getTableItemsByRange(this),
  20554. cell = tableItems.cell;
  20555. return cell &&
  20556. cell.tagName == "TD" &&
  20557. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20558. ? 0
  20559. : -1;
  20560. },
  20561. execCommand: function() {
  20562. var rng = this.selection.getRange(),
  20563. bk = rng.createBookmark(true);
  20564. var tableItems = getTableItemsByRange(this),
  20565. cell = tableItems.cell,
  20566. table = tableItems.table,
  20567. ut = getUETable(table),
  20568. cellInfo = ut.getCellInfo(cell);
  20569. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  20570. if (!ut.selectedTds.length) {
  20571. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell);
  20572. } else {
  20573. var range = ut.cellsRange;
  20574. for (
  20575. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20576. i < len;
  20577. i++
  20578. ) {
  20579. ut.insertRow(range.endRowIndex + 1, cell);
  20580. }
  20581. }
  20582. rng.moveToBookmark(bk).select();
  20583. if (table.getAttribute("interlaced") === "enabled")
  20584. this.fireEvent("interlacetable", table);
  20585. }
  20586. };
  20587. UE.commands["deleterow"] = {
  20588. queryCommandState: function() {
  20589. var tableItems = getTableItemsByRange(this);
  20590. return tableItems.cell ? 0 : -1;
  20591. },
  20592. execCommand: function() {
  20593. var cell = getTableItemsByRange(this).cell,
  20594. ut = getUETable(cell),
  20595. cellsRange = ut.cellsRange,
  20596. cellInfo = ut.getCellInfo(cell),
  20597. preCell = ut.getVSideCell(cell),
  20598. nextCell = ut.getVSideCell(cell, true),
  20599. rng = this.selection.getRange();
  20600. if (utils.isEmptyObject(cellsRange)) {
  20601. ut.deleteRow(cellInfo.rowIndex);
  20602. } else {
  20603. for (
  20604. var i = cellsRange.beginRowIndex;
  20605. i < cellsRange.endRowIndex + 1;
  20606. i++
  20607. ) {
  20608. ut.deleteRow(cellsRange.beginRowIndex);
  20609. }
  20610. }
  20611. var table = ut.table;
  20612. if (!table.getElementsByTagName("td").length) {
  20613. var nextSibling = table.nextSibling;
  20614. domUtils.remove(table);
  20615. if (nextSibling) {
  20616. rng.setStart(nextSibling, 0).setCursor(false, true);
  20617. }
  20618. } else {
  20619. if (
  20620. cellInfo.rowSpan == 1 ||
  20621. cellInfo.rowSpan ==
  20622. cellsRange.endRowIndex - cellsRange.beginRowIndex + 1
  20623. ) {
  20624. if (nextCell || preCell)
  20625. rng.selectNodeContents(nextCell || preCell).setCursor(false, true);
  20626. } else {
  20627. var newCell = ut.getCell(
  20628. cellInfo.rowIndex,
  20629. ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex
  20630. );
  20631. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true);
  20632. }
  20633. }
  20634. if (table.getAttribute("interlaced") === "enabled")
  20635. this.fireEvent("interlacetable", table);
  20636. }
  20637. };
  20638. UE.commands["insertcol"] = {
  20639. queryCommandState: function(cmd) {
  20640. var tableItems = getTableItemsByRange(this),
  20641. cell = tableItems.cell;
  20642. return cell &&
  20643. (cell.tagName == "TD" ||
  20644. (cell.tagName == "TH" && cell !== tableItems.tr.cells[0])) &&
  20645. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20646. ? 0
  20647. : -1;
  20648. },
  20649. execCommand: function(cmd) {
  20650. var rng = this.selection.getRange(),
  20651. bk = rng.createBookmark(true);
  20652. if (this.queryCommandState(cmd) == -1) return;
  20653. var cell = getTableItemsByRange(this).cell,
  20654. ut = getUETable(cell),
  20655. cellInfo = ut.getCellInfo(cell);
  20656. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  20657. if (!ut.selectedTds.length) {
  20658. ut.insertCol(cellInfo.colIndex, cell);
  20659. } else {
  20660. var range = ut.cellsRange;
  20661. for (
  20662. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20663. i < len;
  20664. i++
  20665. ) {
  20666. ut.insertCol(range.beginColIndex, cell);
  20667. }
  20668. }
  20669. rng.moveToBookmark(bk).select(true);
  20670. }
  20671. };
  20672. UE.commands["insertcolnext"] = {
  20673. queryCommandState: function() {
  20674. var tableItems = getTableItemsByRange(this),
  20675. cell = tableItems.cell;
  20676. return cell &&
  20677. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20678. ? 0
  20679. : -1;
  20680. },
  20681. execCommand: function() {
  20682. var rng = this.selection.getRange(),
  20683. bk = rng.createBookmark(true);
  20684. var cell = getTableItemsByRange(this).cell,
  20685. ut = getUETable(cell),
  20686. cellInfo = ut.getCellInfo(cell);
  20687. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  20688. if (!ut.selectedTds.length) {
  20689. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell);
  20690. } else {
  20691. var range = ut.cellsRange;
  20692. for (
  20693. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20694. i < len;
  20695. i++
  20696. ) {
  20697. ut.insertCol(range.endColIndex + 1, cell);
  20698. }
  20699. }
  20700. rng.moveToBookmark(bk).select();
  20701. }
  20702. };
  20703. UE.commands["deletecol"] = {
  20704. queryCommandState: function() {
  20705. var tableItems = getTableItemsByRange(this);
  20706. return tableItems.cell ? 0 : -1;
  20707. },
  20708. execCommand: function() {
  20709. var cell = getTableItemsByRange(this).cell,
  20710. ut = getUETable(cell),
  20711. range = ut.cellsRange,
  20712. cellInfo = ut.getCellInfo(cell),
  20713. preCell = ut.getHSideCell(cell),
  20714. nextCell = ut.getHSideCell(cell, true);
  20715. if (utils.isEmptyObject(range)) {
  20716. ut.deleteCol(cellInfo.colIndex);
  20717. } else {
  20718. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  20719. ut.deleteCol(range.beginColIndex);
  20720. }
  20721. }
  20722. var table = ut.table,
  20723. rng = this.selection.getRange();
  20724. if (!table.getElementsByTagName("td").length) {
  20725. var nextSibling = table.nextSibling;
  20726. domUtils.remove(table);
  20727. if (nextSibling) {
  20728. rng.setStart(nextSibling, 0).setCursor(false, true);
  20729. }
  20730. } else {
  20731. if (domUtils.inDoc(cell, this.document)) {
  20732. rng.setStart(cell, 0).setCursor(false, true);
  20733. } else {
  20734. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  20735. rng.selectNodeContents(nextCell).setCursor(false, true);
  20736. } else {
  20737. if (preCell && domUtils.inDoc(preCell, this.document)) {
  20738. rng.selectNodeContents(preCell).setCursor(true, true);
  20739. }
  20740. }
  20741. }
  20742. }
  20743. }
  20744. };
  20745. UE.commands["splittocells"] = {
  20746. queryCommandState: function() {
  20747. var tableItems = getTableItemsByRange(this),
  20748. cell = tableItems.cell;
  20749. if (!cell) return -1;
  20750. var ut = getUETable(tableItems.table);
  20751. if (ut.selectedTds.length > 0) return -1;
  20752. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1;
  20753. },
  20754. execCommand: function() {
  20755. var rng = this.selection.getRange(),
  20756. bk = rng.createBookmark(true);
  20757. var cell = getTableItemsByRange(this).cell,
  20758. ut = getUETable(cell);
  20759. ut.splitToCells(cell);
  20760. rng.moveToBookmark(bk).select();
  20761. }
  20762. };
  20763. UE.commands["splittorows"] = {
  20764. queryCommandState: function() {
  20765. var tableItems = getTableItemsByRange(this),
  20766. cell = tableItems.cell;
  20767. if (!cell) return -1;
  20768. var ut = getUETable(tableItems.table);
  20769. if (ut.selectedTds.length > 0) return -1;
  20770. return cell && cell.rowSpan > 1 ? 0 : -1;
  20771. },
  20772. execCommand: function() {
  20773. var rng = this.selection.getRange(),
  20774. bk = rng.createBookmark(true);
  20775. var cell = getTableItemsByRange(this).cell,
  20776. ut = getUETable(cell);
  20777. ut.splitToRows(cell);
  20778. rng.moveToBookmark(bk).select();
  20779. }
  20780. };
  20781. UE.commands["splittocols"] = {
  20782. queryCommandState: function() {
  20783. var tableItems = getTableItemsByRange(this),
  20784. cell = tableItems.cell;
  20785. if (!cell) return -1;
  20786. var ut = getUETable(tableItems.table);
  20787. if (ut.selectedTds.length > 0) return -1;
  20788. return cell && cell.colSpan > 1 ? 0 : -1;
  20789. },
  20790. execCommand: function() {
  20791. var rng = this.selection.getRange(),
  20792. bk = rng.createBookmark(true);
  20793. var cell = getTableItemsByRange(this).cell,
  20794. ut = getUETable(cell);
  20795. ut.splitToCols(cell);
  20796. rng.moveToBookmark(bk).select();
  20797. }
  20798. };
  20799. UE.commands["adaptbytext"] = UE.commands["adaptbywindow"] = {
  20800. queryCommandState: function() {
  20801. return getTableItemsByRange(this).table ? 0 : -1;
  20802. },
  20803. execCommand: function(cmd) {
  20804. var tableItems = getTableItemsByRange(this),
  20805. table = tableItems.table;
  20806. if (table) {
  20807. if (cmd == "adaptbywindow") {
  20808. resetTdWidth(table, this);
  20809. } else {
  20810. var cells = domUtils.getElementsByTagName(table, "td th");
  20811. utils.each(cells, function(cell) {
  20812. cell.removeAttribute("width");
  20813. });
  20814. table.removeAttribute("width");
  20815. }
  20816. }
  20817. }
  20818. };
  20819. //平均分配各列
  20820. UE.commands["averagedistributecol"] = {
  20821. queryCommandState: function() {
  20822. var ut = getUETableBySelected(this);
  20823. if (!ut) return -1;
  20824. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  20825. },
  20826. execCommand: function(cmd) {
  20827. var me = this,
  20828. ut = getUETableBySelected(me);
  20829. function getAverageWidth() {
  20830. var tb = ut.table,
  20831. averageWidth,
  20832. sumWidth = 0,
  20833. colsNum = 0,
  20834. tbAttr = getDefaultValue(me, tb);
  20835. if (ut.isFullRow()) {
  20836. sumWidth = tb.offsetWidth;
  20837. colsNum = ut.colsNum;
  20838. } else {
  20839. var begin = ut.cellsRange.beginColIndex,
  20840. end = ut.cellsRange.endColIndex,
  20841. node;
  20842. for (var i = begin; i <= end; ) {
  20843. node = ut.selectedTds[i];
  20844. sumWidth += node.offsetWidth;
  20845. i += node.colSpan;
  20846. colsNum += 1;
  20847. }
  20848. }
  20849. averageWidth =
  20850. Math.ceil(sumWidth / colsNum) -
  20851. tbAttr.tdBorder * 2 -
  20852. tbAttr.tdPadding * 2;
  20853. return averageWidth;
  20854. }
  20855. function setAverageWidth(averageWidth) {
  20856. utils.each(domUtils.getElementsByTagName(ut.table, "th"), function(
  20857. node
  20858. ) {
  20859. node.setAttribute("width", "");
  20860. });
  20861. var cells = ut.isFullRow()
  20862. ? domUtils.getElementsByTagName(ut.table, "td")
  20863. : ut.selectedTds;
  20864. utils.each(cells, function(node) {
  20865. if (node.colSpan == 1) {
  20866. node.setAttribute("width", averageWidth);
  20867. }
  20868. });
  20869. }
  20870. if (ut && ut.selectedTds.length) {
  20871. setAverageWidth(getAverageWidth());
  20872. }
  20873. }
  20874. };
  20875. //平均分配各行
  20876. UE.commands["averagedistributerow"] = {
  20877. queryCommandState: function() {
  20878. var ut = getUETableBySelected(this);
  20879. if (!ut) return -1;
  20880. if (ut.selectedTds && /th/gi.test(ut.selectedTds[0].tagName)) return -1;
  20881. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  20882. },
  20883. execCommand: function(cmd) {
  20884. var me = this,
  20885. ut = getUETableBySelected(me);
  20886. function getAverageHeight() {
  20887. var averageHeight,
  20888. rowNum,
  20889. sumHeight = 0,
  20890. tb = ut.table,
  20891. tbAttr = getDefaultValue(me, tb),
  20892. tdpadding = parseInt(
  20893. domUtils.getComputedStyle(
  20894. tb.getElementsByTagName("td")[0],
  20895. "padding-top"
  20896. )
  20897. );
  20898. if (ut.isFullCol()) {
  20899. var captionArr = domUtils.getElementsByTagName(tb, "caption"),
  20900. thArr = domUtils.getElementsByTagName(tb, "th"),
  20901. captionHeight,
  20902. thHeight;
  20903. if (captionArr.length > 0) {
  20904. captionHeight = captionArr[0].offsetHeight;
  20905. }
  20906. if (thArr.length > 0) {
  20907. thHeight = thArr[0].offsetHeight;
  20908. }
  20909. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0);
  20910. rowNum = thArr.length == 0 ? ut.rowsNum : ut.rowsNum - 1;
  20911. } else {
  20912. var begin = ut.cellsRange.beginRowIndex,
  20913. end = ut.cellsRange.endRowIndex,
  20914. count = 0,
  20915. trs = domUtils.getElementsByTagName(tb, "tr");
  20916. for (var i = begin; i <= end; i++) {
  20917. sumHeight += trs[i].offsetHeight;
  20918. count += 1;
  20919. }
  20920. rowNum = count;
  20921. }
  20922. //ie8下是混杂模式
  20923. if (browser.ie && browser.version < 9) {
  20924. averageHeight = Math.ceil(sumHeight / rowNum);
  20925. } else {
  20926. averageHeight =
  20927. Math.ceil(sumHeight / rowNum) - tbAttr.tdBorder * 2 - tdpadding * 2;
  20928. }
  20929. return averageHeight;
  20930. }
  20931. function setAverageHeight(averageHeight) {
  20932. var cells = ut.isFullCol()
  20933. ? domUtils.getElementsByTagName(ut.table, "td")
  20934. : ut.selectedTds;
  20935. utils.each(cells, function(node) {
  20936. if (node.rowSpan == 1) {
  20937. node.setAttribute("height", averageHeight);
  20938. }
  20939. });
  20940. }
  20941. if (ut && ut.selectedTds.length) {
  20942. setAverageHeight(getAverageHeight());
  20943. }
  20944. }
  20945. };
  20946. //单元格对齐方式
  20947. UE.commands["cellalignment"] = {
  20948. queryCommandState: function() {
  20949. return getTableItemsByRange(this).table ? 0 : -1;
  20950. },
  20951. execCommand: function(cmd, data) {
  20952. var me = this,
  20953. ut = getUETableBySelected(me);
  20954. if (!ut) {
  20955. var start = me.selection.getStart(),
  20956. cell =
  20957. start &&
  20958. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  20959. if (!/caption/gi.test(cell.tagName)) {
  20960. domUtils.setAttributes(cell, data);
  20961. } else {
  20962. cell.style.textAlign = data.align;
  20963. cell.style.verticalAlign = data.vAlign;
  20964. }
  20965. me.selection.getRange().setCursor(true);
  20966. } else {
  20967. utils.each(ut.selectedTds, function(cell) {
  20968. domUtils.setAttributes(cell, data);
  20969. });
  20970. }
  20971. },
  20972. /**
  20973. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  20974. * @see UE.UETable.getTableCellAlignState
  20975. */
  20976. queryCommandValue: function(cmd) {
  20977. var activeMenuCell = getTableItemsByRange(this).cell;
  20978. if (!activeMenuCell) {
  20979. activeMenuCell = getSelectedArr(this)[0];
  20980. }
  20981. if (!activeMenuCell) {
  20982. return null;
  20983. } else {
  20984. //获取同时选中的其他单元格
  20985. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds;
  20986. !cells.length && (cells = activeMenuCell);
  20987. return UE.UETable.getTableCellAlignState(cells);
  20988. }
  20989. }
  20990. };
  20991. //表格对齐方式
  20992. UE.commands["tablealignment"] = {
  20993. queryCommandState: function() {
  20994. if (browser.ie && browser.version < 8) {
  20995. return -1;
  20996. }
  20997. return getTableItemsByRange(this).table ? 0 : -1;
  20998. },
  20999. execCommand: function(cmd, value) {
  21000. var me = this,
  21001. start = me.selection.getStart(),
  21002. table = start && domUtils.findParentByTagName(start, ["table"], true);
  21003. if (table) {
  21004. table.setAttribute("align", value);
  21005. }
  21006. }
  21007. };
  21008. //表格属性
  21009. UE.commands["edittable"] = {
  21010. queryCommandState: function() {
  21011. return getTableItemsByRange(this).table ? 0 : -1;
  21012. },
  21013. execCommand: function(cmd, color) {
  21014. var rng = this.selection.getRange(),
  21015. table = domUtils.findParentByTagName(rng.startContainer, "table");
  21016. if (table) {
  21017. var arr = domUtils
  21018. .getElementsByTagName(table, "td")
  21019. .concat(
  21020. domUtils.getElementsByTagName(table, "th"),
  21021. domUtils.getElementsByTagName(table, "caption")
  21022. );
  21023. utils.each(arr, function(node) {
  21024. node.style.borderColor = color;
  21025. });
  21026. }
  21027. }
  21028. };
  21029. //单元格属性
  21030. UE.commands["edittd"] = {
  21031. queryCommandState: function() {
  21032. return getTableItemsByRange(this).table ? 0 : -1;
  21033. },
  21034. execCommand: function(cmd, bkColor) {
  21035. var me = this,
  21036. ut = getUETableBySelected(me);
  21037. if (!ut) {
  21038. var start = me.selection.getStart(),
  21039. cell =
  21040. start &&
  21041. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  21042. if (cell) {
  21043. cell.style.backgroundColor = bkColor;
  21044. }
  21045. } else {
  21046. utils.each(ut.selectedTds, function(cell) {
  21047. cell.style.backgroundColor = bkColor;
  21048. });
  21049. }
  21050. }
  21051. };
  21052. UE.commands["settablebackground"] = {
  21053. queryCommandState: function() {
  21054. return getSelectedArr(this).length > 1 ? 0 : -1;
  21055. },
  21056. execCommand: function(cmd, value) {
  21057. var cells, ut;
  21058. cells = getSelectedArr(this);
  21059. ut = getUETable(cells[0]);
  21060. ut.setBackground(cells, value);
  21061. }
  21062. };
  21063. UE.commands["cleartablebackground"] = {
  21064. queryCommandState: function() {
  21065. var cells = getSelectedArr(this);
  21066. if (!cells.length) return -1;
  21067. for (var i = 0, cell; (cell = cells[i++]); ) {
  21068. if (cell.style.backgroundColor !== "") return 0;
  21069. }
  21070. return -1;
  21071. },
  21072. execCommand: function() {
  21073. var cells = getSelectedArr(this),
  21074. ut = getUETable(cells[0]);
  21075. ut.removeBackground(cells);
  21076. }
  21077. };
  21078. UE.commands["interlacetable"] = UE.commands["uninterlacetable"] = {
  21079. queryCommandState: function(cmd) {
  21080. var table = getTableItemsByRange(this).table;
  21081. if (!table) return -1;
  21082. var interlaced = table.getAttribute("interlaced");
  21083. if (cmd == "interlacetable") {
  21084. //TODO 待定
  21085. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  21086. return interlaced === "enabled" ? -1 : 0;
  21087. } else {
  21088. return !interlaced || interlaced === "disabled" ? -1 : 0;
  21089. }
  21090. },
  21091. execCommand: function(cmd, classList) {
  21092. var table = getTableItemsByRange(this).table;
  21093. if (cmd == "interlacetable") {
  21094. table.setAttribute("interlaced", "enabled");
  21095. this.fireEvent("interlacetable", table, classList);
  21096. } else {
  21097. table.setAttribute("interlaced", "disabled");
  21098. this.fireEvent("uninterlacetable", table);
  21099. }
  21100. }
  21101. };
  21102. UE.commands["setbordervisible"] = {
  21103. queryCommandState: function(cmd) {
  21104. var table = getTableItemsByRange(this).table;
  21105. if (!table) return -1;
  21106. return 0;
  21107. },
  21108. execCommand: function() {
  21109. var table = getTableItemsByRange(this).table;
  21110. utils.each(domUtils.getElementsByTagName(table, "td"), function(td) {
  21111. td.style.borderWidth = "1px";
  21112. td.style.borderStyle = "solid";
  21113. });
  21114. }
  21115. };
  21116. function resetTdWidth(table, editor) {
  21117. var tds = domUtils.getElementsByTagName(table, "td th");
  21118. utils.each(tds, function(td) {
  21119. td.removeAttribute("width");
  21120. });
  21121. table.setAttribute(
  21122. "width",
  21123. getTableWidth(editor, true, getDefaultValue(editor, table))
  21124. );
  21125. var tdsWidths = [];
  21126. setTimeout(function() {
  21127. utils.each(tds, function(td) {
  21128. td.colSpan == 1 && tdsWidths.push(td.offsetWidth);
  21129. });
  21130. utils.each(tds, function(td, i) {
  21131. td.colSpan == 1 && td.setAttribute("width", tdsWidths[i] + "");
  21132. });
  21133. }, 0);
  21134. }
  21135. function getTableWidth(editor, needIEHack, defaultValue) {
  21136. var body = editor.body;
  21137. return (
  21138. body.offsetWidth -
  21139. (needIEHack
  21140. ? parseInt(domUtils.getComputedStyle(body, "margin-left"), 10) * 2
  21141. : 0) -
  21142. defaultValue.tableBorder * 2 -
  21143. (editor.options.offsetWidth || 0)
  21144. );
  21145. }
  21146. function getSelectedArr(editor) {
  21147. var cell = getTableItemsByRange(editor).cell;
  21148. if (cell) {
  21149. var ut = getUETable(cell);
  21150. return ut.selectedTds.length ? ut.selectedTds : [cell];
  21151. } else {
  21152. return [];
  21153. }
  21154. }
  21155. })();
  21156. // plugins/table.action.js
  21157. /**
  21158. * Created with JetBrains PhpStorm.
  21159. * User: taoqili
  21160. * Date: 12-10-12
  21161. * Time: 上午10:05
  21162. * To change this template use File | Settings | File Templates.
  21163. */
  21164. UE.plugins["table"] = function() {
  21165. var me = this,
  21166. tabTimer = null,
  21167. //拖动计时器
  21168. tableDragTimer = null,
  21169. //双击计时器
  21170. tableResizeTimer = null,
  21171. //单元格最小宽度
  21172. cellMinWidth = 5,
  21173. isInResizeBuffer = false,
  21174. //单元格边框大小
  21175. cellBorderWidth = 5,
  21176. //鼠标偏移距离
  21177. offsetOfTableCell = 10,
  21178. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  21179. singleClickState = 0,
  21180. userActionStatus = null,
  21181. //双击允许的时间范围
  21182. dblclickTime = 360,
  21183. UT = UE.UETable,
  21184. getUETable = function(tdOrTable) {
  21185. return UT.getUETable(tdOrTable);
  21186. },
  21187. getUETableBySelected = function(editor) {
  21188. return UT.getUETableBySelected(editor);
  21189. },
  21190. getDefaultValue = function(editor, table) {
  21191. return UT.getDefaultValue(editor, table);
  21192. },
  21193. removeSelectedClass = function(cells) {
  21194. return UT.removeSelectedClass(cells);
  21195. };
  21196. function showError(e) {
  21197. // throw e;
  21198. }
  21199. me.ready(function() {
  21200. var me = this;
  21201. var orgGetText = me.selection.getText;
  21202. me.selection.getText = function() {
  21203. var table = getUETableBySelected(me);
  21204. if (table) {
  21205. var str = "";
  21206. utils.each(table.selectedTds, function(td) {
  21207. str += td[browser.ie ? "innerText" : "textContent"];
  21208. });
  21209. return str;
  21210. } else {
  21211. return orgGetText.call(me.selection);
  21212. }
  21213. };
  21214. });
  21215. //处理拖动及框选相关方法
  21216. var startTd = null, //鼠标按下时的锚点td
  21217. currentTd = null, //当前鼠标经过时的td
  21218. onDrag = "", //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  21219. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  21220. dragButton = null,
  21221. dragOver = false,
  21222. dragLine = null, //模拟的拖动线
  21223. dragTd = null; //发生拖动的目标td
  21224. var mousedown = false,
  21225. //todo 判断混乱模式
  21226. needIEHack = true;
  21227. me.setOpt({
  21228. maxColNum: 20,
  21229. maxRowNum: 100,
  21230. defaultCols: 5,
  21231. defaultRows: 5,
  21232. tdvalign: "top",
  21233. cursorpath: me.options.UEDITOR_HOME_URL + "themes/" + me.options.theme + "/images/cursor_",
  21234. tableDragable: false,
  21235. classList: [
  21236. "ue-table-interlace-color-single",
  21237. "ue-table-interlace-color-double"
  21238. ]
  21239. });
  21240. me.getUETable = getUETable;
  21241. var commands = {
  21242. deletetable: 1,
  21243. inserttable: 1,
  21244. cellvalign: 1,
  21245. insertcaption: 1,
  21246. deletecaption: 1,
  21247. inserttitle: 1,
  21248. deletetitle: 1,
  21249. mergeright: 1,
  21250. mergedown: 1,
  21251. mergecells: 1,
  21252. insertrow: 1,
  21253. insertrownext: 1,
  21254. deleterow: 1,
  21255. insertcol: 1,
  21256. insertcolnext: 1,
  21257. deletecol: 1,
  21258. splittocells: 1,
  21259. splittorows: 1,
  21260. splittocols: 1,
  21261. adaptbytext: 1,
  21262. adaptbywindow: 1,
  21263. adaptbycustomer: 1,
  21264. insertparagraph: 1,
  21265. insertparagraphbeforetable: 1,
  21266. averagedistributecol: 1,
  21267. averagedistributerow: 1
  21268. };
  21269. me.ready(function() {
  21270. utils.cssRule(
  21271. "table",
  21272. //选中的td上的样式
  21273. ".selectTdClass{background-color:#edf5fa !important}" +
  21274. "table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}" +
  21275. //插入的表格的默认样式
  21276. "table{margin-bottom:10px;border-collapse:collapse;display:table;}" +
  21277. "td,th{padding: 5px 10px;border: 1px solid #DDD;}" +
  21278. "caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}" +
  21279. "th{border-top:1px solid #BBB;background-color:#F7F7F7;}" +
  21280. "table tr.firstRow th{border-top-width:2px;}" +
  21281. ".ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }" +
  21282. "td p{margin:0;padding:0;}",
  21283. me.document
  21284. );
  21285. var tableCopyList, isFullCol, isFullRow;
  21286. //注册del/backspace事件
  21287. me.addListener("keydown", function(cmd, evt) {
  21288. var me = this;
  21289. var keyCode = evt.keyCode || evt.which;
  21290. if (keyCode == 8) {
  21291. var ut = getUETableBySelected(me);
  21292. if (ut && ut.selectedTds.length) {
  21293. if (ut.isFullCol()) {
  21294. me.execCommand("deletecol");
  21295. } else if (ut.isFullRow()) {
  21296. me.execCommand("deleterow");
  21297. } else {
  21298. me.fireEvent("delcells");
  21299. }
  21300. domUtils.preventDefault(evt);
  21301. }
  21302. var caption = domUtils.findParentByTagName(
  21303. me.selection.getStart(),
  21304. "caption",
  21305. true
  21306. ),
  21307. range = me.selection.getRange();
  21308. if (range.collapsed && caption && isEmptyBlock(caption)) {
  21309. me.fireEvent("saveScene");
  21310. var table = caption.parentNode;
  21311. domUtils.remove(caption);
  21312. if (table) {
  21313. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  21314. }
  21315. me.fireEvent("saveScene");
  21316. }
  21317. }
  21318. if (keyCode == 46) {
  21319. ut = getUETableBySelected(me);
  21320. if (ut) {
  21321. me.fireEvent("saveScene");
  21322. for (var i = 0, ci; (ci = ut.selectedTds[i++]); ) {
  21323. domUtils.fillNode(me.document, ci);
  21324. }
  21325. me.fireEvent("saveScene");
  21326. domUtils.preventDefault(evt);
  21327. }
  21328. }
  21329. if (keyCode == 13) {
  21330. var rng = me.selection.getRange(),
  21331. caption = domUtils.findParentByTagName(
  21332. rng.startContainer,
  21333. "caption",
  21334. true
  21335. );
  21336. if (caption) {
  21337. var table = domUtils.findParentByTagName(caption, "table");
  21338. if (!rng.collapsed) {
  21339. rng.deleteContents();
  21340. me.fireEvent("saveScene");
  21341. } else {
  21342. if (caption) {
  21343. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  21344. }
  21345. }
  21346. domUtils.preventDefault(evt);
  21347. return;
  21348. }
  21349. if (rng.collapsed) {
  21350. var table = domUtils.findParentByTagName(rng.startContainer, "table");
  21351. if (table) {
  21352. var cell = table.rows[0].cells[0],
  21353. start = domUtils.findParentByTagName(
  21354. me.selection.getStart(),
  21355. ["td", "th"],
  21356. true
  21357. ),
  21358. preNode = table.previousSibling;
  21359. if (
  21360. cell === start &&
  21361. (!preNode ||
  21362. (preNode.nodeType == 1 && preNode.tagName == "TABLE")) &&
  21363. domUtils.isStartInblock(rng)
  21364. ) {
  21365. var first = domUtils.findParent(
  21366. me.selection.getStart(),
  21367. function(n) {
  21368. return domUtils.isBlockElm(n);
  21369. },
  21370. true
  21371. );
  21372. if (
  21373. first &&
  21374. (/t(h|d)/i.test(first.tagName) || first === start.firstChild)
  21375. ) {
  21376. me.execCommand("insertparagraphbeforetable");
  21377. domUtils.preventDefault(evt);
  21378. }
  21379. }
  21380. }
  21381. }
  21382. }
  21383. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == "67") {
  21384. tableCopyList = null;
  21385. var ut = getUETableBySelected(me);
  21386. if (ut) {
  21387. var tds = ut.selectedTds;
  21388. isFullCol = ut.isFullCol();
  21389. isFullRow = ut.isFullRow();
  21390. tableCopyList = [[ut.cloneCell(tds[0], null, true)]];
  21391. for (var i = 1, ci; (ci = tds[i]); i++) {
  21392. if (ci.parentNode !== tds[i - 1].parentNode) {
  21393. tableCopyList.push([ut.cloneCell(ci, null, true)]);
  21394. } else {
  21395. tableCopyList[tableCopyList.length - 1].push(
  21396. ut.cloneCell(ci, null, true)
  21397. );
  21398. }
  21399. }
  21400. }
  21401. }
  21402. });
  21403. me.addListener("tablehasdeleted", function() {
  21404. toggleDraggableState(this, false, "", null);
  21405. if (dragButton) domUtils.remove(dragButton);
  21406. });
  21407. me.addListener("beforepaste", function(cmd, html) {
  21408. var me = this;
  21409. var rng = me.selection.getRange();
  21410. if (domUtils.findParentByTagName(rng.startContainer, "caption", true)) {
  21411. var div = me.document.createElement("div");
  21412. div.innerHTML = html.html;
  21413. //trace:3729
  21414. html.html = div[browser.ie9below ? "innerText" : "textContent"];
  21415. return;
  21416. }
  21417. var table = getUETableBySelected(me);
  21418. if (tableCopyList) {
  21419. me.fireEvent("saveScene");
  21420. var rng = me.selection.getRange();
  21421. var td = domUtils.findParentByTagName(
  21422. rng.startContainer,
  21423. ["td", "th"],
  21424. true
  21425. ),
  21426. tmpNode,
  21427. preNode;
  21428. if (td) {
  21429. var ut = getUETable(td);
  21430. if (isFullRow) {
  21431. var rowIndex = ut.getCellInfo(td).rowIndex;
  21432. if (td.tagName == "TH") {
  21433. rowIndex++;
  21434. }
  21435. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21436. var tr = ut.insertRow(rowIndex++, "td");
  21437. for (var j = 0, cj; (cj = ci[j]); j++) {
  21438. var cell = tr.cells[j];
  21439. if (!cell) {
  21440. cell = tr.insertCell(j);
  21441. }
  21442. cell.innerHTML = cj.innerHTML;
  21443. cj.getAttribute("width") &&
  21444. cell.setAttribute("width", cj.getAttribute("width"));
  21445. cj.getAttribute("vAlign") &&
  21446. cell.setAttribute("vAlign", cj.getAttribute("vAlign"));
  21447. cj.getAttribute("align") &&
  21448. cell.setAttribute("align", cj.getAttribute("align"));
  21449. cj.style.cssText && (cell.style.cssText = cj.style.cssText);
  21450. }
  21451. for (var j = 0, cj; (cj = tr.cells[j]); j++) {
  21452. if (!ci[j]) break;
  21453. cj.innerHTML = ci[j].innerHTML;
  21454. ci[j].getAttribute("width") &&
  21455. cj.setAttribute("width", ci[j].getAttribute("width"));
  21456. ci[j].getAttribute("vAlign") &&
  21457. cj.setAttribute("vAlign", ci[j].getAttribute("vAlign"));
  21458. ci[j].getAttribute("align") &&
  21459. cj.setAttribute("align", ci[j].getAttribute("align"));
  21460. ci[j].style.cssText && (cj.style.cssText = ci[j].style.cssText);
  21461. }
  21462. }
  21463. } else {
  21464. if (isFullCol) {
  21465. cellInfo = ut.getCellInfo(td);
  21466. var maxColNum = 0;
  21467. for (var j = 0, ci = tableCopyList[0], cj; (cj = ci[j++]); ) {
  21468. maxColNum += cj.colSpan || 1;
  21469. }
  21470. me.__hasEnterExecCommand = true;
  21471. for (i = 0; i < maxColNum; i++) {
  21472. me.execCommand("insertcol");
  21473. }
  21474. me.__hasEnterExecCommand = false;
  21475. td = ut.table.rows[0].cells[cellInfo.cellIndex];
  21476. if (td.tagName == "TH") {
  21477. td = ut.table.rows[1].cells[cellInfo.cellIndex];
  21478. }
  21479. }
  21480. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21481. tmpNode = td;
  21482. for (var j = 0, cj; (cj = ci[j++]); ) {
  21483. if (td) {
  21484. td.innerHTML = cj.innerHTML;
  21485. //todo 定制处理
  21486. cj.getAttribute("width") &&
  21487. td.setAttribute("width", cj.getAttribute("width"));
  21488. cj.getAttribute("vAlign") &&
  21489. td.setAttribute("vAlign", cj.getAttribute("vAlign"));
  21490. cj.getAttribute("align") &&
  21491. td.setAttribute("align", cj.getAttribute("align"));
  21492. cj.style.cssText && (td.style.cssText = cj.style.cssText);
  21493. preNode = td;
  21494. td = td.nextSibling;
  21495. } else {
  21496. var cloneTd = cj.cloneNode(true);
  21497. domUtils.removeAttributes(cloneTd, [
  21498. "class",
  21499. "rowSpan",
  21500. "colSpan"
  21501. ]);
  21502. preNode.parentNode.appendChild(cloneTd);
  21503. }
  21504. }
  21505. td = ut.getNextCell(tmpNode, true, true);
  21506. if (!tableCopyList[i]) break;
  21507. if (!td) {
  21508. var cellInfo = ut.getCellInfo(tmpNode);
  21509. ut.table.insertRow(ut.table.rows.length);
  21510. ut.update();
  21511. td = ut.getVSideCell(tmpNode, true);
  21512. }
  21513. }
  21514. }
  21515. ut.update();
  21516. } else {
  21517. table = me.document.createElement("table");
  21518. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21519. var tr = table.insertRow(table.rows.length);
  21520. for (var j = 0, cj; (cj = ci[j++]); ) {
  21521. cloneTd = UT.cloneCell(cj, null, true);
  21522. domUtils.removeAttributes(cloneTd, ["class"]);
  21523. tr.appendChild(cloneTd);
  21524. }
  21525. if (j == 2 && cloneTd.rowSpan > 1) {
  21526. cloneTd.rowSpan = 1;
  21527. }
  21528. }
  21529. var defaultValue = getDefaultValue(me),
  21530. width =
  21531. me.body.offsetWidth -
  21532. (needIEHack
  21533. ? parseInt(
  21534. domUtils.getComputedStyle(me.body, "margin-left"),
  21535. 10
  21536. ) * 2
  21537. : 0) -
  21538. defaultValue.tableBorder * 2 -
  21539. (me.options.offsetWidth || 0);
  21540. me.execCommand(
  21541. "insertHTML",
  21542. "<table " +
  21543. (isFullCol && isFullRow ? 'width="' + width + '"' : "") +
  21544. ">" +
  21545. table.innerHTML
  21546. .replace(/>\s*</g, "><")
  21547. .replace(/\bth\b/gi, "td") +
  21548. "</table>"
  21549. );
  21550. }
  21551. me.fireEvent("contentchange");
  21552. me.fireEvent("saveScene");
  21553. html.html = "";
  21554. return true;
  21555. } else {
  21556. var div = me.document.createElement("div"),
  21557. tables;
  21558. div.innerHTML = html.html;
  21559. tables = div.getElementsByTagName("table");
  21560. if (domUtils.findParentByTagName(me.selection.getStart(), "table")) {
  21561. utils.each(tables, function(t) {
  21562. domUtils.remove(t);
  21563. });
  21564. if (
  21565. domUtils.findParentByTagName(
  21566. me.selection.getStart(),
  21567. "caption",
  21568. true
  21569. )
  21570. ) {
  21571. div.innerHTML = div[browser.ie ? "innerText" : "textContent"];
  21572. }
  21573. } else {
  21574. utils.each(tables, function(table) {
  21575. removeStyleSize(table, true);
  21576. domUtils.removeAttributes(table, ["style", "border"]);
  21577. utils.each(domUtils.getElementsByTagName(table, "td"), function(
  21578. td
  21579. ) {
  21580. if (isEmptyBlock(td)) {
  21581. domUtils.fillNode(me.document, td);
  21582. }
  21583. removeStyleSize(td, true);
  21584. // domUtils.removeAttributes(td, ['style'])
  21585. });
  21586. });
  21587. }
  21588. html.html = div.innerHTML;
  21589. }
  21590. });
  21591. me.addListener("afterpaste", function() {
  21592. utils.each(domUtils.getElementsByTagName(me.body, "table"), function(
  21593. table
  21594. ) {
  21595. if (table.offsetWidth > me.body.offsetWidth) {
  21596. var defaultValue = getDefaultValue(me, table);
  21597. table.style.width =
  21598. me.body.offsetWidth -
  21599. (needIEHack
  21600. ? parseInt(
  21601. domUtils.getComputedStyle(me.body, "margin-left"),
  21602. 10
  21603. ) * 2
  21604. : 0) -
  21605. defaultValue.tableBorder * 2 -
  21606. (me.options.offsetWidth || 0) +
  21607. "px";
  21608. }
  21609. });
  21610. });
  21611. me.addListener("blur", function() {
  21612. tableCopyList = null;
  21613. });
  21614. var timer;
  21615. me.addListener("keydown", function() {
  21616. clearTimeout(timer);
  21617. timer = setTimeout(function() {
  21618. var rng = me.selection.getRange(),
  21619. cell = domUtils.findParentByTagName(
  21620. rng.startContainer,
  21621. ["th", "td"],
  21622. true
  21623. );
  21624. if (cell) {
  21625. var table = cell.parentNode.parentNode.parentNode;
  21626. if (table.offsetWidth > table.getAttribute("width")) {
  21627. cell.style.wordBreak = "break-all";
  21628. }
  21629. }
  21630. }, 100);
  21631. });
  21632. me.addListener("selectionchange", function() {
  21633. toggleDraggableState(me, false, "", null);
  21634. });
  21635. //内容变化时触发索引更新
  21636. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  21637. me.addListener("contentchange", function() {
  21638. var me = this;
  21639. //尽可能排除一些不需要更新的状况
  21640. hideDragLine(me);
  21641. if (getUETableBySelected(me)) return;
  21642. var rng = me.selection.getRange();
  21643. var start = rng.startContainer;
  21644. start = domUtils.findParentByTagName(start, ["td", "th"], true);
  21645. utils.each(domUtils.getElementsByTagName(me.document, "table"), function(
  21646. table
  21647. ) {
  21648. if (me.fireEvent("excludetable", table) === true) return;
  21649. table.ueTable = new UT(table);
  21650. //trace:3742
  21651. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  21652. //
  21653. // if (domUtils.isEmptyBlock(td) && td !== start) {
  21654. // domUtils.fillNode(me.document, td);
  21655. // if (browser.ie && browser.version == 6) {
  21656. // td.innerHTML = '&nbsp;'
  21657. // }
  21658. // }
  21659. // });
  21660. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  21661. // if (domUtils.isEmptyBlock(th) && th !== start) {
  21662. // domUtils.fillNode(me.document, th);
  21663. // if (browser.ie && browser.version == 6) {
  21664. // th.innerHTML = '&nbsp;'
  21665. // }
  21666. // }
  21667. // });
  21668. table.onmouseover = function() {
  21669. me.fireEvent("tablemouseover", table);
  21670. };
  21671. table.onmousemove = function() {
  21672. me.fireEvent("tablemousemove", table);
  21673. me.options.tableDragable && toggleDragButton(true, this, me);
  21674. utils.defer(function() {
  21675. me.fireEvent("contentchange", 50);
  21676. }, true);
  21677. };
  21678. table.onmouseout = function() {
  21679. me.fireEvent("tablemouseout", table);
  21680. toggleDraggableState(me, false, "", null);
  21681. hideDragLine(me);
  21682. };
  21683. table.onclick = function(evt) {
  21684. evt = me.window.event || evt;
  21685. var target = getParentTdOrTh(evt.target || evt.srcElement);
  21686. if (!target) return;
  21687. var ut = getUETable(target),
  21688. table = ut.table,
  21689. cellInfo = ut.getCellInfo(target),
  21690. cellsRange,
  21691. rng = me.selection.getRange();
  21692. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  21693. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  21694. // ut.setSelected(cellsRange);
  21695. // return;
  21696. // }
  21697. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  21698. //
  21699. // return;
  21700. // }
  21701. if (inTableSide(table, target, evt, true)) {
  21702. var endTdCol = ut.getCell(
  21703. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex,
  21704. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex
  21705. );
  21706. if (evt.shiftKey && ut.selectedTds.length) {
  21707. if (ut.selectedTds[0] !== endTdCol) {
  21708. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol);
  21709. ut.setSelected(cellsRange);
  21710. } else {
  21711. rng && rng.selectNodeContents(endTdCol).select();
  21712. }
  21713. } else {
  21714. if (target !== endTdCol) {
  21715. cellsRange = ut.getCellsRange(target, endTdCol);
  21716. ut.setSelected(cellsRange);
  21717. } else {
  21718. rng && rng.selectNodeContents(endTdCol).select();
  21719. }
  21720. }
  21721. return;
  21722. }
  21723. if (inTableSide(table, target, evt)) {
  21724. var endTdRow = ut.getCell(
  21725. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex,
  21726. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex
  21727. );
  21728. if (evt.shiftKey && ut.selectedTds.length) {
  21729. if (ut.selectedTds[0] !== endTdRow) {
  21730. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow);
  21731. ut.setSelected(cellsRange);
  21732. } else {
  21733. rng && rng.selectNodeContents(endTdRow).select();
  21734. }
  21735. } else {
  21736. if (target !== endTdRow) {
  21737. cellsRange = ut.getCellsRange(target, endTdRow);
  21738. ut.setSelected(cellsRange);
  21739. } else {
  21740. rng && rng.selectNodeContents(endTdRow).select();
  21741. }
  21742. }
  21743. }
  21744. };
  21745. });
  21746. switchBorderColor(me, true);
  21747. });
  21748. domUtils.on(me.document, "mousemove", mouseMoveEvent);
  21749. domUtils.on(me.document, "mouseout", function(evt) {
  21750. var target = evt.target || evt.srcElement;
  21751. if (target.tagName == "TABLE") {
  21752. toggleDraggableState(me, false, "", null);
  21753. }
  21754. });
  21755. /**
  21756. * 表格隔行变色
  21757. */
  21758. me.addListener("interlacetable", function(type, table, classList) {
  21759. if (!table) return;
  21760. var me = this,
  21761. rows = table.rows,
  21762. len = rows.length,
  21763. getClass = function(list, index, repeat) {
  21764. return list[index]
  21765. ? list[index]
  21766. : repeat ? list[index % list.length] : "";
  21767. };
  21768. for (var i = 0; i < len; i++) {
  21769. rows[i].className = getClass(
  21770. classList || me.options.classList,
  21771. i,
  21772. true
  21773. );
  21774. }
  21775. });
  21776. me.addListener("uninterlacetable", function(type, table) {
  21777. if (!table) return;
  21778. var me = this,
  21779. rows = table.rows,
  21780. classList = me.options.classList,
  21781. len = rows.length;
  21782. for (var i = 0; i < len; i++) {
  21783. domUtils.removeClasses(rows[i], classList);
  21784. }
  21785. });
  21786. me.addListener("mousedown", mouseDownEvent);
  21787. me.addListener("mouseup", mouseUpEvent);
  21788. //拖动的时候触发mouseup
  21789. domUtils.on(me.body, "dragstart", function(evt) {
  21790. mouseUpEvent.call(me, "dragstart", evt);
  21791. });
  21792. me.addOutputRule(function(root) {
  21793. utils.each(root.getNodesByTagName("div"), function(n) {
  21794. if (n.getAttr("id") == "ue_tableDragLine") {
  21795. n.parentNode.removeChild(n);
  21796. }
  21797. });
  21798. });
  21799. var currentRowIndex = 0;
  21800. me.addListener("mousedown", function() {
  21801. currentRowIndex = 0;
  21802. });
  21803. me.addListener("tabkeydown", function() {
  21804. var range = this.selection.getRange(),
  21805. common = range.getCommonAncestor(true, true),
  21806. table = domUtils.findParentByTagName(common, "table");
  21807. if (table) {
  21808. if (domUtils.findParentByTagName(common, "caption", true)) {
  21809. var cell = domUtils.getElementsByTagName(table, "th td");
  21810. if (cell && cell.length) {
  21811. range.setStart(cell[0], 0).setCursor(false, true);
  21812. }
  21813. } else {
  21814. var cell = domUtils.findParentByTagName(common, ["td", "th"], true),
  21815. ua = getUETable(cell);
  21816. currentRowIndex = cell.rowSpan > 1
  21817. ? currentRowIndex
  21818. : ua.getCellInfo(cell).rowIndex;
  21819. var nextCell = ua.getTabNextCell(cell, currentRowIndex);
  21820. if (nextCell) {
  21821. if (isEmptyBlock(nextCell)) {
  21822. range.setStart(nextCell, 0).setCursor(false, true);
  21823. } else {
  21824. range.selectNodeContents(nextCell).select();
  21825. }
  21826. } else {
  21827. me.fireEvent("saveScene");
  21828. me.__hasEnterExecCommand = true;
  21829. this.execCommand("insertrownext");
  21830. me.__hasEnterExecCommand = false;
  21831. range = this.selection.getRange();
  21832. range
  21833. .setStart(table.rows[table.rows.length - 1].cells[0], 0)
  21834. .setCursor();
  21835. me.fireEvent("saveScene");
  21836. }
  21837. }
  21838. return true;
  21839. }
  21840. });
  21841. browser.ie &&
  21842. me.addListener("selectionchange", function() {
  21843. toggleDraggableState(this, false, "", null);
  21844. });
  21845. me.addListener("keydown", function(type, evt) {
  21846. var me = this;
  21847. //处理在表格的最后一个输入tab产生新的表格
  21848. var keyCode = evt.keyCode || evt.which;
  21849. if (keyCode == 8 || keyCode == 46) {
  21850. return;
  21851. }
  21852. var notCtrlKey =
  21853. !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey;
  21854. notCtrlKey &&
  21855. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td"));
  21856. var ut = getUETableBySelected(me);
  21857. if (!ut) return;
  21858. notCtrlKey && ut.clearSelected();
  21859. });
  21860. me.addListener("beforegetcontent", function() {
  21861. switchBorderColor(this, false);
  21862. browser.ie &&
  21863. utils.each(this.document.getElementsByTagName("caption"), function(ci) {
  21864. if (domUtils.isEmptyNode(ci)) {
  21865. ci.innerHTML = "&nbsp;";
  21866. }
  21867. });
  21868. });
  21869. me.addListener("aftergetcontent", function() {
  21870. switchBorderColor(this, true);
  21871. });
  21872. me.addListener("getAllHtml", function() {
  21873. removeSelectedClass(me.document.getElementsByTagName("td"));
  21874. });
  21875. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  21876. me.addListener("fullscreenchanged", function(type, fullscreen) {
  21877. if (!fullscreen) {
  21878. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  21879. tables = domUtils.getElementsByTagName(this.body, "table");
  21880. utils.each(tables, function(table) {
  21881. if (table.offsetWidth < me.body.offsetWidth) return false;
  21882. var tds = domUtils.getElementsByTagName(table, "td"),
  21883. backWidths = [];
  21884. utils.each(tds, function(td) {
  21885. backWidths.push(td.offsetWidth);
  21886. });
  21887. for (var i = 0, td; (td = tds[i]); i++) {
  21888. td.setAttribute("width", Math.floor(backWidths[i] * ratio));
  21889. }
  21890. table.setAttribute(
  21891. "width",
  21892. Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me)))
  21893. );
  21894. });
  21895. }
  21896. });
  21897. //重写execCommand命令,用于处理框选时的处理
  21898. var oldExecCommand = me.execCommand;
  21899. me.execCommand = function(cmd, datatat) {
  21900. var me = this,
  21901. args = arguments;
  21902. cmd = cmd.toLowerCase();
  21903. var ut = getUETableBySelected(me),
  21904. tds,
  21905. range = new dom.Range(me.document),
  21906. cmdFun = me.commands[cmd] || UE.commands[cmd],
  21907. result;
  21908. if (!cmdFun) return;
  21909. if (
  21910. ut &&
  21911. !commands[cmd] &&
  21912. !cmdFun.notNeedUndo &&
  21913. !me.__hasEnterExecCommand
  21914. ) {
  21915. me.__hasEnterExecCommand = true;
  21916. me.fireEvent("beforeexeccommand", cmd);
  21917. tds = ut.selectedTds;
  21918. var lastState = -2,
  21919. lastValue = -2,
  21920. value,
  21921. state;
  21922. for (var i = 0, td; (td = tds[i]); i++) {
  21923. if (isEmptyBlock(td)) {
  21924. range.setStart(td, 0).setCursor(false, true);
  21925. } else {
  21926. range.selectNode(td).select(true);
  21927. }
  21928. state = me.queryCommandState(cmd);
  21929. value = me.queryCommandValue(cmd);
  21930. if (state != -1) {
  21931. if (lastState !== state || lastValue !== value) {
  21932. me._ignoreContentChange = true;
  21933. result = oldExecCommand.apply(me, arguments);
  21934. me._ignoreContentChange = false;
  21935. }
  21936. lastState = me.queryCommandState(cmd);
  21937. lastValue = me.queryCommandValue(cmd);
  21938. if (domUtils.isEmptyBlock(td)) {
  21939. domUtils.fillNode(me.document, td);
  21940. }
  21941. }
  21942. }
  21943. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true);
  21944. me.fireEvent("contentchange");
  21945. me.fireEvent("afterexeccommand", cmd);
  21946. me.__hasEnterExecCommand = false;
  21947. me._selectionChange();
  21948. } else {
  21949. result = oldExecCommand.apply(me, arguments);
  21950. }
  21951. return result;
  21952. };
  21953. });
  21954. /**
  21955. * 删除obj的宽高style,改成属性宽高
  21956. * @param obj
  21957. * @param replaceToProperty
  21958. */
  21959. function removeStyleSize(obj, replaceToProperty) {
  21960. removeStyle(obj, "width", true);
  21961. removeStyle(obj, "height", true);
  21962. }
  21963. function removeStyle(obj, styleName, replaceToProperty) {
  21964. if (obj.style[styleName]) {
  21965. replaceToProperty &&
  21966. obj.setAttribute(styleName, parseInt(obj.style[styleName], 10));
  21967. obj.style[styleName] = "";
  21968. }
  21969. }
  21970. function getParentTdOrTh(ele) {
  21971. if (ele.tagName == "TD" || ele.tagName == "TH") return ele;
  21972. var td;
  21973. if (
  21974. (td =
  21975. domUtils.findParentByTagName(ele, "td", true) ||
  21976. domUtils.findParentByTagName(ele, "th", true))
  21977. )
  21978. return td;
  21979. return null;
  21980. }
  21981. function isEmptyBlock(node) {
  21982. var reg = new RegExp(domUtils.fillChar, "g");
  21983. if (
  21984. node[browser.ie ? "innerText" : "textContent"]
  21985. .replace(/^\s*$/, "")
  21986. .replace(reg, "").length > 0
  21987. ) {
  21988. return 0;
  21989. }
  21990. for (var n in dtd.$isNotEmpty) {
  21991. if (node.getElementsByTagName(n).length) {
  21992. return 0;
  21993. }
  21994. }
  21995. return 1;
  21996. }
  21997. function mouseCoords(evt) {
  21998. if (evt.pageX || evt.pageY) {
  21999. return { x: evt.pageX, y: evt.pageY };
  22000. }
  22001. return {
  22002. x:
  22003. evt.clientX + me.document.body.scrollLeft - me.document.body.clientLeft,
  22004. y: evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  22005. };
  22006. }
  22007. function mouseMoveEvent(evt) {
  22008. if (isEditorDisabled()) {
  22009. return;
  22010. }
  22011. try {
  22012. //普通状态下鼠标移动
  22013. var target = getParentTdOrTh(evt.target || evt.srcElement),
  22014. pos;
  22015. //区分用户的行为是拖动还是双击
  22016. if (isInResizeBuffer) {
  22017. me.body.style.webkitUserSelect = "none";
  22018. if (
  22019. Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell ||
  22020. Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell
  22021. ) {
  22022. clearTableDragTimer();
  22023. isInResizeBuffer = false;
  22024. singleClickState = 0;
  22025. //drag action
  22026. tableBorderDrag(evt);
  22027. }
  22028. }
  22029. //修改单元格大小时的鼠标移动
  22030. if (onDrag && dragTd) {
  22031. singleClickState = 0;
  22032. me.body.style.webkitUserSelect = "none";
  22033. me.selection.getNative()[
  22034. browser.ie9below ? "empty" : "removeAllRanges"
  22035. ]();
  22036. pos = mouseCoords(evt);
  22037. toggleDraggableState(me, true, onDrag, pos, target);
  22038. if (onDrag == "h") {
  22039. dragLine.style.left = getPermissionX(dragTd, evt) + "px";
  22040. } else if (onDrag == "v") {
  22041. dragLine.style.top = getPermissionY(dragTd, evt) + "px";
  22042. }
  22043. return;
  22044. }
  22045. //当鼠标处于table上时,修改移动过程中的光标状态
  22046. if (target) {
  22047. //针对使用table作为容器的组件不触发拖拽效果
  22048. if (me.fireEvent("excludetable", target) === true) return;
  22049. pos = mouseCoords(evt);
  22050. var state = getRelation(target, pos),
  22051. table = domUtils.findParentByTagName(target, "table", true);
  22052. if (inTableSide(table, target, evt, true)) {
  22053. if (me.fireEvent("excludetable", table) === true) return;
  22054. me.body.style.cursor =
  22055. "url(" + me.options.cursorpath + "h.png),pointer";
  22056. } else if (inTableSide(table, target, evt)) {
  22057. if (me.fireEvent("excludetable", table) === true) return;
  22058. me.body.style.cursor =
  22059. "url(" + me.options.cursorpath + "v.png),pointer";
  22060. } else {
  22061. me.body.style.cursor = "text";
  22062. var curCell = target;
  22063. if (/\d/.test(state)) {
  22064. state = state.replace(/\d/, "");
  22065. target = getUETable(target).getPreviewCell(target, state == "v");
  22066. }
  22067. //位于第一行的顶部或者第一列的左边时不可拖动
  22068. toggleDraggableState(
  22069. me,
  22070. target ? !!state : false,
  22071. target ? state : "",
  22072. pos,
  22073. target
  22074. );
  22075. }
  22076. } else {
  22077. toggleDragButton(false, table, me);
  22078. }
  22079. } catch (e) {
  22080. showError(e);
  22081. }
  22082. }
  22083. var dragButtonTimer;
  22084. function toggleDragButton(show, table, editor) {
  22085. if (!show) {
  22086. if (dragOver) return;
  22087. dragButtonTimer = setTimeout(function() {
  22088. !dragOver &&
  22089. dragButton &&
  22090. dragButton.parentNode &&
  22091. dragButton.parentNode.removeChild(dragButton);
  22092. }, 2000);
  22093. } else {
  22094. createDragButton(table, editor);
  22095. }
  22096. }
  22097. function createDragButton(table, editor) {
  22098. var pos = domUtils.getXY(table),
  22099. doc = table.ownerDocument;
  22100. if (dragButton && dragButton.parentNode) return dragButton;
  22101. dragButton = doc.createElement("div");
  22102. dragButton.contentEditable = false;
  22103. dragButton.innerHTML = "";
  22104. dragButton.style.cssText =
  22105. "width:15px;height:15px;background-image:url(" +
  22106. editor.options.UEDITOR_HOME_URL +
  22107. "dialogs/table/dragicon.png);position: absolute;cursor:move;top:" +
  22108. (pos.y - 15) +
  22109. "px;left:" +
  22110. pos.x +
  22111. "px;";
  22112. domUtils.unSelectable(dragButton);
  22113. dragButton.onmouseover = function(evt) {
  22114. dragOver = true;
  22115. };
  22116. dragButton.onmouseout = function(evt) {
  22117. dragOver = false;
  22118. };
  22119. domUtils.on(dragButton, "click", function(type, evt) {
  22120. doClick(evt, this);
  22121. });
  22122. domUtils.on(dragButton, "dblclick", function(type, evt) {
  22123. doDblClick(evt);
  22124. });
  22125. domUtils.on(dragButton, "dragstart", function(type, evt) {
  22126. domUtils.preventDefault(evt);
  22127. });
  22128. var timer;
  22129. function doClick(evt, button) {
  22130. // 部分浏览器下需要清理
  22131. clearTimeout(timer);
  22132. timer = setTimeout(function() {
  22133. editor.fireEvent("tableClicked", table, button);
  22134. }, 300);
  22135. }
  22136. function doDblClick(evt) {
  22137. clearTimeout(timer);
  22138. var ut = getUETable(table),
  22139. start = table.rows[0].cells[0],
  22140. end = ut.getLastCell(),
  22141. range = ut.getCellsRange(start, end);
  22142. editor.selection.getRange().setStart(start, 0).setCursor(false, true);
  22143. ut.setSelected(range);
  22144. }
  22145. doc.body.appendChild(dragButton);
  22146. }
  22147. // function inPosition(table, pos) {
  22148. // var tablePos = domUtils.getXY(table),
  22149. // width = table.offsetWidth,
  22150. // height = table.offsetHeight;
  22151. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  22152. // return "topLeft";
  22153. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  22154. // return "bottomRight";
  22155. // }
  22156. // }
  22157. function inTableSide(table, cell, evt, top) {
  22158. var pos = mouseCoords(evt),
  22159. state = getRelation(cell, pos);
  22160. if (top) {
  22161. var caption = table.getElementsByTagName("caption")[0],
  22162. capHeight = caption ? caption.offsetHeight : 0;
  22163. return state == "v1" && pos.y - domUtils.getXY(table).y - capHeight < 8;
  22164. } else {
  22165. return state == "h1" && pos.x - domUtils.getXY(table).x < 8;
  22166. }
  22167. }
  22168. /**
  22169. * 获取拖动时允许的X轴坐标
  22170. * @param dragTd
  22171. * @param evt
  22172. */
  22173. function getPermissionX(dragTd, evt) {
  22174. var ut = getUETable(dragTd);
  22175. if (ut) {
  22176. var preTd = ut.getSameEndPosCells(dragTd, "x")[0],
  22177. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  22178. mouseX = mouseCoords(evt).x,
  22179. left =
  22180. (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  22181. right = nextTd
  22182. ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20
  22183. : me.body.offsetWidth + 5 ||
  22184. parseInt(domUtils.getComputedStyle(me.body, "width"), 10);
  22185. left += cellMinWidth;
  22186. right -= cellMinWidth;
  22187. return mouseX < left ? left : mouseX > right ? right : mouseX;
  22188. }
  22189. }
  22190. /**
  22191. * 获取拖动时允许的Y轴坐标
  22192. */
  22193. function getPermissionY(dragTd, evt) {
  22194. try {
  22195. var top = domUtils.getXY(dragTd).y,
  22196. mousePosY = mouseCoords(evt).y;
  22197. return mousePosY < top ? top : mousePosY;
  22198. } catch (e) {
  22199. showError(e);
  22200. }
  22201. }
  22202. /**
  22203. * 移动状态切换
  22204. */
  22205. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  22206. try {
  22207. editor.body.style.cursor = dir == "h"
  22208. ? "col-resize"
  22209. : dir == "v" ? "row-resize" : "text";
  22210. if (browser.ie) {
  22211. if (dir && !mousedown && !getUETableBySelected(editor)) {
  22212. getDragLine(editor, editor.document);
  22213. showDragLineAt(dir, cell);
  22214. } else {
  22215. hideDragLine(editor);
  22216. }
  22217. }
  22218. onBorder = draggable;
  22219. } catch (e) {
  22220. showError(e);
  22221. }
  22222. }
  22223. /**
  22224. * 获取与UETable相关的resize line
  22225. * @param uetable UETable对象
  22226. */
  22227. function getResizeLineByUETable() {
  22228. var lineId = "_UETableResizeLine",
  22229. line = this.document.getElementById(lineId);
  22230. if (!line) {
  22231. line = this.document.createElement("div");
  22232. line.id = lineId;
  22233. line.contnetEditable = false;
  22234. line.setAttribute("unselectable", "on");
  22235. var styles = {
  22236. width: 2 * cellBorderWidth + 1 + "px",
  22237. position: "absolute",
  22238. "z-index": 100000,
  22239. cursor: "col-resize",
  22240. background: "red",
  22241. display: "none"
  22242. };
  22243. //切换状态
  22244. line.onmouseout = function() {
  22245. this.style.display = "none";
  22246. };
  22247. utils.extend(line.style, styles);
  22248. this.document.body.appendChild(line);
  22249. }
  22250. return line;
  22251. }
  22252. /**
  22253. * 更新resize-line
  22254. */
  22255. function updateResizeLine(cell, uetable) {
  22256. var line = getResizeLineByUETable.call(this),
  22257. table = uetable.table,
  22258. styles = {
  22259. top: domUtils.getXY(table).y + "px",
  22260. left:
  22261. domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + "px",
  22262. display: "block",
  22263. height: table.offsetHeight + "px"
  22264. };
  22265. utils.extend(line.style, styles);
  22266. }
  22267. /**
  22268. * 显示resize-line
  22269. */
  22270. function showResizeLine(cell) {
  22271. var uetable = getUETable(cell);
  22272. updateResizeLine.call(this, cell, uetable);
  22273. }
  22274. /**
  22275. * 获取鼠标与当前单元格的相对位置
  22276. * @param ele
  22277. * @param mousePos
  22278. */
  22279. function getRelation(ele, mousePos) {
  22280. var elePos = domUtils.getXY(ele);
  22281. if (!elePos) {
  22282. return "";
  22283. }
  22284. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  22285. return "h";
  22286. }
  22287. if (mousePos.x - elePos.x < cellBorderWidth) {
  22288. return "h1";
  22289. }
  22290. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  22291. return "v";
  22292. }
  22293. if (mousePos.y - elePos.y < cellBorderWidth) {
  22294. return "v1";
  22295. }
  22296. return "";
  22297. }
  22298. function mouseDownEvent(type, evt) {
  22299. if (isEditorDisabled()) {
  22300. return;
  22301. }
  22302. userActionStatus = {
  22303. x: evt.clientX,
  22304. y: evt.clientY
  22305. };
  22306. //右键菜单单独处理
  22307. if (evt.button == 2) {
  22308. var ut = getUETableBySelected(me),
  22309. flag = false;
  22310. if (ut) {
  22311. var td = getTargetTd(me, evt);
  22312. utils.each(ut.selectedTds, function(ti) {
  22313. if (ti === td) {
  22314. flag = true;
  22315. }
  22316. });
  22317. if (!flag) {
  22318. removeSelectedClass(domUtils.getElementsByTagName(me.body, "th td"));
  22319. ut.clearSelected();
  22320. } else {
  22321. td = ut.selectedTds[0];
  22322. setTimeout(function() {
  22323. me.selection.getRange().setStart(td, 0).setCursor(false, true);
  22324. }, 0);
  22325. }
  22326. }
  22327. } else {
  22328. tableClickHander(evt);
  22329. }
  22330. }
  22331. //清除表格的计时器
  22332. function clearTableTimer() {
  22333. tabTimer && clearTimeout(tabTimer);
  22334. tabTimer = null;
  22335. }
  22336. //双击收缩
  22337. function tableDbclickHandler(evt) {
  22338. singleClickState = 0;
  22339. evt = evt || me.window.event;
  22340. var target = getParentTdOrTh(evt.target || evt.srcElement);
  22341. if (target) {
  22342. var h;
  22343. if ((h = getRelation(target, mouseCoords(evt)))) {
  22344. hideDragLine(me);
  22345. if (h == "h1") {
  22346. h = "h";
  22347. if (
  22348. inTableSide(
  22349. domUtils.findParentByTagName(target, "table"),
  22350. target,
  22351. evt
  22352. )
  22353. ) {
  22354. me.execCommand("adaptbywindow");
  22355. } else {
  22356. target = getUETable(target).getPreviewCell(target);
  22357. if (target) {
  22358. var rng = me.selection.getRange();
  22359. rng.selectNodeContents(target).setCursor(true, true);
  22360. }
  22361. }
  22362. }
  22363. if (h == "h") {
  22364. var ut = getUETable(target),
  22365. table = ut.table,
  22366. cells = getCellsByMoveBorder(target, table, true);
  22367. cells = extractArray(cells, "left");
  22368. ut.width = ut.offsetWidth;
  22369. var oldWidth = [],
  22370. newWidth = [];
  22371. utils.each(cells, function(cell) {
  22372. oldWidth.push(cell.offsetWidth);
  22373. });
  22374. utils.each(cells, function(cell) {
  22375. cell.removeAttribute("width");
  22376. });
  22377. window.setTimeout(function() {
  22378. //是否允许改变
  22379. var changeable = true;
  22380. utils.each(cells, function(cell, index) {
  22381. var width = cell.offsetWidth;
  22382. if (width > oldWidth[index]) {
  22383. changeable = false;
  22384. return false;
  22385. }
  22386. newWidth.push(width);
  22387. });
  22388. var change = changeable ? newWidth : oldWidth;
  22389. utils.each(cells, function(cell, index) {
  22390. cell.width = change[index] - getTabcellSpace();
  22391. });
  22392. }, 0);
  22393. // minWidth -= cellMinWidth;
  22394. //
  22395. // table.removeAttribute("width");
  22396. // utils.each(cells, function (cell) {
  22397. // cell.style.width = "";
  22398. // cell.width -= minWidth;
  22399. // });
  22400. }
  22401. }
  22402. }
  22403. }
  22404. function tableClickHander(evt) {
  22405. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td th"));
  22406. //trace:3113
  22407. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  22408. utils.each(me.document.getElementsByTagName("table"), function(t) {
  22409. t.ueTable = null;
  22410. });
  22411. startTd = getTargetTd(me, evt);
  22412. if (!startTd) return;
  22413. var table = domUtils.findParentByTagName(startTd, "table", true);
  22414. ut = getUETable(table);
  22415. ut && ut.clearSelected();
  22416. //判断当前鼠标状态
  22417. if (!onBorder) {
  22418. me.document.body.style.webkitUserSelect = "";
  22419. mousedown = true;
  22420. me.addListener("mouseover", mouseOverEvent);
  22421. } else {
  22422. //边框上的动作处理
  22423. borderActionHandler(evt);
  22424. }
  22425. }
  22426. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  22427. function borderActionHandler(evt) {
  22428. if (browser.ie) {
  22429. evt = reconstruct(evt);
  22430. }
  22431. clearTableDragTimer();
  22432. //是否正在等待resize的缓冲中
  22433. isInResizeBuffer = true;
  22434. tableDragTimer = setTimeout(function() {
  22435. tableBorderDrag(evt);
  22436. }, dblclickTime);
  22437. }
  22438. function extractArray(originArr, key) {
  22439. var result = [],
  22440. tmp = null;
  22441. for (var i = 0, len = originArr.length; i < len; i++) {
  22442. tmp = originArr[i][key];
  22443. if (tmp) {
  22444. result.push(tmp);
  22445. }
  22446. }
  22447. return result;
  22448. }
  22449. function clearTableDragTimer() {
  22450. tableDragTimer && clearTimeout(tableDragTimer);
  22451. tableDragTimer = null;
  22452. }
  22453. function reconstruct(obj) {
  22454. var attrs = [
  22455. "pageX",
  22456. "pageY",
  22457. "clientX",
  22458. "clientY",
  22459. "srcElement",
  22460. "target"
  22461. ],
  22462. newObj = {};
  22463. if (obj) {
  22464. for (var i = 0, key, val; (key = attrs[i]); i++) {
  22465. val = obj[key];
  22466. val && (newObj[key] = val);
  22467. }
  22468. }
  22469. return newObj;
  22470. }
  22471. //边框拖动
  22472. function tableBorderDrag(evt) {
  22473. isInResizeBuffer = false;
  22474. startTd = evt.target || evt.srcElement;
  22475. if (!startTd) return;
  22476. var state = getRelation(startTd, mouseCoords(evt));
  22477. if (/\d/.test(state)) {
  22478. state = state.replace(/\d/, "");
  22479. startTd = getUETable(startTd).getPreviewCell(startTd, state == "v");
  22480. }
  22481. hideDragLine(me);
  22482. getDragLine(me, me.document);
  22483. me.fireEvent("saveScene");
  22484. showDragLineAt(state, startTd);
  22485. mousedown = true;
  22486. //拖动开始
  22487. onDrag = state;
  22488. dragTd = startTd;
  22489. }
  22490. function mouseUpEvent(type, evt) {
  22491. if (isEditorDisabled()) {
  22492. return;
  22493. }
  22494. clearTableDragTimer();
  22495. isInResizeBuffer = false;
  22496. if (onBorder) {
  22497. singleClickState = ++singleClickState % 3;
  22498. userActionStatus = {
  22499. x: evt.clientX,
  22500. y: evt.clientY
  22501. };
  22502. tableResizeTimer = setTimeout(function() {
  22503. singleClickState > 0 && singleClickState--;
  22504. }, dblclickTime);
  22505. if (singleClickState === 2) {
  22506. singleClickState = 0;
  22507. tableDbclickHandler(evt);
  22508. return;
  22509. }
  22510. }
  22511. if (evt.button == 2) return;
  22512. var me = this;
  22513. //清除表格上原生跨选问题
  22514. var range = me.selection.getRange(),
  22515. start = domUtils.findParentByTagName(range.startContainer, "table", true),
  22516. end = domUtils.findParentByTagName(range.endContainer, "table", true);
  22517. if (start || end) {
  22518. if (start === end) {
  22519. start = domUtils.findParentByTagName(
  22520. range.startContainer,
  22521. ["td", "th", "caption"],
  22522. true
  22523. );
  22524. end = domUtils.findParentByTagName(
  22525. range.endContainer,
  22526. ["td", "th", "caption"],
  22527. true
  22528. );
  22529. if (start !== end) {
  22530. me.selection.clearRange();
  22531. }
  22532. } else {
  22533. me.selection.clearRange();
  22534. }
  22535. }
  22536. mousedown = false;
  22537. me.document.body.style.webkitUserSelect = "";
  22538. //拖拽状态下的mouseUP
  22539. if (onDrag && dragTd) {
  22540. me.selection.getNative()[
  22541. browser.ie9below ? "empty" : "removeAllRanges"
  22542. ]();
  22543. singleClickState = 0;
  22544. dragLine = me.document.getElementById("ue_tableDragLine");
  22545. // trace 3973
  22546. if (dragLine) {
  22547. var dragTdPos = domUtils.getXY(dragTd),
  22548. dragLinePos = domUtils.getXY(dragLine);
  22549. switch (onDrag) {
  22550. case "h":
  22551. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x);
  22552. break;
  22553. case "v":
  22554. changeRowHeight(
  22555. dragTd,
  22556. dragLinePos.y - dragTdPos.y - dragTd.offsetHeight
  22557. );
  22558. break;
  22559. default:
  22560. }
  22561. onDrag = "";
  22562. dragTd = null;
  22563. hideDragLine(me);
  22564. me.fireEvent("saveScene");
  22565. return;
  22566. }
  22567. }
  22568. //正常状态下的mouseup
  22569. if (!startTd) {
  22570. var target = domUtils.findParentByTagName(
  22571. evt.target || evt.srcElement,
  22572. "td",
  22573. true
  22574. );
  22575. if (!target)
  22576. target = domUtils.findParentByTagName(
  22577. evt.target || evt.srcElement,
  22578. "th",
  22579. true
  22580. );
  22581. if (target && (target.tagName == "TD" || target.tagName == "TH")) {
  22582. if (me.fireEvent("excludetable", target) === true) return;
  22583. range = new dom.Range(me.document);
  22584. range.setStart(target, 0).setCursor(false, true);
  22585. }
  22586. } else {
  22587. var ut = getUETable(startTd),
  22588. cell = ut ? ut.selectedTds[0] : null;
  22589. if (cell) {
  22590. range = new dom.Range(me.document);
  22591. if (domUtils.isEmptyBlock(cell)) {
  22592. range.setStart(cell, 0).setCursor(false, true);
  22593. } else {
  22594. range
  22595. .selectNodeContents(cell)
  22596. .shrinkBoundary()
  22597. .setCursor(false, true);
  22598. }
  22599. } else {
  22600. range = me.selection.getRange().shrinkBoundary();
  22601. if (!range.collapsed) {
  22602. var start = domUtils.findParentByTagName(
  22603. range.startContainer,
  22604. ["td", "th"],
  22605. true
  22606. ),
  22607. end = domUtils.findParentByTagName(
  22608. range.endContainer,
  22609. ["td", "th"],
  22610. true
  22611. );
  22612. //在table里边的不能清除
  22613. if (
  22614. (start && !end) ||
  22615. (!start && end) ||
  22616. (start && end && start !== end)
  22617. ) {
  22618. range.setCursor(false, true);
  22619. }
  22620. }
  22621. }
  22622. startTd = null;
  22623. me.removeListener("mouseover", mouseOverEvent);
  22624. }
  22625. me._selectionChange(250, evt);
  22626. }
  22627. function mouseOverEvent(type, evt) {
  22628. if (isEditorDisabled()) {
  22629. return;
  22630. }
  22631. var me = this,
  22632. tar = evt.target || evt.srcElement;
  22633. currentTd =
  22634. domUtils.findParentByTagName(tar, "td", true) ||
  22635. domUtils.findParentByTagName(tar, "th", true);
  22636. //需要判断两个TD是否位于同一个表格内
  22637. if (
  22638. startTd &&
  22639. currentTd &&
  22640. ((startTd.tagName == "TD" && currentTd.tagName == "TD") ||
  22641. (startTd.tagName == "TH" && currentTd.tagName == "TH")) &&
  22642. domUtils.findParentByTagName(startTd, "table") ==
  22643. domUtils.findParentByTagName(currentTd, "table")
  22644. ) {
  22645. var ut = getUETable(currentTd);
  22646. if (startTd != currentTd) {
  22647. me.document.body.style.webkitUserSelect = "none";
  22648. me.selection.getNative()[
  22649. browser.ie9below ? "empty" : "removeAllRanges"
  22650. ]();
  22651. var range = ut.getCellsRange(startTd, currentTd);
  22652. ut.setSelected(range);
  22653. } else {
  22654. me.document.body.style.webkitUserSelect = "";
  22655. ut.clearSelected();
  22656. }
  22657. }
  22658. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  22659. }
  22660. function setCellHeight(cell, height, backHeight) {
  22661. var lineHight = parseInt(
  22662. domUtils.getComputedStyle(cell, "line-height"),
  22663. 10
  22664. ),
  22665. tmpHeight = backHeight + height;
  22666. height = tmpHeight < lineHight ? lineHight : tmpHeight;
  22667. if (cell.style.height) cell.style.height = "";
  22668. cell.rowSpan == 1
  22669. ? cell.setAttribute("height", height)
  22670. : cell.removeAttribute && cell.removeAttribute("height");
  22671. }
  22672. function getWidth(cell) {
  22673. if (!cell) return 0;
  22674. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  22675. }
  22676. function changeColWidth(cell, changeValue) {
  22677. var ut = getUETable(cell);
  22678. if (ut) {
  22679. //根据当前移动的边框获取相关的单元格
  22680. var table = ut.table,
  22681. cells = getCellsByMoveBorder(cell, table);
  22682. table.style.width = "";
  22683. table.removeAttribute("width");
  22684. //修正改变量
  22685. changeValue = correctChangeValue(changeValue, cell, cells);
  22686. if (cell.nextSibling) {
  22687. var i = 0;
  22688. utils.each(cells, function(cellGroup) {
  22689. cellGroup.left.width = +cellGroup.left.width + changeValue;
  22690. cellGroup.right &&
  22691. (cellGroup.right.width = +cellGroup.right.width - changeValue);
  22692. });
  22693. } else {
  22694. utils.each(cells, function(cellGroup) {
  22695. cellGroup.left.width -= -changeValue;
  22696. });
  22697. }
  22698. }
  22699. }
  22700. function isEditorDisabled() {
  22701. return me.body.contentEditable === "false";
  22702. }
  22703. function changeRowHeight(td, changeValue) {
  22704. if (Math.abs(changeValue) < 10) return;
  22705. var ut = getUETable(td);
  22706. if (ut) {
  22707. var cells = ut.getSameEndPosCells(td, "y"),
  22708. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  22709. backHeight = cells[0] ? cells[0].offsetHeight : 0;
  22710. for (var i = 0, cell; (cell = cells[i++]); ) {
  22711. setCellHeight(cell, changeValue, backHeight);
  22712. }
  22713. }
  22714. }
  22715. /**
  22716. * 获取调整单元格大小的相关单元格
  22717. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  22718. */
  22719. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  22720. if (!table) {
  22721. table = domUtils.findParentByTagName(cell, "table");
  22722. }
  22723. if (!table) {
  22724. return null;
  22725. }
  22726. //获取到该单元格所在行的序列号
  22727. var index = domUtils.getNodeIndex(cell),
  22728. temp = cell,
  22729. rows = table.rows,
  22730. colIndex = 0;
  22731. while (temp) {
  22732. //获取到当前单元格在未发生单元格合并时的序列
  22733. if (temp.nodeType === 1) {
  22734. colIndex += temp.colSpan || 1;
  22735. }
  22736. temp = temp.previousSibling;
  22737. }
  22738. temp = null;
  22739. //记录想关的单元格
  22740. var borderCells = [];
  22741. utils.each(rows, function(tabRow) {
  22742. var cells = tabRow.cells,
  22743. currIndex = 0;
  22744. utils.each(cells, function(tabCell) {
  22745. currIndex += tabCell.colSpan || 1;
  22746. if (currIndex === colIndex) {
  22747. borderCells.push({
  22748. left: tabCell,
  22749. right: tabCell.nextSibling || null
  22750. });
  22751. return false;
  22752. } else if (currIndex > colIndex) {
  22753. if (isContainMergeCell) {
  22754. borderCells.push({
  22755. left: tabCell
  22756. });
  22757. }
  22758. return false;
  22759. }
  22760. });
  22761. });
  22762. return borderCells;
  22763. }
  22764. /**
  22765. * 通过给定的单元格集合获取最小的单元格width
  22766. */
  22767. function getMinWidthByTableCells(cells) {
  22768. var minWidth = Number.MAX_VALUE;
  22769. for (var i = 0, curCell; (curCell = cells[i]); i++) {
  22770. minWidth = Math.min(
  22771. minWidth,
  22772. curCell.width || getTableCellWidth(curCell)
  22773. );
  22774. }
  22775. return minWidth;
  22776. }
  22777. function correctChangeValue(changeValue, relatedCell, cells) {
  22778. //为单元格的paading预留空间
  22779. changeValue -= getTabcellSpace();
  22780. if (changeValue < 0) {
  22781. return 0;
  22782. }
  22783. changeValue -= getTableCellWidth(relatedCell);
  22784. //确定方向
  22785. var direction = changeValue < 0 ? "left" : "right";
  22786. changeValue = Math.abs(changeValue);
  22787. //只关心非最后一个单元格就可以
  22788. utils.each(cells, function(cellGroup) {
  22789. var curCell = cellGroup[direction];
  22790. //为单元格保留最小空间
  22791. if (curCell) {
  22792. changeValue = Math.min(
  22793. changeValue,
  22794. getTableCellWidth(curCell) - cellMinWidth
  22795. );
  22796. }
  22797. });
  22798. //修正越界
  22799. changeValue = changeValue < 0 ? 0 : changeValue;
  22800. return direction === "left" ? -changeValue : changeValue;
  22801. }
  22802. function getTableCellWidth(cell) {
  22803. var width = 0,
  22804. //偏移纠正量
  22805. offset = 0,
  22806. width = cell.offsetWidth - getTabcellSpace();
  22807. //最后一个节点纠正一下
  22808. if (!cell.nextSibling) {
  22809. width -= getTableCellOffset(cell);
  22810. }
  22811. width = width < 0 ? 0 : width;
  22812. try {
  22813. cell.width = width;
  22814. } catch (e) {}
  22815. return width;
  22816. }
  22817. /**
  22818. * 获取单元格所在表格的最末单元格的偏移量
  22819. */
  22820. function getTableCellOffset(cell) {
  22821. tab = domUtils.findParentByTagName(cell, "table", false);
  22822. if (tab.offsetVal === undefined) {
  22823. var prev = cell.previousSibling;
  22824. if (prev) {
  22825. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  22826. tab.offsetVal = cell.offsetWidth - prev.offsetWidth === UT.borderWidth
  22827. ? UT.borderWidth
  22828. : 0;
  22829. } else {
  22830. tab.offsetVal = 0;
  22831. }
  22832. }
  22833. return tab.offsetVal;
  22834. }
  22835. function getTabcellSpace() {
  22836. if (UT.tabcellSpace === undefined) {
  22837. var cell = null,
  22838. tab = me.document.createElement("table"),
  22839. tbody = me.document.createElement("tbody"),
  22840. trow = me.document.createElement("tr"),
  22841. tabcell = me.document.createElement("td"),
  22842. mirror = null;
  22843. tabcell.style.cssText = "border: 0;";
  22844. tabcell.width = 1;
  22845. trow.appendChild(tabcell);
  22846. trow.appendChild((mirror = tabcell.cloneNode(false)));
  22847. tbody.appendChild(trow);
  22848. tab.appendChild(tbody);
  22849. tab.style.cssText = "visibility: hidden;";
  22850. me.body.appendChild(tab);
  22851. UT.paddingSpace = tabcell.offsetWidth - 1;
  22852. var tmpTabWidth = tab.offsetWidth;
  22853. tabcell.style.cssText = "";
  22854. mirror.style.cssText = "";
  22855. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3;
  22856. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth;
  22857. me.body.removeChild(tab);
  22858. }
  22859. getTabcellSpace = function() {
  22860. return UT.tabcellSpace;
  22861. };
  22862. return UT.tabcellSpace;
  22863. }
  22864. function getDragLine(editor, doc) {
  22865. if (mousedown) return;
  22866. dragLine = editor.document.createElement("div");
  22867. domUtils.setAttributes(dragLine, {
  22868. id: "ue_tableDragLine",
  22869. unselectable: "on",
  22870. contenteditable: false,
  22871. onresizestart: "return false",
  22872. ondragstart: "return false",
  22873. onselectstart: "return false",
  22874. style:
  22875. "background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)"
  22876. });
  22877. editor.body.appendChild(dragLine);
  22878. }
  22879. function hideDragLine(editor) {
  22880. if (mousedown) return;
  22881. var line;
  22882. while ((line = editor.document.getElementById("ue_tableDragLine"))) {
  22883. domUtils.remove(line);
  22884. }
  22885. }
  22886. /**
  22887. * 依据state(v|h)在cell位置显示横线
  22888. * @param state
  22889. * @param cell
  22890. */
  22891. function showDragLineAt(state, cell) {
  22892. if (!cell) return;
  22893. var table = domUtils.findParentByTagName(cell, "table"),
  22894. caption = table.getElementsByTagName("caption"),
  22895. width = table.offsetWidth,
  22896. height =
  22897. table.offsetHeight - (caption.length > 0 ? caption[0].offsetHeight : 0),
  22898. tablePos = domUtils.getXY(table),
  22899. cellPos = domUtils.getXY(cell),
  22900. css;
  22901. switch (state) {
  22902. case "h":
  22903. css =
  22904. "height:" +
  22905. height +
  22906. "px;top:" +
  22907. (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) +
  22908. "px;left:" +
  22909. (cellPos.x + cell.offsetWidth);
  22910. dragLine.style.cssText =
  22911. css +
  22912. "px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)";
  22913. break;
  22914. case "v":
  22915. css =
  22916. "width:" +
  22917. width +
  22918. "px;left:" +
  22919. tablePos.x +
  22920. "px;top:" +
  22921. (cellPos.y + cell.offsetHeight);
  22922. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  22923. dragLine.style.cssText =
  22924. css +
  22925. "px;overflow:hidden;position: absolute;display:block;background-color:blue;height:1px;border:0;color:blue;opacity:.2;filter:alpha(opacity=20)";
  22926. break;
  22927. default:
  22928. }
  22929. }
  22930. /**
  22931. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  22932. * @param editor
  22933. * @param flag
  22934. */
  22935. function switchBorderColor(editor, flag) {
  22936. var tableArr = domUtils.getElementsByTagName(editor.body, "table"),
  22937. color;
  22938. for (var i = 0, node; (node = tableArr[i++]); ) {
  22939. var td = domUtils.getElementsByTagName(node, "td");
  22940. if (td[0]) {
  22941. if (flag) {
  22942. color = td[0].style.borderColor.replace(/\s/g, "");
  22943. if (/(#ffffff)|(rgb\(255,255,255\))/gi.test(color))
  22944. domUtils.addClass(node, "noBorderTable");
  22945. } else {
  22946. domUtils.removeClasses(node, "noBorderTable");
  22947. }
  22948. }
  22949. }
  22950. }
  22951. function getTableWidth(editor, needIEHack, defaultValue) {
  22952. var body = editor.body;
  22953. return (
  22954. body.offsetWidth -
  22955. (needIEHack
  22956. ? parseInt(domUtils.getComputedStyle(body, "margin-left"), 10) * 2
  22957. : 0) -
  22958. defaultValue.tableBorder * 2 -
  22959. (editor.options.offsetWidth || 0)
  22960. );
  22961. }
  22962. /**
  22963. * 获取当前拖动的单元格
  22964. */
  22965. function getTargetTd(editor, evt) {
  22966. var target = domUtils.findParentByTagName(
  22967. evt.target || evt.srcElement,
  22968. ["td", "th"],
  22969. true
  22970. ),
  22971. dir = null;
  22972. if (!target) {
  22973. return null;
  22974. }
  22975. dir = getRelation(target, mouseCoords(evt));
  22976. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  22977. if (!target) {
  22978. return null;
  22979. }
  22980. if (dir === "h1" && target.previousSibling) {
  22981. var position = domUtils.getXY(target),
  22982. cellWidth = target.offsetWidth;
  22983. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  22984. target = target.previousSibling;
  22985. }
  22986. } else if (dir === "v1" && target.parentNode.previousSibling) {
  22987. var position = domUtils.getXY(target),
  22988. cellHeight = target.offsetHeight;
  22989. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  22990. target = target.parentNode.previousSibling.firstChild;
  22991. }
  22992. }
  22993. //排除了非td内部以及用于代码高亮部分的td
  22994. return target && !(editor.fireEvent("excludetable", target) === true)
  22995. ? target
  22996. : null;
  22997. }
  22998. };
  22999. // plugins/table.sort.js
  23000. /**
  23001. * Created with JetBrains PhpStorm.
  23002. * User: Jinqn
  23003. * Date: 13-10-12
  23004. * Time: 上午10:20
  23005. * To change this template use File | Settings | File Templates.
  23006. */
  23007. UE.UETable.prototype.sortTable = function(sortByCellIndex, compareFn) {
  23008. var table = this.table,
  23009. rows = table.rows,
  23010. trArray = [],
  23011. flag = rows[0].cells[0].tagName === "TH",
  23012. lastRowIndex = 0;
  23013. if (this.selectedTds.length) {
  23014. var range = this.cellsRange,
  23015. len = range.endRowIndex + 1;
  23016. for (var i = range.beginRowIndex; i < len; i++) {
  23017. trArray[i] = rows[i];
  23018. }
  23019. trArray.splice(0, range.beginRowIndex);
  23020. lastRowIndex = range.endRowIndex + 1 === this.rowsNum
  23021. ? 0
  23022. : range.endRowIndex + 1;
  23023. } else {
  23024. for (var i = 0, len = rows.length; i < len; i++) {
  23025. trArray[i] = rows[i];
  23026. }
  23027. }
  23028. var Fn = {
  23029. reversecurrent: function(td1, td2) {
  23030. return 1;
  23031. },
  23032. orderbyasc: function(td1, td2) {
  23033. var value1 = td1.innerText || td1.textContent,
  23034. value2 = td2.innerText || td2.textContent;
  23035. return value1.localeCompare(value2);
  23036. },
  23037. reversebyasc: function(td1, td2) {
  23038. var value1 = td1.innerHTML,
  23039. value2 = td2.innerHTML;
  23040. return value2.localeCompare(value1);
  23041. },
  23042. orderbynum: function(td1, td2) {
  23043. var value1 = td1[browser.ie ? "innerText" : "textContent"].match(/\d+/),
  23044. value2 = td2[browser.ie ? "innerText" : "textContent"].match(/\d+/);
  23045. if (value1) value1 = +value1[0];
  23046. if (value2) value2 = +value2[0];
  23047. return (value1 || 0) - (value2 || 0);
  23048. },
  23049. reversebynum: function(td1, td2) {
  23050. var value1 = td1[browser.ie ? "innerText" : "textContent"].match(/\d+/),
  23051. value2 = td2[browser.ie ? "innerText" : "textContent"].match(/\d+/);
  23052. if (value1) value1 = +value1[0];
  23053. if (value2) value2 = +value2[0];
  23054. return (value2 || 0) - (value1 || 0);
  23055. }
  23056. };
  23057. //对表格设置排序的标记data-sort-type
  23058. table.setAttribute(
  23059. "data-sort-type",
  23060. compareFn && typeof compareFn === "string" && Fn[compareFn] ? compareFn : ""
  23061. );
  23062. //th不参与排序
  23063. flag && trArray.splice(0, 1);
  23064. trArray = utils.sort(trArray, function(tr1, tr2) {
  23065. var result;
  23066. if (compareFn && typeof compareFn === "function") {
  23067. result = compareFn.call(
  23068. this,
  23069. tr1.cells[sortByCellIndex],
  23070. tr2.cells[sortByCellIndex]
  23071. );
  23072. } else if (compareFn && typeof compareFn === "number") {
  23073. result = 1;
  23074. } else if (compareFn && typeof compareFn === "string" && Fn[compareFn]) {
  23075. result = Fn[compareFn].call(
  23076. this,
  23077. tr1.cells[sortByCellIndex],
  23078. tr2.cells[sortByCellIndex]
  23079. );
  23080. } else {
  23081. result = Fn["orderbyasc"].call(
  23082. this,
  23083. tr1.cells[sortByCellIndex],
  23084. tr2.cells[sortByCellIndex]
  23085. );
  23086. }
  23087. return result;
  23088. });
  23089. var fragment = table.ownerDocument.createDocumentFragment();
  23090. for (var j = 0, len = trArray.length; j < len; j++) {
  23091. fragment.appendChild(trArray[j]);
  23092. }
  23093. var tbody = table.getElementsByTagName("tbody")[0];
  23094. if (!lastRowIndex) {
  23095. tbody.appendChild(fragment);
  23096. } else {
  23097. tbody.insertBefore(
  23098. fragment,
  23099. rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1]
  23100. );
  23101. }
  23102. };
  23103. UE.plugins["tablesort"] = function() {
  23104. var me = this,
  23105. UT = UE.UETable,
  23106. getUETable = function(tdOrTable) {
  23107. return UT.getUETable(tdOrTable);
  23108. },
  23109. getTableItemsByRange = function(editor) {
  23110. return UT.getTableItemsByRange(editor);
  23111. };
  23112. me.ready(function() {
  23113. //添加表格可排序的样式
  23114. utils.cssRule(
  23115. "tablesort",
  23116. "table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;" +
  23117. " background-image:url(" +
  23118. me.options.themePath +
  23119. me.options.theme +
  23120. "/images/sortable.png);}",
  23121. me.document
  23122. );
  23123. //做单元格合并操作时,清除可排序标识
  23124. me.addListener("afterexeccommand", function(type, cmd) {
  23125. if (cmd == "mergeright" || cmd == "mergedown" || cmd == "mergecells") {
  23126. this.execCommand("disablesort");
  23127. }
  23128. });
  23129. });
  23130. //表格排序
  23131. UE.commands["sorttable"] = {
  23132. queryCommandState: function() {
  23133. var me = this,
  23134. tableItems = getTableItemsByRange(me);
  23135. if (!tableItems.cell) return -1;
  23136. var table = tableItems.table,
  23137. cells = table.getElementsByTagName("td");
  23138. for (var i = 0, cell; (cell = cells[i++]); ) {
  23139. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1;
  23140. }
  23141. return 0;
  23142. },
  23143. execCommand: function(cmd, fn) {
  23144. var me = this,
  23145. range = me.selection.getRange(),
  23146. bk = range.createBookmark(true),
  23147. tableItems = getTableItemsByRange(me),
  23148. cell = tableItems.cell,
  23149. ut = getUETable(tableItems.table),
  23150. cellInfo = ut.getCellInfo(cell);
  23151. ut.sortTable(cellInfo.cellIndex, fn);
  23152. range.moveToBookmark(bk);
  23153. try {
  23154. range.select();
  23155. } catch (e) {}
  23156. }
  23157. };
  23158. //设置表格可排序,清除表格可排序
  23159. UE.commands["enablesort"] = UE.commands["disablesort"] = {
  23160. queryCommandState: function(cmd) {
  23161. var table = getTableItemsByRange(this).table;
  23162. if (table && cmd == "enablesort") {
  23163. var cells = domUtils.getElementsByTagName(table, "th td");
  23164. for (var i = 0; i < cells.length; i++) {
  23165. if (
  23166. cells[i].getAttribute("colspan") > 1 ||
  23167. cells[i].getAttribute("rowspan") > 1
  23168. )
  23169. return -1;
  23170. }
  23171. }
  23172. return !table
  23173. ? -1
  23174. : (cmd == "enablesort") ^
  23175. (table.getAttribute("data-sort") != "sortEnabled")
  23176. ? -1
  23177. : 0;
  23178. },
  23179. execCommand: function(cmd) {
  23180. var table = getTableItemsByRange(this).table;
  23181. table.setAttribute(
  23182. "data-sort",
  23183. cmd == "enablesort" ? "sortEnabled" : "sortDisabled"
  23184. );
  23185. cmd == "enablesort"
  23186. ? domUtils.addClass(table, "sortEnabled")
  23187. : domUtils.removeClasses(table, "sortEnabled");
  23188. }
  23189. };
  23190. };
  23191. // plugins/contextmenu.js
  23192. ///import core
  23193. ///commands 右键菜单
  23194. ///commandsName ContextMenu
  23195. ///commandsTitle 右键菜单
  23196. /**
  23197. * 右键菜单
  23198. * @function
  23199. * @name baidu.editor.plugins.contextmenu
  23200. * @author zhanyi
  23201. */
  23202. UE.plugins["contextmenu"] = function() {
  23203. var me = this;
  23204. me.setOpt("enableContextMenu", me.getOpt("enableContextMenu") || true);
  23205. if (me.getOpt("enableContextMenu") === false) {
  23206. return;
  23207. }
  23208. var lang = me.getLang("contextMenu"),
  23209. menu,
  23210. items = me.options.contextMenu || [
  23211. { label: lang["selectall"], cmdName: "selectall" },
  23212. {
  23213. label: lang.cleardoc,
  23214. cmdName: "cleardoc",
  23215. exec: function() {
  23216. if (confirm(lang.confirmclear)) {
  23217. this.execCommand("cleardoc");
  23218. }
  23219. }
  23220. },
  23221. "-",
  23222. {
  23223. label: lang.unlink,
  23224. cmdName: "unlink"
  23225. },
  23226. "-",
  23227. {
  23228. group: lang.paragraph,
  23229. icon: "justifyjustify",
  23230. subMenu: [
  23231. {
  23232. label: lang.justifyleft,
  23233. cmdName: "justify",
  23234. value: "left"
  23235. },
  23236. {
  23237. label: lang.justifyright,
  23238. cmdName: "justify",
  23239. value: "right"
  23240. },
  23241. {
  23242. label: lang.justifycenter,
  23243. cmdName: "justify",
  23244. value: "center"
  23245. },
  23246. {
  23247. label: lang.justifyjustify,
  23248. cmdName: "justify",
  23249. value: "justify"
  23250. }
  23251. ]
  23252. },
  23253. "-",
  23254. {
  23255. group: lang.table,
  23256. icon: "table",
  23257. subMenu: [
  23258. {
  23259. label: lang.inserttable,
  23260. cmdName: "inserttable"
  23261. },
  23262. {
  23263. label: lang.deletetable,
  23264. cmdName: "deletetable"
  23265. },
  23266. "-",
  23267. {
  23268. label: lang.deleterow,
  23269. cmdName: "deleterow"
  23270. },
  23271. {
  23272. label: lang.deletecol,
  23273. cmdName: "deletecol"
  23274. },
  23275. {
  23276. label: lang.insertcol,
  23277. cmdName: "insertcol"
  23278. },
  23279. {
  23280. label: lang.insertcolnext,
  23281. cmdName: "insertcolnext"
  23282. },
  23283. {
  23284. label: lang.insertrow,
  23285. cmdName: "insertrow"
  23286. },
  23287. {
  23288. label: lang.insertrownext,
  23289. cmdName: "insertrownext"
  23290. },
  23291. "-",
  23292. {
  23293. label: lang.insertcaption,
  23294. cmdName: "insertcaption"
  23295. },
  23296. {
  23297. label: lang.deletecaption,
  23298. cmdName: "deletecaption"
  23299. },
  23300. {
  23301. label: lang.inserttitle,
  23302. cmdName: "inserttitle"
  23303. },
  23304. {
  23305. label: lang.deletetitle,
  23306. cmdName: "deletetitle"
  23307. },
  23308. {
  23309. label: lang.inserttitlecol,
  23310. cmdName: "inserttitlecol"
  23311. },
  23312. {
  23313. label: lang.deletetitlecol,
  23314. cmdName: "deletetitlecol"
  23315. },
  23316. "-",
  23317. {
  23318. label: lang.mergecells,
  23319. cmdName: "mergecells"
  23320. },
  23321. {
  23322. label: lang.mergeright,
  23323. cmdName: "mergeright"
  23324. },
  23325. {
  23326. label: lang.mergedown,
  23327. cmdName: "mergedown"
  23328. },
  23329. "-",
  23330. {
  23331. label: lang.splittorows,
  23332. cmdName: "splittorows"
  23333. },
  23334. {
  23335. label: lang.splittocols,
  23336. cmdName: "splittocols"
  23337. },
  23338. {
  23339. label: lang.splittocells,
  23340. cmdName: "splittocells"
  23341. },
  23342. "-",
  23343. {
  23344. label: lang.averageDiseRow,
  23345. cmdName: "averagedistributerow"
  23346. },
  23347. {
  23348. label: lang.averageDisCol,
  23349. cmdName: "averagedistributecol"
  23350. },
  23351. "-",
  23352. {
  23353. label: lang.edittd,
  23354. cmdName: "edittd",
  23355. exec: function() {
  23356. if (UE.ui["edittd"]) {
  23357. new UE.ui["edittd"](this);
  23358. }
  23359. this.getDialog("edittd").open();
  23360. }
  23361. },
  23362. {
  23363. label: lang.edittable,
  23364. cmdName: "edittable",
  23365. exec: function() {
  23366. if (UE.ui["edittable"]) {
  23367. new UE.ui["edittable"](this);
  23368. }
  23369. this.getDialog("edittable").open();
  23370. }
  23371. },
  23372. {
  23373. label: lang.setbordervisible,
  23374. cmdName: "setbordervisible"
  23375. }
  23376. ]
  23377. },
  23378. {
  23379. group: lang.tablesort,
  23380. icon: "tablesort",
  23381. subMenu: [
  23382. {
  23383. label: lang.enablesort,
  23384. cmdName: "enablesort"
  23385. },
  23386. {
  23387. label: lang.disablesort,
  23388. cmdName: "disablesort"
  23389. },
  23390. "-",
  23391. {
  23392. label: lang.reversecurrent,
  23393. cmdName: "sorttable",
  23394. value: "reversecurrent"
  23395. },
  23396. {
  23397. label: lang.orderbyasc,
  23398. cmdName: "sorttable",
  23399. value: "orderbyasc"
  23400. },
  23401. {
  23402. label: lang.reversebyasc,
  23403. cmdName: "sorttable",
  23404. value: "reversebyasc"
  23405. },
  23406. {
  23407. label: lang.orderbynum,
  23408. cmdName: "sorttable",
  23409. value: "orderbynum"
  23410. },
  23411. {
  23412. label: lang.reversebynum,
  23413. cmdName: "sorttable",
  23414. value: "reversebynum"
  23415. }
  23416. ]
  23417. },
  23418. {
  23419. group: lang.borderbk,
  23420. icon: "borderBack",
  23421. subMenu: [
  23422. {
  23423. label: lang.setcolor,
  23424. cmdName: "interlacetable",
  23425. exec: function() {
  23426. this.execCommand("interlacetable");
  23427. }
  23428. },
  23429. {
  23430. label: lang.unsetcolor,
  23431. cmdName: "uninterlacetable",
  23432. exec: function() {
  23433. this.execCommand("uninterlacetable");
  23434. }
  23435. },
  23436. {
  23437. label: lang.setbackground,
  23438. cmdName: "settablebackground",
  23439. exec: function() {
  23440. this.execCommand("settablebackground", {
  23441. repeat: true,
  23442. colorList: ["#bbb", "#ccc"]
  23443. });
  23444. }
  23445. },
  23446. {
  23447. label: lang.unsetbackground,
  23448. cmdName: "cleartablebackground",
  23449. exec: function() {
  23450. this.execCommand("cleartablebackground");
  23451. }
  23452. },
  23453. {
  23454. label: lang.redandblue,
  23455. cmdName: "settablebackground",
  23456. exec: function() {
  23457. this.execCommand("settablebackground", {
  23458. repeat: true,
  23459. colorList: ["red", "blue"]
  23460. });
  23461. }
  23462. },
  23463. {
  23464. label: lang.threecolorgradient,
  23465. cmdName: "settablebackground",
  23466. exec: function() {
  23467. this.execCommand("settablebackground", {
  23468. repeat: true,
  23469. colorList: ["#aaa", "#bbb", "#ccc"]
  23470. });
  23471. }
  23472. }
  23473. ]
  23474. },
  23475. {
  23476. group: lang.aligntd,
  23477. icon: "aligntd",
  23478. subMenu: [
  23479. {
  23480. cmdName: "cellalignment",
  23481. value: { align: "left", vAlign: "top" }
  23482. },
  23483. {
  23484. cmdName: "cellalignment",
  23485. value: { align: "center", vAlign: "top" }
  23486. },
  23487. {
  23488. cmdName: "cellalignment",
  23489. value: { align: "right", vAlign: "top" }
  23490. },
  23491. {
  23492. cmdName: "cellalignment",
  23493. value: { align: "left", vAlign: "middle" }
  23494. },
  23495. {
  23496. cmdName: "cellalignment",
  23497. value: { align: "center", vAlign: "middle" }
  23498. },
  23499. {
  23500. cmdName: "cellalignment",
  23501. value: { align: "right", vAlign: "middle" }
  23502. },
  23503. {
  23504. cmdName: "cellalignment",
  23505. value: { align: "left", vAlign: "bottom" }
  23506. },
  23507. {
  23508. cmdName: "cellalignment",
  23509. value: { align: "center", vAlign: "bottom" }
  23510. },
  23511. {
  23512. cmdName: "cellalignment",
  23513. value: { align: "right", vAlign: "bottom" }
  23514. }
  23515. ]
  23516. },
  23517. {
  23518. group: lang.aligntable,
  23519. icon: "aligntable",
  23520. subMenu: [
  23521. {
  23522. cmdName: "tablealignment",
  23523. className: "left",
  23524. label: lang.tableleft,
  23525. value: "left"
  23526. },
  23527. {
  23528. cmdName: "tablealignment",
  23529. className: "center",
  23530. label: lang.tablecenter,
  23531. value: "center"
  23532. },
  23533. {
  23534. cmdName: "tablealignment",
  23535. className: "right",
  23536. label: lang.tableright,
  23537. value: "right"
  23538. }
  23539. ]
  23540. },
  23541. "-",
  23542. {
  23543. label: lang.insertparagraphbefore,
  23544. cmdName: "insertparagraph",
  23545. value: true
  23546. },
  23547. {
  23548. label: lang.insertparagraphafter,
  23549. cmdName: "insertparagraph"
  23550. },
  23551. {
  23552. label: lang["copy"],
  23553. cmdName: "copy"
  23554. },
  23555. {
  23556. label: lang["paste"],
  23557. cmdName: "paste"
  23558. }
  23559. ];
  23560. if (!items.length) {
  23561. return;
  23562. }
  23563. var uiUtils = UE.ui.uiUtils;
  23564. me.addListener("contextmenu", function(type, evt) {
  23565. var offset = uiUtils.getViewportOffsetByEvent(evt);
  23566. me.fireEvent("beforeselectionchange");
  23567. if (menu) {
  23568. menu.destroy();
  23569. }
  23570. for (var i = 0, ti, contextItems = []; (ti = items[i]); i++) {
  23571. var last;
  23572. (function(item) {
  23573. if (item == "-") {
  23574. if ((last = contextItems[contextItems.length - 1]) && last !== "-") {
  23575. contextItems.push("-");
  23576. }
  23577. } else if (item.hasOwnProperty("group")) {
  23578. for (var j = 0, cj, subMenu = []; (cj = item.subMenu[j]); j++) {
  23579. (function(subItem) {
  23580. if (subItem == "-") {
  23581. if ((last = subMenu[subMenu.length - 1]) && last !== "-") {
  23582. subMenu.push("-");
  23583. } else {
  23584. subMenu.splice(subMenu.length - 1);
  23585. }
  23586. } else {
  23587. if (
  23588. (me.commands[subItem.cmdName] ||
  23589. UE.commands[subItem.cmdName] ||
  23590. subItem.query) &&
  23591. (subItem.query
  23592. ? subItem.query()
  23593. : me.queryCommandState(subItem.cmdName)) > -1
  23594. ) {
  23595. subMenu.push({
  23596. label:
  23597. subItem.label ||
  23598. me.getLang(
  23599. "contextMenu." +
  23600. subItem.cmdName +
  23601. (subItem.value || "")
  23602. ) ||
  23603. "",
  23604. className:
  23605. "edui-for-" +
  23606. subItem.cmdName +
  23607. (subItem.className
  23608. ? " edui-for-" +
  23609. subItem.cmdName +
  23610. "-" +
  23611. subItem.className
  23612. : ""),
  23613. onclick: subItem.exec
  23614. ? function() {
  23615. subItem.exec.call(me);
  23616. }
  23617. : function() {
  23618. me.execCommand(subItem.cmdName, subItem.value);
  23619. }
  23620. });
  23621. }
  23622. }
  23623. })(cj);
  23624. }
  23625. if (subMenu.length) {
  23626. function getLabel() {
  23627. switch (item.icon) {
  23628. case "table":
  23629. return me.getLang("contextMenu.table");
  23630. case "justifyjustify":
  23631. return me.getLang("contextMenu.paragraph");
  23632. case "aligntd":
  23633. return me.getLang("contextMenu.aligntd");
  23634. case "aligntable":
  23635. return me.getLang("contextMenu.aligntable");
  23636. case "tablesort":
  23637. return lang.tablesort;
  23638. case "borderBack":
  23639. return lang.borderbk;
  23640. default:
  23641. return "";
  23642. }
  23643. }
  23644. contextItems.push({
  23645. //todo 修正成自动获取方式
  23646. label: getLabel(),
  23647. className: "edui-for-" + item.icon,
  23648. subMenu: {
  23649. items: subMenu,
  23650. editor: me
  23651. }
  23652. });
  23653. }
  23654. } else {
  23655. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  23656. if (
  23657. (me.commands[item.cmdName] ||
  23658. UE.commands[item.cmdName] ||
  23659. item.query) &&
  23660. (item.query
  23661. ? item.query.call(me)
  23662. : me.queryCommandState(item.cmdName)) > -1
  23663. ) {
  23664. contextItems.push({
  23665. label: item.label || me.getLang("contextMenu." + item.cmdName),
  23666. className:
  23667. "edui-for-" +
  23668. (item.icon ? item.icon : item.cmdName + (item.value || "")),
  23669. onclick: item.exec
  23670. ? function() {
  23671. item.exec.call(me);
  23672. }
  23673. : function() {
  23674. me.execCommand(item.cmdName, item.value);
  23675. }
  23676. });
  23677. }
  23678. }
  23679. })(ti);
  23680. }
  23681. if (contextItems[contextItems.length - 1] == "-") {
  23682. contextItems.pop();
  23683. }
  23684. menu = new UE.ui.Menu({
  23685. items: contextItems,
  23686. className: "edui-contextmenu",
  23687. editor: me
  23688. });
  23689. menu.render();
  23690. menu.showAt(offset);
  23691. me.fireEvent("aftershowcontextmenu", menu);
  23692. domUtils.preventDefault(evt);
  23693. if (browser.ie) {
  23694. var ieRange;
  23695. try {
  23696. ieRange = me.selection.getNative().createRange();
  23697. } catch (e) {
  23698. return;
  23699. }
  23700. if (ieRange.item) {
  23701. var range = new dom.Range(me.document);
  23702. range.selectNode(ieRange.item(0)).select(true, true);
  23703. }
  23704. }
  23705. });
  23706. // 添加复制的flash按钮
  23707. me.addListener("aftershowcontextmenu", function(type, menu) {
  23708. if (me.zeroclipboard) {
  23709. var items = menu.items;
  23710. for (var key in items) {
  23711. if (items[key].className == "edui-for-copy") {
  23712. me.zeroclipboard.clip(items[key].getDom());
  23713. }
  23714. }
  23715. }
  23716. });
  23717. };
  23718. // plugins/shortcutmenu.js
  23719. ///import core
  23720. ///commands 弹出菜单
  23721. // commandsName popupmenu
  23722. ///commandsTitle 弹出菜单
  23723. /**
  23724. * 弹出菜单
  23725. * @function
  23726. * @name baidu.editor.plugins.popupmenu
  23727. * @author xuheng
  23728. */
  23729. UE.plugins["shortcutmenu"] = function() {
  23730. var me = this,
  23731. menu,
  23732. items = me.options.shortcutMenu || [];
  23733. if (!items.length) {
  23734. return;
  23735. }
  23736. me.addListener("contextmenu mouseup", function(type, e) {
  23737. var me = this,
  23738. customEvt = {
  23739. type: type,
  23740. target: e.target || e.srcElement,
  23741. screenX: e.screenX,
  23742. screenY: e.screenY,
  23743. clientX: e.clientX,
  23744. clientY: e.clientY
  23745. };
  23746. setTimeout(function() {
  23747. var rng = me.selection.getRange();
  23748. if (rng.collapsed === false || type == "contextmenu") {
  23749. if (!menu) {
  23750. menu = new baidu.editor.ui.ShortCutMenu({
  23751. editor: me,
  23752. items: items,
  23753. theme: me.options.theme,
  23754. className: "edui-shortcutmenu"
  23755. });
  23756. menu.render();
  23757. me.fireEvent("afterrendershortcutmenu", menu);
  23758. }
  23759. menu.show(customEvt, !!UE.plugins["contextmenu"]);
  23760. }
  23761. });
  23762. if (type == "contextmenu") {
  23763. domUtils.preventDefault(e);
  23764. if (browser.ie9below) {
  23765. var ieRange;
  23766. try {
  23767. ieRange = me.selection.getNative().createRange();
  23768. } catch (e) {
  23769. return;
  23770. }
  23771. if (ieRange.item) {
  23772. var range = new dom.Range(me.document);
  23773. range.selectNode(ieRange.item(0)).select(true, true);
  23774. }
  23775. }
  23776. }
  23777. });
  23778. me.addListener("keydown", function(type) {
  23779. if (type == "keydown") {
  23780. menu && !menu.isHidden && menu.hide();
  23781. }
  23782. });
  23783. };
  23784. // plugins/basestyle.js
  23785. /**
  23786. * B、I、sub、super命令支持
  23787. * @file
  23788. * @since 1.2.6.1
  23789. */
  23790. UE.plugins["basestyle"] = function() {
  23791. /**
  23792. * 字体加粗
  23793. * @command bold
  23794. * @param { String } cmd 命令字符串
  23795. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  23796. * @method execCommand
  23797. * @example
  23798. * ```javascript
  23799. * //editor是编辑器实例
  23800. * //对当前选中的文本内容执行加粗操作
  23801. * //第一次执行, 文本内容加粗
  23802. * editor.execCommand( 'bold' );
  23803. *
  23804. * //第二次执行, 文本内容取消加粗
  23805. * editor.execCommand( 'bold' );
  23806. * ```
  23807. */
  23808. /**
  23809. * 字体倾斜
  23810. * @command italic
  23811. * @method execCommand
  23812. * @param { String } cmd 命令字符串
  23813. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  23814. * @example
  23815. * ```javascript
  23816. * //editor是编辑器实例
  23817. * //对当前选中的文本内容执行斜体操作
  23818. * //第一次操作, 文本内容将变成斜体
  23819. * editor.execCommand( 'italic' );
  23820. *
  23821. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23822. * editor.execCommand( 'italic' );
  23823. * ```
  23824. */
  23825. /**
  23826. * 下标文本,与“superscript”命令互斥
  23827. * @command subscript
  23828. * @method execCommand
  23829. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  23830. * @param { String } cmd 命令字符串
  23831. * @example
  23832. * ```javascript
  23833. * //editor是编辑器实例
  23834. * //对当前选中的文本内容执行下标操作
  23835. * //第一次操作, 文本内容将变成下标文本
  23836. * editor.execCommand( 'subscript' );
  23837. *
  23838. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23839. * editor.execCommand( 'subscript' );
  23840. * ```
  23841. */
  23842. /**
  23843. * 上标文本,与“subscript”命令互斥
  23844. * @command superscript
  23845. * @method execCommand
  23846. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  23847. * @param { String } cmd 命令字符串
  23848. * @example
  23849. * ```javascript
  23850. * //editor是编辑器实例
  23851. * //对当前选中的文本内容执行上标操作
  23852. * //第一次操作, 文本内容将变成上标文本
  23853. * editor.execCommand( 'superscript' );
  23854. *
  23855. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23856. * editor.execCommand( 'superscript' );
  23857. * ```
  23858. */
  23859. var basestyles = {
  23860. bold: ["strong", "b"],
  23861. italic: ["em", "i"],
  23862. subscript: ["sub"],
  23863. superscript: ["sup"]
  23864. },
  23865. getObj = function(editor, tagNames) {
  23866. return domUtils.filterNodeList(
  23867. editor.selection.getStartElementPath(),
  23868. tagNames
  23869. );
  23870. },
  23871. me = this;
  23872. //添加快捷键
  23873. me.addshortcutkey({
  23874. Bold: "ctrl+66", //^B
  23875. Italic: "ctrl+73", //^I
  23876. Underline: "ctrl+85" //^U
  23877. });
  23878. me.addInputRule(function(root) {
  23879. utils.each(root.getNodesByTagName("b i"), function(node) {
  23880. switch (node.tagName) {
  23881. case "b":
  23882. node.tagName = "strong";
  23883. break;
  23884. case "i":
  23885. node.tagName = "em";
  23886. }
  23887. });
  23888. });
  23889. for (var style in basestyles) {
  23890. (function(cmd, tagNames) {
  23891. me.commands[cmd] = {
  23892. execCommand: function(cmdName) {
  23893. var range = me.selection.getRange(),
  23894. obj = getObj(this, tagNames);
  23895. if (range.collapsed) {
  23896. if (obj) {
  23897. var tmpText = me.document.createTextNode("");
  23898. range.insertNode(tmpText).removeInlineStyle(tagNames);
  23899. range.setStartBefore(tmpText);
  23900. domUtils.remove(tmpText);
  23901. } else {
  23902. var tmpNode = range.document.createElement(tagNames[0]);
  23903. if (cmdName == "superscript" || cmdName == "subscript") {
  23904. tmpText = me.document.createTextNode("");
  23905. range
  23906. .insertNode(tmpText)
  23907. .removeInlineStyle(["sub", "sup"])
  23908. .setStartBefore(tmpText)
  23909. .collapse(true);
  23910. }
  23911. range.insertNode(tmpNode).setStart(tmpNode, 0);
  23912. }
  23913. range.collapse(true);
  23914. } else {
  23915. if (cmdName == "superscript" || cmdName == "subscript") {
  23916. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  23917. range.removeInlineStyle(["sub", "sup"]);
  23918. }
  23919. }
  23920. obj
  23921. ? range.removeInlineStyle(tagNames)
  23922. : range.applyInlineStyle(tagNames[0]);
  23923. }
  23924. range.select();
  23925. },
  23926. queryCommandState: function() {
  23927. return getObj(this, tagNames) ? 1 : 0;
  23928. }
  23929. };
  23930. })(style, basestyles[style]);
  23931. }
  23932. };
  23933. // plugins/elementpath.js
  23934. /**
  23935. * 选取路径命令
  23936. * @file
  23937. */
  23938. UE.plugins["elementpath"] = function() {
  23939. var currentLevel,
  23940. tagNames,
  23941. me = this;
  23942. me.setOpt("elementPathEnabled", true);
  23943. if (!me.options.elementPathEnabled) {
  23944. return;
  23945. }
  23946. me.commands["elementpath"] = {
  23947. execCommand: function(cmdName, level) {
  23948. var start = tagNames[level],
  23949. range = me.selection.getRange();
  23950. currentLevel = level * 1;
  23951. range.selectNode(start).select();
  23952. },
  23953. queryCommandValue: function() {
  23954. //产生一个副本,不能修改原来的startElementPath;
  23955. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  23956. names = [];
  23957. tagNames = parents;
  23958. for (var i = 0, ci; (ci = parents[i]); i++) {
  23959. if (ci.nodeType == 3) {
  23960. continue;
  23961. }
  23962. var name = ci.tagName.toLowerCase();
  23963. if (name == "img" && ci.getAttribute("anchorname")) {
  23964. name = "anchor";
  23965. }
  23966. names[i] = name;
  23967. if (currentLevel == i) {
  23968. currentLevel = -1;
  23969. break;
  23970. }
  23971. }
  23972. return names;
  23973. }
  23974. };
  23975. };
  23976. // plugins/formatmatch.js
  23977. /**
  23978. * 格式刷,只格式inline的
  23979. * @file
  23980. * @since 1.2.6.1
  23981. */
  23982. /**
  23983. * 格式刷
  23984. * @command formatmatch
  23985. * @method execCommand
  23986. * @remind 该操作不能复制段落格式
  23987. * @param { String } cmd 命令字符串
  23988. * @example
  23989. * ```javascript
  23990. * //editor是编辑器实例
  23991. * //获取格式刷
  23992. * editor.execCommand( 'formatmatch' );
  23993. * ```
  23994. */
  23995. UE.plugins["formatmatch"] = function() {
  23996. var me = this,
  23997. list = [],
  23998. img,
  23999. flag = 0;
  24000. me.addListener("reset", function() {
  24001. list = [];
  24002. flag = 0;
  24003. });
  24004. function addList(type, evt) {
  24005. if (browser.webkit) {
  24006. var target = evt.target.tagName == "IMG" ? evt.target : null;
  24007. }
  24008. function addFormat(range) {
  24009. if (text) {
  24010. range.selectNode(text);
  24011. }
  24012. return range.applyInlineStyle(list[list.length - 1].tagName, null, list);
  24013. }
  24014. me.undoManger && me.undoManger.save();
  24015. var range = me.selection.getRange(),
  24016. imgT = target || range.getClosedNode();
  24017. if (img && imgT && imgT.tagName == "IMG") {
  24018. //trace:964
  24019. imgT.style.cssText +=
  24020. ";float:" +
  24021. (img.style.cssFloat || img.style.styleFloat || "none") +
  24022. ";display:" +
  24023. (img.style.display || "inline");
  24024. img = null;
  24025. } else {
  24026. if (!img) {
  24027. var collapsed = range.collapsed;
  24028. if (collapsed) {
  24029. var text = me.document.createTextNode("match");
  24030. range.insertNode(text).select();
  24031. }
  24032. me.__hasEnterExecCommand = true;
  24033. //不能把block上的属性干掉
  24034. //trace:1553
  24035. var removeFormatAttributes = me.options.removeFormatAttributes;
  24036. me.options.removeFormatAttributes = "";
  24037. me.execCommand("removeformat");
  24038. me.options.removeFormatAttributes = removeFormatAttributes;
  24039. me.__hasEnterExecCommand = false;
  24040. //trace:969
  24041. range = me.selection.getRange();
  24042. if (list.length) {
  24043. addFormat(range);
  24044. }
  24045. if (text) {
  24046. range.setStartBefore(text).collapse(true);
  24047. }
  24048. range.select();
  24049. text && domUtils.remove(text);
  24050. }
  24051. }
  24052. me.undoManger && me.undoManger.save();
  24053. me.removeListener("mouseup", addList);
  24054. flag = 0;
  24055. }
  24056. me.commands["formatmatch"] = {
  24057. execCommand: function(cmdName) {
  24058. if (flag) {
  24059. flag = 0;
  24060. list = [];
  24061. me.removeListener("mouseup", addList);
  24062. return;
  24063. }
  24064. var range = me.selection.getRange();
  24065. img = range.getClosedNode();
  24066. if (!img || img.tagName != "IMG") {
  24067. range.collapse(true).shrinkBoundary();
  24068. var start = range.startContainer;
  24069. list = domUtils.findParents(start, true, function(node) {
  24070. return !domUtils.isBlockElm(node) && node.nodeType == 1;
  24071. });
  24072. //a不能加入格式刷, 并且克隆节点
  24073. for (var i = 0, ci; (ci = list[i]); i++) {
  24074. if (ci.tagName == "A") {
  24075. list.splice(i, 1);
  24076. break;
  24077. }
  24078. }
  24079. }
  24080. me.addListener("mouseup", addList);
  24081. flag = 1;
  24082. },
  24083. queryCommandState: function() {
  24084. return flag;
  24085. },
  24086. notNeedUndo: 1
  24087. };
  24088. };
  24089. // plugins/searchreplace.js
  24090. ///import core
  24091. ///commands 查找替换
  24092. ///commandsName SearchReplace
  24093. ///commandsTitle 查询替换
  24094. ///commandsDialog dialogs\searchreplace
  24095. /**
  24096. * @description 查找替换
  24097. * @author zhanyi
  24098. */
  24099. UE.plugin.register("searchreplace", function() {
  24100. var me = this;
  24101. var _blockElm = { table: 1, tbody: 1, tr: 1, ol: 1, ul: 1 };
  24102. var lastRng = null;
  24103. function getText(node) {
  24104. var text = node.nodeType == 3
  24105. ? node.nodeValue
  24106. : node[browser.ie ? "innerText" : "textContent"];
  24107. return text.replace(domUtils.fillChar, "");
  24108. }
  24109. function findTextInString(textContent, opt, currentIndex) {
  24110. var str = opt.searchStr;
  24111. var reg = new RegExp(str, "g" + (opt.casesensitive ? "" : "i")),
  24112. match;
  24113. if (opt.dir == -1) {
  24114. textContent = textContent.substr(0, currentIndex);
  24115. textContent = textContent.split("").reverse().join("");
  24116. str = str.split("").reverse().join("");
  24117. match = reg.exec(textContent);
  24118. if (match) {
  24119. return currentIndex - match.index - str.length;
  24120. }
  24121. } else {
  24122. textContent = textContent.substr(currentIndex);
  24123. match = reg.exec(textContent);
  24124. if (match) {
  24125. return match.index + currentIndex;
  24126. }
  24127. }
  24128. return -1;
  24129. }
  24130. function findTextBlockElm(node, currentIndex, opt) {
  24131. var textContent,
  24132. index,
  24133. methodName = opt.all || opt.dir == 1 ? "getNextDomNode" : "getPreDomNode";
  24134. if (domUtils.isBody(node)) {
  24135. node = node.firstChild;
  24136. }
  24137. var first = 1;
  24138. while (node) {
  24139. textContent = getText(node);
  24140. index = findTextInString(textContent, opt, currentIndex);
  24141. first = 0;
  24142. if (index != -1) {
  24143. return {
  24144. node: node,
  24145. index: index
  24146. };
  24147. }
  24148. node = domUtils[methodName](node);
  24149. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  24150. node = domUtils[methodName](node, true);
  24151. }
  24152. if (node) {
  24153. currentIndex = opt.dir == -1 ? getText(node).length : 0;
  24154. }
  24155. }
  24156. }
  24157. function findNTextInBlockElm(node, index, str) {
  24158. var currentIndex = 0,
  24159. currentNode = node.firstChild,
  24160. currentNodeLength = 0,
  24161. result;
  24162. while (currentNode) {
  24163. if (currentNode.nodeType == 3) {
  24164. currentNodeLength = getText(currentNode).replace(
  24165. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24166. ""
  24167. ).length;
  24168. currentIndex += currentNodeLength;
  24169. if (currentIndex >= index) {
  24170. return {
  24171. node: currentNode,
  24172. index: currentNodeLength - (currentIndex - index)
  24173. };
  24174. }
  24175. } else if (!dtd.$empty[currentNode.tagName]) {
  24176. currentNodeLength = getText(currentNode).replace(
  24177. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24178. ""
  24179. ).length;
  24180. currentIndex += currentNodeLength;
  24181. if (currentIndex >= index) {
  24182. result = findNTextInBlockElm(
  24183. currentNode,
  24184. currentNodeLength - (currentIndex - index),
  24185. str
  24186. );
  24187. if (result) {
  24188. return result;
  24189. }
  24190. }
  24191. }
  24192. currentNode = domUtils.getNextDomNode(currentNode);
  24193. }
  24194. }
  24195. function searchReplace(me, opt) {
  24196. var rng = lastRng || me.selection.getRange(),
  24197. startBlockNode,
  24198. searchStr = opt.searchStr,
  24199. span = me.document.createElement("span");
  24200. span.innerHTML = "$$ueditor_searchreplace_key$$";
  24201. rng.shrinkBoundary(true);
  24202. //判断是不是第一次选中
  24203. if (!rng.collapsed) {
  24204. rng.select();
  24205. var rngText = me.selection.getText();
  24206. if (
  24207. new RegExp(
  24208. "^" + opt.searchStr + "$",
  24209. opt.casesensitive ? "" : "i"
  24210. ).test(rngText)
  24211. ) {
  24212. if (opt.replaceStr != undefined) {
  24213. replaceText(rng, opt.replaceStr);
  24214. rng.select();
  24215. return true;
  24216. } else {
  24217. rng.collapse(opt.dir == -1);
  24218. }
  24219. }
  24220. }
  24221. rng.insertNode(span);
  24222. rng.enlargeToBlockElm(true);
  24223. startBlockNode = rng.startContainer;
  24224. var currentIndex = getText(startBlockNode).indexOf(
  24225. "$$ueditor_searchreplace_key$$"
  24226. );
  24227. rng.setStartBefore(span);
  24228. domUtils.remove(span);
  24229. var result = findTextBlockElm(startBlockNode, currentIndex, opt);
  24230. if (result) {
  24231. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr);
  24232. var rngEnd = findNTextInBlockElm(
  24233. result.node,
  24234. result.index + searchStr.length,
  24235. searchStr
  24236. );
  24237. rng
  24238. .setStart(rngStart.node, rngStart.index)
  24239. .setEnd(rngEnd.node, rngEnd.index);
  24240. if (opt.replaceStr !== undefined) {
  24241. replaceText(rng, opt.replaceStr);
  24242. }
  24243. rng.select();
  24244. return true;
  24245. } else {
  24246. rng.setCursor();
  24247. }
  24248. }
  24249. function replaceText(rng, str) {
  24250. str = me.document.createTextNode(str);
  24251. rng.deleteContents().insertNode(str);
  24252. }
  24253. return {
  24254. commands: {
  24255. searchreplace: {
  24256. execCommand: function(cmdName, opt) {
  24257. utils.extend(
  24258. opt,
  24259. {
  24260. all: false,
  24261. casesensitive: false,
  24262. dir: 1
  24263. },
  24264. true
  24265. );
  24266. var num = 0;
  24267. if (opt.all) {
  24268. lastRng = null;
  24269. var rng = me.selection.getRange(),
  24270. first = me.body.firstChild;
  24271. if (first && first.nodeType == 1) {
  24272. rng.setStart(first, 0);
  24273. rng.shrinkBoundary(true);
  24274. } else if (first.nodeType == 3) {
  24275. rng.setStartBefore(first);
  24276. }
  24277. rng.collapse(true).select(true);
  24278. if (opt.replaceStr !== undefined) {
  24279. me.fireEvent("saveScene");
  24280. }
  24281. while (searchReplace(this, opt)) {
  24282. num++;
  24283. lastRng = me.selection.getRange();
  24284. lastRng.collapse(opt.dir == -1);
  24285. }
  24286. if (num) {
  24287. me.fireEvent("saveScene");
  24288. }
  24289. } else {
  24290. if (opt.replaceStr !== undefined) {
  24291. me.fireEvent("saveScene");
  24292. }
  24293. if (searchReplace(this, opt)) {
  24294. num++;
  24295. lastRng = me.selection.getRange();
  24296. lastRng.collapse(opt.dir == -1);
  24297. }
  24298. if (num) {
  24299. me.fireEvent("saveScene");
  24300. }
  24301. }
  24302. return num;
  24303. },
  24304. notNeedUndo: 1
  24305. }
  24306. },
  24307. bindEvents: {
  24308. clearlastSearchResult: function() {
  24309. lastRng = null;
  24310. }
  24311. }
  24312. };
  24313. });
  24314. // plugins/customstyle.js
  24315. /**
  24316. * 自定义样式
  24317. * @file
  24318. * @since 1.2.6.1
  24319. */
  24320. /**
  24321. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  24322. * @command customstyle
  24323. * @method execCommand
  24324. * @param { String } cmd 命令字符串
  24325. * @example
  24326. * ```javascript
  24327. * editor.execCommand( 'customstyle' );
  24328. * ```
  24329. */
  24330. UE.plugins["customstyle"] = function() {
  24331. var me = this;
  24332. me.setOpt({
  24333. customstyle: [
  24334. {
  24335. tag: "h1",
  24336. name: "tc",
  24337. style:
  24338. "font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:center;margin:0 0 20px 0;"
  24339. },
  24340. {
  24341. tag: "h1",
  24342. name: "tl",
  24343. style:
  24344. "font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:left;margin:0 0 10px 0;"
  24345. },
  24346. {
  24347. tag: "span",
  24348. name: "im",
  24349. style:
  24350. "font-size:16px;font-style:italic;font-weight:bold;line-height:18px;"
  24351. },
  24352. {
  24353. tag: "span",
  24354. name: "hi",
  24355. style:
  24356. "font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;"
  24357. }
  24358. ]
  24359. });
  24360. me.commands["customstyle"] = {
  24361. execCommand: function(cmdName, obj) {
  24362. var me = this,
  24363. tagName = obj.tag,
  24364. node = domUtils.findParent(
  24365. me.selection.getStart(),
  24366. function(node) {
  24367. return node.getAttribute("label");
  24368. },
  24369. true
  24370. ),
  24371. range,
  24372. bk,
  24373. tmpObj = {};
  24374. for (var p in obj) {
  24375. if (obj[p] !== undefined) tmpObj[p] = obj[p];
  24376. }
  24377. delete tmpObj.tag;
  24378. if (node && node.getAttribute("label") == obj.label) {
  24379. range = this.selection.getRange();
  24380. bk = range.createBookmark();
  24381. if (range.collapsed) {
  24382. //trace:1732 删掉自定义标签,要有p来回填站位
  24383. if (dtd.$block[node.tagName]) {
  24384. var fillNode = me.document.createElement("p");
  24385. domUtils.moveChild(node, fillNode);
  24386. node.parentNode.insertBefore(fillNode, node);
  24387. domUtils.remove(node);
  24388. } else {
  24389. domUtils.remove(node, true);
  24390. }
  24391. } else {
  24392. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  24393. nodes = domUtils.getElementsByTagName(common, tagName);
  24394. if (new RegExp(tagName, "i").test(common.tagName)) {
  24395. nodes.push(common);
  24396. }
  24397. for (var i = 0, ni; (ni = nodes[i++]); ) {
  24398. if (ni.getAttribute("label") == obj.label) {
  24399. var ps = domUtils.getPosition(ni, bk.start),
  24400. pe = domUtils.getPosition(ni, bk.end);
  24401. if (
  24402. (ps & domUtils.POSITION_FOLLOWING ||
  24403. ps & domUtils.POSITION_CONTAINS) &&
  24404. (pe & domUtils.POSITION_PRECEDING ||
  24405. pe & domUtils.POSITION_CONTAINS)
  24406. )
  24407. if (dtd.$block[tagName]) {
  24408. var fillNode = me.document.createElement("p");
  24409. domUtils.moveChild(ni, fillNode);
  24410. ni.parentNode.insertBefore(fillNode, ni);
  24411. }
  24412. domUtils.remove(ni, true);
  24413. }
  24414. }
  24415. node = domUtils.findParent(
  24416. common,
  24417. function(node) {
  24418. return node.getAttribute("label") == obj.label;
  24419. },
  24420. true
  24421. );
  24422. if (node) {
  24423. domUtils.remove(node, true);
  24424. }
  24425. }
  24426. range.moveToBookmark(bk).select();
  24427. } else {
  24428. if (dtd.$block[tagName]) {
  24429. this.execCommand("paragraph", tagName, tmpObj, "customstyle");
  24430. range = me.selection.getRange();
  24431. if (!range.collapsed) {
  24432. range.collapse();
  24433. node = domUtils.findParent(
  24434. me.selection.getStart(),
  24435. function(node) {
  24436. return node.getAttribute("label") == obj.label;
  24437. },
  24438. true
  24439. );
  24440. var pNode = me.document.createElement("p");
  24441. domUtils.insertAfter(node, pNode);
  24442. domUtils.fillNode(me.document, pNode);
  24443. range.setStart(pNode, 0).setCursor();
  24444. }
  24445. } else {
  24446. range = me.selection.getRange();
  24447. if (range.collapsed) {
  24448. node = me.document.createElement(tagName);
  24449. domUtils.setAttributes(node, tmpObj);
  24450. range.insertNode(node).setStart(node, 0).setCursor();
  24451. return;
  24452. }
  24453. bk = range.createBookmark();
  24454. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select();
  24455. }
  24456. }
  24457. },
  24458. queryCommandValue: function() {
  24459. var parent = domUtils.filterNodeList(
  24460. this.selection.getStartElementPath(),
  24461. function(node) {
  24462. return node.getAttribute("label");
  24463. }
  24464. );
  24465. return parent ? parent.getAttribute("label") : "";
  24466. }
  24467. };
  24468. //当去掉customstyle是,如果是块元素,用p代替
  24469. me.addListener("keyup", function(type, evt) {
  24470. var keyCode = evt.keyCode || evt.which;
  24471. if (keyCode == 32 || keyCode == 13) {
  24472. var range = me.selection.getRange();
  24473. if (range.collapsed) {
  24474. var node = domUtils.findParent(
  24475. me.selection.getStart(),
  24476. function(node) {
  24477. return node.getAttribute("label");
  24478. },
  24479. true
  24480. );
  24481. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  24482. var p = me.document.createElement("p");
  24483. domUtils.insertAfter(node, p);
  24484. domUtils.fillNode(me.document, p);
  24485. domUtils.remove(node);
  24486. range.setStart(p, 0).setCursor();
  24487. }
  24488. }
  24489. }
  24490. });
  24491. };
  24492. // plugins/catchremoteimage.js
  24493. ///import core
  24494. ///commands 远程图片抓取
  24495. ///commandsName catchRemoteImage,catchremoteimageenable
  24496. ///commandsTitle 远程图片抓取
  24497. /**
  24498. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  24499. */
  24500. UE.plugins['catchremoteimage'] = function () {
  24501. var me = this,
  24502. imgArr = [];
  24503. /* 设置默认值 */
  24504. if (me.options.catchRemoteImageEnable === false) return;
  24505. me.setOpt({
  24506. catchRemoteImageEnable: false
  24507. });
  24508. me.addListener("afterpaste", function () {
  24509. var catcherLocalDomain = me.getOpt('catcherLocalDomain');
  24510. var remoteImages = [],
  24511. imgs = UE.dom.domUtils.getElementsByTagName(me.document, "img"),
  24512. test = function (src, urls) {
  24513. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  24514. return true;
  24515. }
  24516. if (urls) {
  24517. for (var j = 0, url; url = urls[j++];) {
  24518. if (src.indexOf(url) !== -1) {
  24519. return true;
  24520. }
  24521. }
  24522. }
  24523. return false;
  24524. };
  24525. for (var i = 0, ci; ci = imgs[i++];) {
  24526. if (ci.getAttribute("word_img")) {
  24527. continue;
  24528. }
  24529. var src = ci.getAttribute("_src") || ci.src || "";
  24530. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  24531. remoteImages.push(src);
  24532. }
  24533. }
  24534. if (remoteImages.length) {
  24535. /* 获取源路径和新路径 */
  24536. var oldSrc,elem;
  24537. for (var k = 0; elem = imgs[k++];) {
  24538. oldSrc = elem.getAttribute("_src") || elem.src || "";
  24539. getBlob(oldSrc,elem, function (base64Data,el) {
  24540. var file = dataURLtoFile(base64Data, (new Date()).valueOf() + '.jpg');
  24541. var Upload = me.getOpt("uploadService")(me, me).Upload();
  24542. var Fast = me.getOpt("uploadService")(me, me).Fast();
  24543. Upload.api.send(file, function (res) {
  24544. var newSrc = Fast.api.cdnurl(res.url, true);
  24545. UE.dom.domUtils.setAttributes(el, {
  24546. "src": newSrc,
  24547. "_src": newSrc
  24548. });
  24549. // console.log('fileData', file, el);
  24550. });
  24551. // imgArr.push({ el: el, file: file, oldSrc: oldSrc });
  24552. });
  24553. }
  24554. me.fireEvent('catchremotesuccess');
  24555. }
  24556. // queue(imgArr);
  24557. function queue(arr) {
  24558. var obj = arr.pop();
  24559. var Upload = me.getOpt("uploadService")(me, me).Upload();
  24560. var Fast = me.getOpt("uploadService")(me, me).Fast();
  24561. Upload.api.send(obj.file, function (imgUrl) {
  24562. newSrc = Fast.api.cdnurl(imgUrl, true);
  24563. UE.dom.domUtils.setAttributes(obj.el, {
  24564. "src": newSrc,
  24565. "_src": newSrc
  24566. });
  24567. if (arr.length > 0) {
  24568. queue(arr);
  24569. }
  24570. });
  24571. }
  24572. function dataURLtoFile(data, filename) {
  24573. var arr = data.split(','),
  24574. mime = arr[0].match(/:(.*?);/)[1],
  24575. bstr = atob(arr[1]),
  24576. n = bstr.length,
  24577. u8arr = new Uint8Array(n);
  24578. while (n--) {
  24579. u8arr[n] = bstr.charCodeAt(n);
  24580. }
  24581. return new File([u8arr], filename, {
  24582. type: mime
  24583. });
  24584. }
  24585. function getBlob(imgUrl, elem, cb) {
  24586. window.URL = window.URL || window.webkitURL;
  24587. var xhr = new XMLHttpRequest();
  24588. xhr.open("get", imgUrl, true);
  24589. // 至关重要
  24590. xhr.responseType = "blob";
  24591. xhr.onload = function () {
  24592. if (this.status == 200) {
  24593. //得到一个blob对象
  24594. var blob = this.response;
  24595. var oFileReader = new FileReader();
  24596. oFileReader.onloadend = function (e) {
  24597. // 此处拿到的已经是 base64的图片了
  24598. var base64 = e.target.result;
  24599. cb(base64, elem);
  24600. };
  24601. oFileReader.readAsDataURL(blob);
  24602. }
  24603. }
  24604. xhr.send();
  24605. }
  24606. });
  24607. };
  24608. // plugins/snapscreen.js
  24609. /**
  24610. * 截屏插件,为UEditor提供插入支持
  24611. * @file
  24612. * @since 1.4.2
  24613. */
  24614. UE.plugin.register("snapscreen", function() {
  24615. var me = this;
  24616. var snapplugin;
  24617. function getLocation(url) {
  24618. var search,
  24619. a = document.createElement("a"),
  24620. params = utils.serializeParam(me.queryCommandValue("serverparam")) || "";
  24621. a.href = url;
  24622. if (browser.ie) {
  24623. a.href = a.href;
  24624. }
  24625. search = a.search;
  24626. if (params) {
  24627. search = search + (search.indexOf("?") == -1 ? "?" : "&") + params;
  24628. search = search.replace(/[&]+/gi, "&");
  24629. }
  24630. return {
  24631. port: a.port,
  24632. hostname: a.hostname,
  24633. path: a.pathname + search || +a.hash
  24634. };
  24635. }
  24636. return {
  24637. commands: {
  24638. /**
  24639. * 字体背景颜色
  24640. * @command snapscreen
  24641. * @method execCommand
  24642. * @param { String } cmd 命令字符串
  24643. * @example
  24644. * ```javascript
  24645. * editor.execCommand('snapscreen');
  24646. * ```
  24647. */
  24648. snapscreen: {
  24649. execCommand: function(cmd) {
  24650. var url, local, res;
  24651. var lang = me.getLang("snapScreen_plugin");
  24652. if (!snapplugin) {
  24653. var container = me.container;
  24654. var doc = me.container.ownerDocument || me.container.document;
  24655. snapplugin = doc.createElement("object");
  24656. try {
  24657. snapplugin.type = "application/x-pluginbaidusnap";
  24658. } catch (e) {
  24659. return;
  24660. }
  24661. snapplugin.style.cssText =
  24662. "position:absolute;left:-9999px;width:0;height:0;";
  24663. snapplugin.setAttribute("width", "0");
  24664. snapplugin.setAttribute("height", "0");
  24665. container.appendChild(snapplugin);
  24666. }
  24667. function onSuccess(rs) {
  24668. try {
  24669. rs = eval("(" + rs + ")");
  24670. if (rs.state == "SUCCESS") {
  24671. var opt = me.options;
  24672. me.execCommand("insertimage", {
  24673. src: opt.snapscreenUrlPrefix + rs.url,
  24674. _src: opt.snapscreenUrlPrefix + rs.url,
  24675. alt: rs.title || "",
  24676. floatStyle: opt.snapscreenImgAlign
  24677. });
  24678. } else {
  24679. alert(rs.state);
  24680. }
  24681. } catch (e) {
  24682. alert(lang.callBackErrorMsg);
  24683. }
  24684. }
  24685. url = me.getActionUrl(me.getOpt("snapscreenActionName"));
  24686. local = getLocation(url);
  24687. setTimeout(function() {
  24688. try {
  24689. res = snapplugin.saveSnapshot(
  24690. local.hostname,
  24691. local.path,
  24692. local.port
  24693. );
  24694. } catch (e) {
  24695. me.ui._dialogs["snapscreenDialog"].open();
  24696. return;
  24697. }
  24698. onSuccess(res);
  24699. }, 50);
  24700. },
  24701. queryCommandState: function() {
  24702. return navigator.userAgent.indexOf("Windows", 0) != -1 ? 0 : -1;
  24703. }
  24704. }
  24705. }
  24706. };
  24707. });
  24708. // plugins/insertparagraph.js
  24709. /**
  24710. * 插入段落
  24711. * @file
  24712. * @since 1.2.6.1
  24713. */
  24714. /**
  24715. * 插入段落
  24716. * @command insertparagraph
  24717. * @method execCommand
  24718. * @param { String } cmd 命令字符串
  24719. * @example
  24720. * ```javascript
  24721. * //editor是编辑器实例
  24722. * editor.execCommand( 'insertparagraph' );
  24723. * ```
  24724. */
  24725. UE.commands["insertparagraph"] = {
  24726. execCommand: function(cmdName, front) {
  24727. var me = this,
  24728. range = me.selection.getRange(),
  24729. start = range.startContainer,
  24730. tmpNode;
  24731. while (start) {
  24732. if (domUtils.isBody(start)) {
  24733. break;
  24734. }
  24735. tmpNode = start;
  24736. start = start.parentNode;
  24737. }
  24738. if (tmpNode) {
  24739. var p = me.document.createElement("p");
  24740. if (front) {
  24741. tmpNode.parentNode.insertBefore(p, tmpNode);
  24742. } else {
  24743. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling);
  24744. }
  24745. domUtils.fillNode(me.document, p);
  24746. range.setStart(p, 0).setCursor(false, true);
  24747. }
  24748. }
  24749. };
  24750. // plugins/webapp.js
  24751. /**
  24752. * 百度应用
  24753. * @file
  24754. * @since 1.2.6.1
  24755. */
  24756. /**
  24757. * 插入百度应用
  24758. * @command webapp
  24759. * @method execCommand
  24760. * @remind 需要百度APPKey
  24761. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  24762. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  24763. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  24764. * @example
  24765. * ```javascript
  24766. * //editor是编辑器实例
  24767. * //在编辑器里插入一个“植物大战僵尸”的APP
  24768. * editor.execCommand( 'webapp' , {
  24769. * title: '植物大战僵尸',
  24770. * width: 560,
  24771. * height: 465,
  24772. * logo: '应用展示的图片',
  24773. * url: '百度应用的地址'
  24774. * } );
  24775. * ```
  24776. */
  24777. //UE.plugins['webapp'] = function () {
  24778. // var me = this;
  24779. // function createInsertStr( obj, toIframe, addParagraph ) {
  24780. // return !toIframe ?
  24781. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  24782. // ' 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 + '" />' +
  24783. // (addParagraph ? '</p>' : '')
  24784. // :
  24785. // '<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>';
  24786. // }
  24787. //
  24788. // function switchImgAndIframe( img2frame ) {
  24789. // var tmpdiv,
  24790. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  24791. // for ( var i = 0, node; node = nodes[i++]; ) {
  24792. // if ( node.className != "edui-faked-webapp" ){
  24793. // continue;
  24794. // }
  24795. // tmpdiv = me.document.createElement( "div" );
  24796. // 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 );
  24797. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  24798. // }
  24799. // }
  24800. //
  24801. // me.addListener( "beforegetcontent", function () {
  24802. // switchImgAndIframe( true );
  24803. // } );
  24804. // me.addListener( 'aftersetcontent', function () {
  24805. // switchImgAndIframe( false );
  24806. // } );
  24807. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  24808. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  24809. // return;
  24810. // }
  24811. // switchImgAndIframe( false );
  24812. // } );
  24813. //
  24814. // me.commands['webapp'] = {
  24815. // execCommand:function ( cmd, obj ) {
  24816. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  24817. // }
  24818. // };
  24819. //};
  24820. UE.plugin.register("webapp", function() {
  24821. var me = this;
  24822. function createInsertStr(obj, toEmbed) {
  24823. return !toEmbed
  24824. ? '<img title="' +
  24825. obj.title +
  24826. '" width="' +
  24827. obj.width +
  24828. '" height="' +
  24829. obj.height +
  24830. '"' +
  24831. ' src="' +
  24832. me.options.UEDITOR_HOME_URL +
  24833. 'themes/default/images/spacer.gif" _logo_url="' +
  24834. obj.logo +
  24835. '" style="background:url(' +
  24836. obj.logo +
  24837. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' +
  24838. obj.url +
  24839. '" ' +
  24840. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : "") +
  24841. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : "") +
  24842. "/>"
  24843. : '<iframe class="edui-faked-webapp" title="' +
  24844. obj.title +
  24845. '" ' +
  24846. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : "") +
  24847. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : "") +
  24848. 'width="' +
  24849. obj.width +
  24850. '" height="' +
  24851. obj.height +
  24852. '" scrolling="no" frameborder="0" src="' +
  24853. obj.url +
  24854. '" logo_url = "' +
  24855. obj.logo +
  24856. '"></iframe>';
  24857. }
  24858. return {
  24859. outputRule: function(root) {
  24860. utils.each(root.getNodesByTagName("img"), function(node) {
  24861. var html;
  24862. if (node.getAttr("class") == "edui-faked-webapp") {
  24863. html = createInsertStr(
  24864. {
  24865. title: node.getAttr("title"),
  24866. width: node.getAttr("width"),
  24867. height: node.getAttr("height"),
  24868. align: node.getAttr("align"),
  24869. cssfloat: node.getStyle("float"),
  24870. url: node.getAttr("_url"),
  24871. logo: node.getAttr("_logo_url")
  24872. },
  24873. true
  24874. );
  24875. var embed = UE.uNode.createElement(html);
  24876. node.parentNode.replaceChild(embed, node);
  24877. }
  24878. });
  24879. },
  24880. inputRule: function(root) {
  24881. utils.each(root.getNodesByTagName("iframe"), function(node) {
  24882. if (node.getAttr("class") == "edui-faked-webapp") {
  24883. var img = UE.uNode.createElement(
  24884. createInsertStr({
  24885. title: node.getAttr("title"),
  24886. width: node.getAttr("width"),
  24887. height: node.getAttr("height"),
  24888. align: node.getAttr("align"),
  24889. cssfloat: node.getStyle("float"),
  24890. url: node.getAttr("src"),
  24891. logo: node.getAttr("logo_url")
  24892. })
  24893. );
  24894. node.parentNode.replaceChild(img, node);
  24895. }
  24896. });
  24897. },
  24898. commands: {
  24899. /**
  24900. * 插入百度应用
  24901. * @command webapp
  24902. * @method execCommand
  24903. * @remind 需要百度APPKey
  24904. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  24905. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  24906. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  24907. * @example
  24908. * ```javascript
  24909. * //editor是编辑器实例
  24910. * //在编辑器里插入一个“植物大战僵尸”的APP
  24911. * editor.execCommand( 'webapp' , {
  24912. * title: '植物大战僵尸',
  24913. * width: 560,
  24914. * height: 465,
  24915. * logo: '应用展示的图片',
  24916. * url: '百度应用的地址'
  24917. * } );
  24918. * ```
  24919. */
  24920. webapp: {
  24921. execCommand: function(cmd, obj) {
  24922. var me = this,
  24923. str = createInsertStr(
  24924. utils.extend(obj, {
  24925. align: "none"
  24926. }),
  24927. false
  24928. );
  24929. me.execCommand("inserthtml", str);
  24930. },
  24931. queryCommandState: function() {
  24932. var me = this,
  24933. img = me.selection.getRange().getClosedNode(),
  24934. flag = img && img.className == "edui-faked-webapp";
  24935. return flag ? 1 : 0;
  24936. }
  24937. }
  24938. }
  24939. };
  24940. });
  24941. // plugins/template.js
  24942. ///import core
  24943. ///import plugins\inserthtml.js
  24944. ///import plugins\cleardoc.js
  24945. ///commands 模板
  24946. ///commandsName template
  24947. ///commandsTitle 模板
  24948. ///commandsDialog dialogs\template
  24949. UE.plugins["template"] = function() {
  24950. UE.commands["template"] = {
  24951. execCommand: function(cmd, obj) {
  24952. obj.html && this.execCommand("inserthtml", obj.html);
  24953. }
  24954. };
  24955. this.addListener("click", function(type, evt) {
  24956. var el = evt.target || evt.srcElement,
  24957. range = this.selection.getRange();
  24958. var tnode = domUtils.findParent(
  24959. el,
  24960. function(node) {
  24961. if (node.className && domUtils.hasClass(node, "ue_t")) {
  24962. return node;
  24963. }
  24964. },
  24965. true
  24966. );
  24967. tnode && range.selectNode(tnode).shrinkBoundary().select();
  24968. });
  24969. this.addListener("keydown", function(type, evt) {
  24970. var range = this.selection.getRange();
  24971. if (!range.collapsed) {
  24972. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  24973. var tnode = domUtils.findParent(
  24974. range.startContainer,
  24975. function(node) {
  24976. if (node.className && domUtils.hasClass(node, "ue_t")) {
  24977. return node;
  24978. }
  24979. },
  24980. true
  24981. );
  24982. if (tnode) {
  24983. domUtils.removeClasses(tnode, ["ue_t"]);
  24984. }
  24985. }
  24986. }
  24987. });
  24988. };
  24989. // plugins/music.js
  24990. /**
  24991. * 插入音乐命令
  24992. * @file
  24993. */
  24994. UE.plugin.register("music", function() {
  24995. var me = this;
  24996. function creatInsertStr(url, width, height, align, cssfloat, toEmbed) {
  24997. return !toEmbed
  24998. ? "<img " +
  24999. (align && !cssfloat ? 'align="' + align + '"' : "") +
  25000. (cssfloat ? 'style="float:' + cssfloat + '"' : "") +
  25001. ' width="' +
  25002. width +
  25003. '" height="' +
  25004. height +
  25005. '" _url="' +
  25006. url +
  25007. '" class="edui-faked-music"' +
  25008. ' src="' +
  25009. me.options.langPath +
  25010. me.options.lang +
  25011. '/images/music.png" />'
  25012. : '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  25013. ' src="' +
  25014. url +
  25015. '" width="' +
  25016. width +
  25017. '" height="' +
  25018. height +
  25019. '" ' +
  25020. (align && !cssfloat ? 'align="' + align + '"' : "") +
  25021. (cssfloat ? 'style="float:' + cssfloat + '"' : "") +
  25022. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  25023. }
  25024. return {
  25025. outputRule: function(root) {
  25026. utils.each(root.getNodesByTagName("img"), function(node) {
  25027. var html;
  25028. if (node.getAttr("class") == "edui-faked-music") {
  25029. var cssfloat = node.getStyle("float");
  25030. var align = node.getAttr("align");
  25031. html = creatInsertStr(
  25032. node.getAttr("_url"),
  25033. node.getAttr("width"),
  25034. node.getAttr("height"),
  25035. align,
  25036. cssfloat,
  25037. true
  25038. );
  25039. var embed = UE.uNode.createElement(html);
  25040. node.parentNode.replaceChild(embed, node);
  25041. }
  25042. });
  25043. },
  25044. inputRule: function(root) {
  25045. utils.each(root.getNodesByTagName("embed"), function(node) {
  25046. if (node.getAttr("class") == "edui-faked-music") {
  25047. var cssfloat = node.getStyle("float");
  25048. var align = node.getAttr("align");
  25049. html = creatInsertStr(
  25050. node.getAttr("src"),
  25051. node.getAttr("width"),
  25052. node.getAttr("height"),
  25053. align,
  25054. cssfloat,
  25055. false
  25056. );
  25057. var img = UE.uNode.createElement(html);
  25058. node.parentNode.replaceChild(img, node);
  25059. }
  25060. });
  25061. },
  25062. commands: {
  25063. /**
  25064. * 插入音乐
  25065. * @command music
  25066. * @method execCommand
  25067. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  25068. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  25069. * @example
  25070. * ```javascript
  25071. * //editor是编辑器实例
  25072. * //在编辑器里插入一个“植物大战僵尸”的APP
  25073. * editor.execCommand( 'music' , {
  25074. * width: 400,
  25075. * height: 95,
  25076. * align: "center",
  25077. * url: "音乐地址"
  25078. * } );
  25079. * ```
  25080. */
  25081. music: {
  25082. execCommand: function(cmd, musicObj) {
  25083. var me = this,
  25084. str = creatInsertStr(
  25085. musicObj.url,
  25086. musicObj.width || 400,
  25087. musicObj.height || 95,
  25088. "none",
  25089. false
  25090. );
  25091. me.execCommand("inserthtml", str);
  25092. },
  25093. queryCommandState: function() {
  25094. var me = this,
  25095. img = me.selection.getRange().getClosedNode(),
  25096. flag = img && img.className == "edui-faked-music";
  25097. return flag ? 1 : 0;
  25098. }
  25099. }
  25100. }
  25101. };
  25102. });
  25103. // plugins/autoupload.js
  25104. /**
  25105. * @description
  25106. * 1.拖放文件到编辑区域,自动上传并插入到选区
  25107. * 2.插入粘贴板的图片,自动上传并插入到选区
  25108. * @author Jinqn
  25109. * @date 2013-10-14
  25110. */
  25111. UE.plugin.register("autoupload", function() {
  25112. function sendAndInsertFile(file, editor) {
  25113. var me = editor;
  25114. //模拟数据
  25115. var fieldName,
  25116. urlPrefix,
  25117. maxSize,
  25118. allowFiles,
  25119. actionUrl,
  25120. loadingHtml,
  25121. errorHandler,
  25122. successHandler,
  25123. filetype = /image\/\w+/i.test(file.type) ? "image" : "file",
  25124. loadingId = "loading_" + (+new Date()).toString(36);
  25125. fieldName = me.getOpt(filetype + "FieldName");
  25126. urlPrefix = me.getOpt(filetype + "UrlPrefix");
  25127. maxSize = me.getOpt(filetype + "MaxSize");
  25128. allowFiles = me.getOpt(filetype + "AllowFiles");
  25129. actionUrl = me.getActionUrl(me.getOpt(filetype + "ActionName"));
  25130. errorHandler = function(title) {
  25131. var loader = me.document.getElementById(loadingId);
  25132. loader && domUtils.remove(loader);
  25133. me.fireEvent("showmessage", {
  25134. id: loadingId,
  25135. content: title,
  25136. type: "error",
  25137. timeout: 4000
  25138. });
  25139. };
  25140. if (filetype == "image") {
  25141. loadingHtml =
  25142. '<img class="loadingclass" id="' +
  25143. loadingId +
  25144. '" src="' +
  25145. me.options.themePath +
  25146. me.options.theme +
  25147. '/images/spacer.gif">';
  25148. successHandler = function(data) {
  25149. var link = urlPrefix + data.url,
  25150. loader = me.document.getElementById(loadingId);
  25151. if (loader) {
  25152. domUtils.removeClasses(loader, "loadingclass");
  25153. loader.setAttribute("src", link);
  25154. loader.setAttribute("_src", link);
  25155. loader.setAttribute("alt", data.original || "");
  25156. loader.removeAttribute("id");
  25157. me.trigger("contentchange", loader);
  25158. }
  25159. };
  25160. } else {
  25161. loadingHtml =
  25162. "<p>" +
  25163. '<img class="loadingclass" id="' +
  25164. loadingId +
  25165. '" src="' +
  25166. me.options.themePath +
  25167. me.options.theme +
  25168. '/images/spacer.gif">' +
  25169. "</p>";
  25170. successHandler = function(data) {
  25171. var link = urlPrefix + data.url,
  25172. loader = me.document.getElementById(loadingId);
  25173. var rng = me.selection.getRange(),
  25174. bk = rng.createBookmark();
  25175. rng.selectNode(loader).select();
  25176. me.execCommand("insertfile", { url: link });
  25177. rng.moveToBookmark(bk).select();
  25178. };
  25179. }
  25180. /* 插入loading的占位符 */
  25181. me.execCommand("inserthtml", loadingHtml);
  25182. /* 判断后端配置是否没有加载成功 */
  25183. if (!me.getOpt(filetype + "ActionName")) {
  25184. errorHandler(me.getLang("autoupload.errorLoadConfig"));
  25185. return;
  25186. }
  25187. /* 判断文件大小是否超出限制 */
  25188. if (file.size > maxSize) {
  25189. errorHandler(me.getLang("autoupload.exceedSizeError"));
  25190. return;
  25191. }
  25192. /* 判断文件格式是否超出允许 */
  25193. var fileext = file.name ? file.name.substr(file.name.lastIndexOf(".")) : "";
  25194. if (
  25195. (fileext && filetype != "image") ||
  25196. (allowFiles &&
  25197. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") == -1)
  25198. ) {
  25199. errorHandler(me.getLang("autoupload.exceedTypeError"));
  25200. return;
  25201. }
  25202. try {
  25203. var uploadAction = editor.getOpt("uploadService")(this, editor).Upload();
  25204. var Fast = editor.getOpt("uploadService")(this, editor).Fast();
  25205. uploadAction.api.send(file, function (data) {
  25206. var url = Fast.api.cdnurl(data.url, true);
  25207. successHandler({
  25208. "state": "SUCCESS",
  25209. "url": url,
  25210. "title": file.name,
  25211. "original": file.name,
  25212. "type": fileext,
  25213. "size": file['size']
  25214. });
  25215. });
  25216. } catch (er) {
  25217. errorHandler(me.getLang('autoupload.loadError'));
  25218. }
  25219. // /* 创建Ajax并提交 */
  25220. // var xhr = new XMLHttpRequest(),
  25221. // fd = new FormData(),
  25222. // params = utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  25223. // url = utils.formatUrl(
  25224. // actionUrl + (actionUrl.indexOf("?") == -1 ? "?" : "&") + params
  25225. // );
  25226. // fd.append(
  25227. // fieldName,
  25228. // file,
  25229. // file.name || "blob." + file.type.substr("image/".length)
  25230. // );
  25231. // fd.append("type", "ajax");
  25232. // xhr.open("post", url, true);
  25233. // xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  25234. // xhr.addEventListener("load", function(e) {
  25235. // try {
  25236. // var json = new Function("return " + utils.trim(e.target.response))();
  25237. // if (json.state == "SUCCESS" && json.url) {
  25238. // successHandler(json);
  25239. // } else {
  25240. // errorHandler(json.state);
  25241. // }
  25242. // } catch (er) {
  25243. // errorHandler(me.getLang("autoupload.loadError"));
  25244. // }
  25245. // });
  25246. // xhr.send(fd);
  25247. }
  25248. function getPasteImage(e) {
  25249. return e.clipboardData &&
  25250. e.clipboardData.items &&
  25251. e.clipboardData.items.length == 1 &&
  25252. /^image\//.test(e.clipboardData.items[0].type)
  25253. ? e.clipboardData.items
  25254. : null;
  25255. }
  25256. function getDropImage(e) {
  25257. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  25258. }
  25259. return {
  25260. outputRule: function(root) {
  25261. utils.each(root.getNodesByTagName("img"), function(n) {
  25262. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  25263. n.parentNode.removeChild(n);
  25264. }
  25265. });
  25266. utils.each(root.getNodesByTagName("p"), function(n) {
  25267. if (/\bloadpara\b/.test(n.getAttr("class"))) {
  25268. n.parentNode.removeChild(n);
  25269. }
  25270. });
  25271. },
  25272. bindEvents: {
  25273. defaultOptions: {
  25274. //默认间隔时间
  25275. enableDragUpload: true,
  25276. enablePasteUpload: true
  25277. },
  25278. //插入粘贴板的图片,拖放插入图片
  25279. ready: function(e) {
  25280. var me = this;
  25281. if (window.FormData && window.FileReader) {
  25282. var handler = function(e) {
  25283. var hasImg = false,
  25284. items;
  25285. //获取粘贴板文件列表或者拖放文件列表
  25286. items = e.type == "paste" ? getPasteImage(e) : getDropImage(e);
  25287. if (items) {
  25288. var len = items.length,
  25289. file;
  25290. while (len--) {
  25291. file = items[len];
  25292. if (file.getAsFile) file = file.getAsFile();
  25293. if (file && file.size > 0) {
  25294. sendAndInsertFile(file, me);
  25295. hasImg = true;
  25296. }
  25297. }
  25298. hasImg && e.preventDefault();
  25299. }
  25300. };
  25301. if (me.getOpt("enablePasteUpload") !== false) {
  25302. domUtils.on(me.body, "paste ", handler);
  25303. }
  25304. if (me.getOpt("enableDragUpload") !== false) {
  25305. domUtils.on(me.body, "drop", handler);
  25306. //取消拖放图片时出现的文字光标位置提示
  25307. domUtils.on(me.body, "dragover", function(e) {
  25308. if (e.dataTransfer.types[0] == "Files") {
  25309. e.preventDefault();
  25310. }
  25311. });
  25312. } else {
  25313. if (browser.gecko) {
  25314. domUtils.on(me.body, "drop", function(e) {
  25315. if (getDropImage(e)) {
  25316. e.preventDefault();
  25317. }
  25318. });
  25319. }
  25320. }
  25321. //设置loading的样式
  25322. utils.cssRule(
  25323. "loading",
  25324. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  25325. this.options.themePath +
  25326. this.options.theme +
  25327. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n" +
  25328. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  25329. this.options.themePath +
  25330. this.options.theme +
  25331. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  25332. "}",
  25333. this.document
  25334. );
  25335. }
  25336. }
  25337. }
  25338. };
  25339. });
  25340. // plugins/autosave.js
  25341. UE.plugin.register("autosave", function() {
  25342. var me = this,
  25343. //无限循环保护
  25344. lastSaveTime = new Date(),
  25345. //最小保存间隔时间
  25346. MIN_TIME = 20,
  25347. //auto save key
  25348. saveKey = null;
  25349. function save(editor) {
  25350. var saveData;
  25351. if (new Date() - lastSaveTime < MIN_TIME) {
  25352. return;
  25353. }
  25354. if (!editor.hasContents()) {
  25355. //这里不能调用命令来删除, 会造成事件死循环
  25356. saveKey && me.removePreferences(saveKey);
  25357. return;
  25358. }
  25359. lastSaveTime = new Date();
  25360. editor._saveFlag = null;
  25361. saveData = me.body.innerHTML;
  25362. if (
  25363. editor.fireEvent("beforeautosave", {
  25364. content: saveData
  25365. }) === false
  25366. ) {
  25367. return;
  25368. }
  25369. me.setPreferences(saveKey, saveData);
  25370. editor.fireEvent("afterautosave", {
  25371. content: saveData
  25372. });
  25373. }
  25374. return {
  25375. defaultOptions: {
  25376. //默认间隔时间
  25377. saveInterval: 500,
  25378. enableAutoSave: true
  25379. },
  25380. bindEvents: {
  25381. ready: function() {
  25382. var _suffix = "-drafts-data",
  25383. key = null;
  25384. if (me.key) {
  25385. key = me.key + _suffix;
  25386. } else {
  25387. key = (me.container.parentNode.id || "ue-common") + _suffix;
  25388. }
  25389. //页面地址+编辑器ID 保持唯一
  25390. saveKey =
  25391. (location.protocol + location.host + location.pathname).replace(
  25392. /[.:\/]/g,
  25393. "_"
  25394. ) + key;
  25395. },
  25396. contentchange: function() {
  25397. if (!me.getOpt("enableAutoSave")) {
  25398. return;
  25399. }
  25400. if (!saveKey) {
  25401. return;
  25402. }
  25403. if (me._saveFlag) {
  25404. window.clearTimeout(me._saveFlag);
  25405. }
  25406. if (me.options.saveInterval > 0) {
  25407. me._saveFlag = window.setTimeout(function() {
  25408. save(me);
  25409. }, me.options.saveInterval);
  25410. } else {
  25411. save(me);
  25412. }
  25413. }
  25414. },
  25415. commands: {
  25416. clearlocaldata: {
  25417. execCommand: function(cmd, name) {
  25418. if (saveKey && me.getPreferences(saveKey)) {
  25419. me.removePreferences(saveKey);
  25420. }
  25421. },
  25422. notNeedUndo: true,
  25423. ignoreContentChange: true
  25424. },
  25425. getlocaldata: {
  25426. execCommand: function(cmd, name) {
  25427. return saveKey ? me.getPreferences(saveKey) || "" : "";
  25428. },
  25429. notNeedUndo: true,
  25430. ignoreContentChange: true
  25431. },
  25432. drafts: {
  25433. execCommand: function(cmd, name) {
  25434. if (saveKey) {
  25435. window.setTimeout(function(){
  25436. me.body.innerHTML =
  25437. me.getPreferences(saveKey) || "<p>" + domUtils.fillHtml + "</p>";
  25438. // me.focus(true);
  25439. }, 0);
  25440. }
  25441. },
  25442. queryCommandState: function() {
  25443. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1;
  25444. },
  25445. notNeedUndo: true,
  25446. ignoreContentChange: true
  25447. }
  25448. }
  25449. };
  25450. });
  25451. // plugins/charts.js
  25452. UE.plugin.register("charts", function() {
  25453. var me = this;
  25454. return {
  25455. bindEvents: {
  25456. chartserror: function() {}
  25457. },
  25458. commands: {
  25459. charts: {
  25460. execCommand: function(cmd, data) {
  25461. var tableNode = domUtils.findParentByTagName(
  25462. this.selection.getRange().startContainer,
  25463. "table",
  25464. true
  25465. ),
  25466. flagText = [],
  25467. config = {};
  25468. if (!tableNode) {
  25469. return false;
  25470. }
  25471. if (!validData(tableNode)) {
  25472. me.fireEvent("chartserror");
  25473. return false;
  25474. }
  25475. config.title = data.title || "";
  25476. config.subTitle = data.subTitle || "";
  25477. config.xTitle = data.xTitle || "";
  25478. config.yTitle = data.yTitle || "";
  25479. config.suffix = data.suffix || "";
  25480. config.tip = data.tip || "";
  25481. //数据对齐方式
  25482. config.dataFormat = data.tableDataFormat || "";
  25483. //图表类型
  25484. config.chartType = data.chartType || 0;
  25485. for (var key in config) {
  25486. if (!config.hasOwnProperty(key)) {
  25487. continue;
  25488. }
  25489. flagText.push(key + ":" + config[key]);
  25490. }
  25491. tableNode.setAttribute("data-chart", flagText.join(";"));
  25492. domUtils.addClass(tableNode, "edui-charts-table");
  25493. },
  25494. queryCommandState: function(cmd, name) {
  25495. var tableNode = domUtils.findParentByTagName(
  25496. this.selection.getRange().startContainer,
  25497. "table",
  25498. true
  25499. );
  25500. return tableNode && validData(tableNode) ? 0 : -1;
  25501. }
  25502. }
  25503. },
  25504. inputRule: function(root) {
  25505. utils.each(root.getNodesByTagName("table"), function(tableNode) {
  25506. if (tableNode.getAttr("data-chart") !== undefined) {
  25507. tableNode.setAttr("style");
  25508. }
  25509. });
  25510. },
  25511. outputRule: function(root) {
  25512. utils.each(root.getNodesByTagName("table"), function(tableNode) {
  25513. if (tableNode.getAttr("data-chart") !== undefined) {
  25514. tableNode.setAttr("style", "display: none;");
  25515. }
  25516. });
  25517. }
  25518. };
  25519. function validData(table) {
  25520. var firstRows = null,
  25521. cellCount = 0;
  25522. //行数不够
  25523. if (table.rows.length < 2) {
  25524. return false;
  25525. }
  25526. //列数不够
  25527. if (table.rows[0].cells.length < 2) {
  25528. return false;
  25529. }
  25530. //第一行所有cell必须是th
  25531. firstRows = table.rows[0].cells;
  25532. cellCount = firstRows.length;
  25533. for (var i = 0, cell; (cell = firstRows[i]); i++) {
  25534. if (cell.tagName.toLowerCase() !== "th") {
  25535. return false;
  25536. }
  25537. }
  25538. for (var i = 1, row; (row = table.rows[i]); i++) {
  25539. //每行单元格数不匹配, 返回false
  25540. if (row.cells.length != cellCount) {
  25541. return false;
  25542. }
  25543. //第一列不是th也返回false
  25544. if (row.cells[0].tagName.toLowerCase() !== "th") {
  25545. return false;
  25546. }
  25547. for (var j = 1, cell; (cell = row.cells[j]); j++) {
  25548. var value = utils.trim(cell.innerText || cell.textContent || "");
  25549. value = value
  25550. .replace(new RegExp(UE.dom.domUtils.fillChar, "g"), "")
  25551. .replace(/^\s+|\s+$/g, "");
  25552. //必须是数字
  25553. if (!/^\d*\.?\d+$/.test(value)) {
  25554. return false;
  25555. }
  25556. }
  25557. }
  25558. return true;
  25559. }
  25560. });
  25561. // plugins/section.js
  25562. /**
  25563. * 目录大纲支持插件
  25564. * @file
  25565. * @since 1.3.0
  25566. */
  25567. UE.plugin.register("section", function() {
  25568. /* 目录节点对象 */
  25569. function Section(option) {
  25570. this.tag = "";
  25571. (this.level = -1), (this.dom = null);
  25572. this.nextSection = null;
  25573. this.previousSection = null;
  25574. this.parentSection = null;
  25575. this.startAddress = [];
  25576. this.endAddress = [];
  25577. this.children = [];
  25578. }
  25579. function getSection(option) {
  25580. var section = new Section();
  25581. return utils.extend(section, option);
  25582. }
  25583. function getNodeFromAddress(startAddress, root) {
  25584. var current = root;
  25585. for (var i = 0; i < startAddress.length; i++) {
  25586. if (!current.childNodes) return null;
  25587. current = current.childNodes[startAddress[i]];
  25588. }
  25589. return current;
  25590. }
  25591. var me = this;
  25592. return {
  25593. bindMultiEvents: {
  25594. type: "aftersetcontent afterscencerestore",
  25595. handler: function() {
  25596. me.fireEvent("updateSections");
  25597. }
  25598. },
  25599. bindEvents: {
  25600. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  25601. ready: function() {
  25602. me.fireEvent("updateSections");
  25603. domUtils.on(me.body, "drop paste", function() {
  25604. me.fireEvent("updateSections");
  25605. });
  25606. },
  25607. /* 执行paragraph命令之后 */
  25608. afterexeccommand: function(type, cmd) {
  25609. if (cmd == "paragraph") {
  25610. me.fireEvent("updateSections");
  25611. }
  25612. },
  25613. /* 部分键盘操作,触发updateSections事件 */
  25614. keyup: function(type, e) {
  25615. var me = this,
  25616. range = me.selection.getRange();
  25617. if (range.collapsed != true) {
  25618. me.fireEvent("updateSections");
  25619. } else {
  25620. var keyCode = e.keyCode || e.which;
  25621. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  25622. me.fireEvent("updateSections");
  25623. }
  25624. }
  25625. }
  25626. },
  25627. commands: {
  25628. getsections: {
  25629. execCommand: function(cmd, levels) {
  25630. var levelFn = levels || ["h1", "h2", "h3", "h4", "h5", "h6"];
  25631. for (var i = 0; i < levelFn.length; i++) {
  25632. if (typeof levelFn[i] == "string") {
  25633. levelFn[i] = (function(fn) {
  25634. return function(node) {
  25635. return node.tagName == fn.toUpperCase();
  25636. };
  25637. })(levelFn[i]);
  25638. } else if (typeof levelFn[i] != "function") {
  25639. levelFn[i] = function(node) {
  25640. return null;
  25641. };
  25642. }
  25643. }
  25644. function getSectionLevel(node) {
  25645. for (var i = 0; i < levelFn.length; i++) {
  25646. if (levelFn[i](node)) return i;
  25647. }
  25648. return -1;
  25649. }
  25650. var me = this,
  25651. Directory = getSection({ level: -1, title: "root" }),
  25652. previous = Directory;
  25653. function traversal(node, Directory) {
  25654. var level,
  25655. tmpSection = null,
  25656. parent,
  25657. child,
  25658. children = node.childNodes;
  25659. for (var i = 0, len = children.length; i < len; i++) {
  25660. child = children[i];
  25661. level = getSectionLevel(child);
  25662. if (level >= 0) {
  25663. var address = me.selection
  25664. .getRange()
  25665. .selectNode(child)
  25666. .createAddress(true).startAddress,
  25667. current = getSection({
  25668. tag: child.tagName,
  25669. title: child.innerText || child.textContent || "",
  25670. level: level,
  25671. dom: child,
  25672. startAddress: utils.clone(address, []),
  25673. endAddress: utils.clone(address, []),
  25674. children: []
  25675. });
  25676. previous.nextSection = current;
  25677. current.previousSection = previous;
  25678. parent = previous;
  25679. while (level <= parent.level) {
  25680. parent = parent.parentSection;
  25681. }
  25682. current.parentSection = parent;
  25683. parent.children.push(current);
  25684. tmpSection = previous = current;
  25685. } else {
  25686. child.nodeType === 1 && traversal(child, Directory);
  25687. tmpSection &&
  25688. tmpSection.endAddress[tmpSection.endAddress.length - 1]++;
  25689. }
  25690. }
  25691. }
  25692. traversal(me.body, Directory);
  25693. return Directory;
  25694. },
  25695. notNeedUndo: true
  25696. },
  25697. movesection: {
  25698. execCommand: function(cmd, sourceSection, targetSection, isAfter) {
  25699. var me = this,
  25700. targetAddress,
  25701. target;
  25702. if (!sourceSection || !targetSection || targetSection.level == -1)
  25703. return;
  25704. targetAddress = isAfter
  25705. ? targetSection.endAddress
  25706. : targetSection.startAddress;
  25707. target = getNodeFromAddress(targetAddress, me.body);
  25708. /* 判断目标地址是否被源章节包含 */
  25709. if (
  25710. !targetAddress ||
  25711. !target ||
  25712. isContainsAddress(
  25713. sourceSection.startAddress,
  25714. sourceSection.endAddress,
  25715. targetAddress
  25716. )
  25717. )
  25718. return;
  25719. var startNode = getNodeFromAddress(
  25720. sourceSection.startAddress,
  25721. me.body
  25722. ),
  25723. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  25724. current,
  25725. nextNode;
  25726. if (isAfter) {
  25727. current = endNode;
  25728. while (
  25729. current &&
  25730. !(
  25731. domUtils.getPosition(startNode, current) &
  25732. domUtils.POSITION_FOLLOWING
  25733. )
  25734. ) {
  25735. nextNode = current.previousSibling;
  25736. domUtils.insertAfter(target, current);
  25737. if (current == startNode) break;
  25738. current = nextNode;
  25739. }
  25740. } else {
  25741. current = startNode;
  25742. while (
  25743. current &&
  25744. !(
  25745. domUtils.getPosition(current, endNode) &
  25746. domUtils.POSITION_FOLLOWING
  25747. )
  25748. ) {
  25749. nextNode = current.nextSibling;
  25750. target.parentNode.insertBefore(current, target);
  25751. if (current == endNode) break;
  25752. current = nextNode;
  25753. }
  25754. }
  25755. me.fireEvent("updateSections");
  25756. /* 获取地址的包含关系 */
  25757. function isContainsAddress(startAddress, endAddress, addressTarget) {
  25758. var isAfterStartAddress = false,
  25759. isBeforeEndAddress = false;
  25760. for (var i = 0; i < startAddress.length; i++) {
  25761. if (i >= addressTarget.length) break;
  25762. if (addressTarget[i] > startAddress[i]) {
  25763. isAfterStartAddress = true;
  25764. break;
  25765. } else if (addressTarget[i] < startAddress[i]) {
  25766. break;
  25767. }
  25768. }
  25769. for (var i = 0; i < endAddress.length; i++) {
  25770. if (i >= addressTarget.length) break;
  25771. if (addressTarget[i] < startAddress[i]) {
  25772. isBeforeEndAddress = true;
  25773. break;
  25774. } else if (addressTarget[i] > startAddress[i]) {
  25775. break;
  25776. }
  25777. }
  25778. return isAfterStartAddress && isBeforeEndAddress;
  25779. }
  25780. }
  25781. },
  25782. deletesection: {
  25783. execCommand: function(cmd, section, keepChildren) {
  25784. var me = this;
  25785. if (!section) return;
  25786. function getNodeFromAddress(startAddress) {
  25787. var current = me.body;
  25788. for (var i = 0; i < startAddress.length; i++) {
  25789. if (!current.childNodes) return null;
  25790. current = current.childNodes[startAddress[i]];
  25791. }
  25792. return current;
  25793. }
  25794. var startNode = getNodeFromAddress(section.startAddress),
  25795. endNode = getNodeFromAddress(section.endAddress),
  25796. current = startNode,
  25797. nextNode;
  25798. if (!keepChildren) {
  25799. while (
  25800. current &&
  25801. domUtils.inDoc(endNode, me.document) &&
  25802. !(
  25803. domUtils.getPosition(current, endNode) &
  25804. domUtils.POSITION_FOLLOWING
  25805. )
  25806. ) {
  25807. nextNode = current.nextSibling;
  25808. domUtils.remove(current);
  25809. current = nextNode;
  25810. }
  25811. } else {
  25812. domUtils.remove(current);
  25813. }
  25814. me.fireEvent("updateSections");
  25815. }
  25816. },
  25817. selectsection: {
  25818. execCommand: function(cmd, section) {
  25819. if (!section && !section.dom) return false;
  25820. var me = this,
  25821. range = me.selection.getRange(),
  25822. address = {
  25823. startAddress: utils.clone(section.startAddress, []),
  25824. endAddress: utils.clone(section.endAddress, [])
  25825. };
  25826. address.endAddress[address.endAddress.length - 1]++;
  25827. range.moveToAddress(address).select().scrollToView();
  25828. return true;
  25829. },
  25830. notNeedUndo: true
  25831. },
  25832. scrolltosection: {
  25833. execCommand: function(cmd, section) {
  25834. if (!section && !section.dom) return false;
  25835. var me = this,
  25836. range = me.selection.getRange(),
  25837. address = {
  25838. startAddress: section.startAddress,
  25839. endAddress: section.endAddress
  25840. };
  25841. address.endAddress[address.endAddress.length - 1]++;
  25842. range.moveToAddress(address).scrollToView();
  25843. return true;
  25844. },
  25845. notNeedUndo: true
  25846. }
  25847. }
  25848. };
  25849. });
  25850. // plugins/simpleupload.js
  25851. /**
  25852. * @description
  25853. * 简单上传:点击按钮,直接选择文件上传
  25854. * @author Jinqn
  25855. * @date 2014-03-31
  25856. */
  25857. UE.plugin.register("simpleupload", function() {
  25858. var me = this,
  25859. isLoaded = false,
  25860. containerBtn;
  25861. function initUploadBtn() {
  25862. var w = containerBtn.offsetWidth || 20,
  25863. h = containerBtn.offsetHeight || 20,
  25864. btnIframe = document.createElement("iframe"),
  25865. btnStyle =
  25866. "display:block;width:" +
  25867. w +
  25868. "px;height:" +
  25869. h +
  25870. "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;";
  25871. domUtils.on(btnIframe, "load", function() {
  25872. var timestrap = (+new Date()).toString(36),
  25873. wrapper,
  25874. btnIframeDoc,
  25875. btnIframeBody;
  25876. btnIframeDoc =
  25877. btnIframe.contentDocument || btnIframe.contentWindow.document;
  25878. btnIframeBody = btnIframeDoc.body;
  25879. wrapper = btnIframeDoc.createElement("div");
  25880. wrapper.innerHTML =
  25881. '<form id="edui_form_' +
  25882. timestrap +
  25883. '" target="edui_iframe_' +
  25884. timestrap +
  25885. '" method="POST" enctype="multipart/form-data" action="' +
  25886. me.getOpt("serverUrl") +
  25887. '" ' +
  25888. 'style="' +
  25889. btnStyle +
  25890. '">' +
  25891. '<input id="edui_input_' +
  25892. timestrap +
  25893. '" type="file" accept="image/*" name="' +
  25894. me.options.imageFieldName +
  25895. '" ' +
  25896. 'style="' +
  25897. btnStyle +
  25898. '">' +
  25899. "</form>" +
  25900. '<iframe id="edui_iframe_' +
  25901. timestrap +
  25902. '" name="edui_iframe_' +
  25903. timestrap +
  25904. '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>';
  25905. wrapper.className = "edui-" + me.options.theme;
  25906. wrapper.id = me.ui.id + "_iframeupload";
  25907. btnIframeBody.style.cssText = btnStyle;
  25908. btnIframeBody.style.width = w + "px";
  25909. btnIframeBody.style.height = h + "px";
  25910. btnIframeBody.appendChild(wrapper);
  25911. if (btnIframeBody.parentNode) {
  25912. btnIframeBody.parentNode.style.width = w + "px";
  25913. btnIframeBody.parentNode.style.height = w + "px";
  25914. }
  25915. var form = btnIframeDoc.getElementById("edui_form_" + timestrap);
  25916. var input = btnIframeDoc.getElementById("edui_input_" + timestrap);
  25917. var iframe = btnIframeDoc.getElementById("edui_iframe_" + timestrap);
  25918. domUtils.on(input, "change", function() {
  25919. if (!input.value) return;
  25920. var loadingId = "loading_" + (+new Date()).toString(36);
  25921. var params =
  25922. utils.serializeParam(me.queryCommandValue("serverparam")) || "";
  25923. var imageActionUrl = me.getActionUrl(me.getOpt("imageActionName"));
  25924. var allowFiles = me.getOpt("imageAllowFiles");
  25925. me.focus();
  25926. me.execCommand(
  25927. "inserthtml",
  25928. '<img class="loadingclass" id="' +
  25929. loadingId +
  25930. '" src="' +
  25931. me.options.themePath +
  25932. me.options.theme +
  25933. '/images/spacer.gif">'
  25934. );
  25935. function callback() {
  25936. try {
  25937. var link,
  25938. json,
  25939. loader,
  25940. body = (iframe.contentDocument || iframe.contentWindow.document)
  25941. .body,
  25942. result = body.innerText || body.textContent || "";
  25943. json = new Function("return " + result)();
  25944. link = me.options.imageUrlPrefix + json.url;
  25945. if (json.state == "SUCCESS" && json.url) {
  25946. loader = me.document.getElementById(loadingId);
  25947. domUtils.removeClasses(loader, "loadingclass");
  25948. domUtils.on(loader,'load',function(){
  25949. me.fireEvent('contentchange');
  25950. });
  25951. loader.setAttribute("src", link);
  25952. loader.setAttribute("_src", link);
  25953. loader.setAttribute("alt", json.original || "");
  25954. loader.removeAttribute("id");
  25955. } else {
  25956. showErrorLoader && showErrorLoader(json.state);
  25957. }
  25958. } catch (er) {
  25959. showErrorLoader &&
  25960. showErrorLoader(me.getLang("simpleupload.loadError"));
  25961. }
  25962. form.reset();
  25963. domUtils.un(iframe, "load", callback);
  25964. }
  25965. function showErrorLoader(title) {
  25966. if (loadingId) {
  25967. var loader = me.document.getElementById(loadingId);
  25968. loader && domUtils.remove(loader);
  25969. me.fireEvent("showmessage", {
  25970. id: loadingId,
  25971. content: title,
  25972. type: "error",
  25973. timeout: 4000
  25974. });
  25975. }
  25976. }
  25977. /* 判断后端配置是否没有加载成功 */
  25978. if (!me.getOpt("imageActionName")) {
  25979. errorHandler(me.getLang("autoupload.errorLoadConfig"));
  25980. return;
  25981. }
  25982. // 判断文件格式是否错误
  25983. var filename = input.value,
  25984. fileext = filename ? filename.substr(filename.lastIndexOf(".")) : "";
  25985. if (
  25986. !fileext ||
  25987. (allowFiles &&
  25988. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") ==
  25989. -1)
  25990. ) {
  25991. showErrorLoader(me.getLang("simpleupload.exceedTypeError"));
  25992. return;
  25993. }
  25994. domUtils.on(iframe, "load", callback);
  25995. form.action = utils.formatUrl(
  25996. imageActionUrl +
  25997. (imageActionUrl.indexOf("?") == -1 ? "?" : "&") +
  25998. params
  25999. );
  26000. form.submit();
  26001. });
  26002. var stateTimer;
  26003. me.addListener("selectionchange", function() {
  26004. clearTimeout(stateTimer);
  26005. stateTimer = setTimeout(function() {
  26006. var state = me.queryCommandState("simpleupload");
  26007. if (state == -1) {
  26008. input.disabled = "disabled";
  26009. } else {
  26010. input.disabled = false;
  26011. }
  26012. }, 400);
  26013. });
  26014. isLoaded = true;
  26015. });
  26016. btnIframe.style.cssText = btnStyle;
  26017. containerBtn.appendChild(btnIframe);
  26018. }
  26019. return {
  26020. bindEvents: {
  26021. ready: function() {
  26022. //设置loading的样式
  26023. utils.cssRule(
  26024. "loading",
  26025. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  26026. this.options.themePath +
  26027. this.options.theme +
  26028. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n" +
  26029. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  26030. this.options.themePath +
  26031. this.options.theme +
  26032. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  26033. "}",
  26034. this.document
  26035. );
  26036. },
  26037. /* 初始化简单上传按钮 */
  26038. simpleuploadbtnready: function(type, container) {
  26039. containerBtn = container;
  26040. me.afterConfigReady(initUploadBtn);
  26041. }
  26042. },
  26043. outputRule: function(root) {
  26044. utils.each(root.getNodesByTagName("img"), function(n) {
  26045. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  26046. n.parentNode.removeChild(n);
  26047. }
  26048. });
  26049. },
  26050. commands: {
  26051. simpleupload: {
  26052. queryCommandState: function() {
  26053. return isLoaded ? 0 : -1;
  26054. }
  26055. }
  26056. }
  26057. };
  26058. });
  26059. // plugins/serverparam.js
  26060. /**
  26061. * 服务器提交的额外参数列表设置插件
  26062. * @file
  26063. * @since 1.2.6.1
  26064. */
  26065. UE.plugin.register("serverparam", function() {
  26066. var me = this,
  26067. serverParam = {};
  26068. return {
  26069. commands: {
  26070. /**
  26071. * 修改服务器提交的额外参数列表,清除所有项
  26072. * @command serverparam
  26073. * @method execCommand
  26074. * @param { String } cmd 命令字符串
  26075. * @example
  26076. * ```javascript
  26077. * editor.execCommand('serverparam');
  26078. * editor.queryCommandValue('serverparam'); //返回空
  26079. * ```
  26080. */
  26081. /**
  26082. * 修改服务器提交的额外参数列表,删除指定项
  26083. * @command serverparam
  26084. * @method execCommand
  26085. * @param { String } cmd 命令字符串
  26086. * @param { String } key 要清除的属性
  26087. * @example
  26088. * ```javascript
  26089. * editor.execCommand('serverparam', 'name'); //删除属性name
  26090. * ```
  26091. */
  26092. /**
  26093. * 修改服务器提交的额外参数列表,使用键值添加项
  26094. * @command serverparam
  26095. * @method execCommand
  26096. * @param { String } cmd 命令字符串
  26097. * @param { String } key 要添加的属性
  26098. * @param { String } value 要添加属性的值
  26099. * @example
  26100. * ```javascript
  26101. * editor.execCommand('serverparam', 'name', 'hello');
  26102. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26103. * ```
  26104. */
  26105. /**
  26106. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  26107. * @command serverparam
  26108. * @method execCommand
  26109. * @param { String } cmd 命令字符串
  26110. * @param { Object } key 传入的键值对对象
  26111. * @example
  26112. * ```javascript
  26113. * editor.execCommand('serverparam', {'name': 'hello'});
  26114. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26115. * ```
  26116. */
  26117. /**
  26118. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  26119. * @command serverparam
  26120. * @method execCommand
  26121. * @param { String } cmd 命令字符串
  26122. * @param { Function } key 自定义获取参数的函数
  26123. * @example
  26124. * ```javascript
  26125. * editor.execCommand('serverparam', function(editor){
  26126. * return {'key': 'value'};
  26127. * });
  26128. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  26129. * ```
  26130. */
  26131. /**
  26132. * 获取服务器提交的额外参数列表
  26133. * @command serverparam
  26134. * @method queryCommandValue
  26135. * @param { String } cmd 命令字符串
  26136. * @example
  26137. * ```javascript
  26138. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  26139. * ```
  26140. */
  26141. serverparam: {
  26142. execCommand: function(cmd, key, value) {
  26143. if (key === undefined || key === null) {
  26144. //不传参数,清空列表
  26145. serverParam = {};
  26146. } else if (utils.isString(key)) {
  26147. //传入键值
  26148. if (value === undefined || value === null) {
  26149. delete serverParam[key];
  26150. } else {
  26151. serverParam[key] = value;
  26152. }
  26153. } else if (utils.isObject(key)) {
  26154. //传入对象,覆盖列表项
  26155. utils.extend(serverParam, key, false);
  26156. } else if (utils.isFunction(key)) {
  26157. //传入函数,添加列表项
  26158. utils.extend(serverParam, key(), false);
  26159. }
  26160. },
  26161. queryCommandValue: function() {
  26162. return serverParam || {};
  26163. }
  26164. }
  26165. }
  26166. };
  26167. });
  26168. // plugins/insertfile.js
  26169. /**
  26170. * 插入附件
  26171. */
  26172. UE.plugin.register("insertfile", function() {
  26173. var me = this;
  26174. function getFileIcon(url) {
  26175. var ext = url.substr(url.lastIndexOf(".") + 1).toLowerCase(),
  26176. maps = {
  26177. rar: "icon_rar.gif",
  26178. zip: "icon_rar.gif",
  26179. tar: "icon_rar.gif",
  26180. gz: "icon_rar.gif",
  26181. bz2: "icon_rar.gif",
  26182. doc: "icon_doc.gif",
  26183. docx: "icon_doc.gif",
  26184. pdf: "icon_pdf.gif",
  26185. mp3: "icon_mp3.gif",
  26186. xls: "icon_xls.gif",
  26187. chm: "icon_chm.gif",
  26188. ppt: "icon_ppt.gif",
  26189. pptx: "icon_ppt.gif",
  26190. avi: "icon_mv.gif",
  26191. rmvb: "icon_mv.gif",
  26192. wmv: "icon_mv.gif",
  26193. flv: "icon_mv.gif",
  26194. swf: "icon_mv.gif",
  26195. rm: "icon_mv.gif",
  26196. exe: "icon_exe.gif",
  26197. psd: "icon_psd.gif",
  26198. txt: "icon_txt.gif",
  26199. jpg: "icon_jpg.gif",
  26200. png: "icon_jpg.gif",
  26201. jpeg: "icon_jpg.gif",
  26202. gif: "icon_jpg.gif",
  26203. ico: "icon_jpg.gif",
  26204. bmp: "icon_jpg.gif"
  26205. };
  26206. return maps[ext] ? maps[ext] : maps["txt"];
  26207. }
  26208. return {
  26209. commands: {
  26210. insertfile: {
  26211. execCommand: function(command, filelist) {
  26212. filelist = utils.isArray(filelist) ? filelist : [filelist];
  26213. if (me.fireEvent("beforeinsertfile", filelist) === true) {
  26214. return;
  26215. }
  26216. var i,
  26217. item,
  26218. icon,
  26219. title,
  26220. html = "",
  26221. URL = me.getOpt("UEDITOR_HOME_URL"),
  26222. iconDir =
  26223. URL +
  26224. (URL.substr(URL.length - 1) == "/" ? "" : "/") +
  26225. "dialogs/attachment/fileTypeImages/";
  26226. for (i = 0; i < filelist.length; i++) {
  26227. item = filelist[i];
  26228. icon = iconDir + getFileIcon(item.url);
  26229. title =
  26230. item.title || item.url.substr(item.url.lastIndexOf("/") + 1);
  26231. html +=
  26232. '<p style="line-height: 16px;">' +
  26233. '<img style="vertical-align: middle; margin-right: 2px;" src="' +
  26234. icon +
  26235. '" _src="' +
  26236. icon +
  26237. '" />' +
  26238. '<a style="font-size:12px; color:#0066cc;" href="' +
  26239. item.url +
  26240. '" title="' +
  26241. title +
  26242. '">' +
  26243. title +
  26244. "</a>" +
  26245. "</p>";
  26246. }
  26247. me.execCommand("insertHtml", html);
  26248. me.fireEvent("afterinsertfile", filelist);
  26249. }
  26250. }
  26251. }
  26252. };
  26253. });
  26254. // plugins/xssFilter.js
  26255. /**
  26256. * @file xssFilter.js
  26257. * @desc xss过滤器
  26258. * @author robbenmu
  26259. */
  26260. UE.plugins.xssFilter = function() {
  26261. var config = UEDITOR_CONFIG;
  26262. var whitList = config.whitList;
  26263. function filter(node) {
  26264. var tagName = node.tagName;
  26265. var attrs = node.attrs;
  26266. if (!whitList.hasOwnProperty(tagName)) {
  26267. node.parentNode.removeChild(node);
  26268. return false;
  26269. }
  26270. UE.utils.each(attrs, function (val, key) {
  26271. if (whitList[tagName].indexOf(key) === -1) {
  26272. node.setAttr(key);
  26273. }
  26274. });
  26275. }
  26276. // 添加inserthtml\paste等操作用的过滤规则
  26277. if (whitList && config.xssFilterRules) {
  26278. this.options.filterRules = function () {
  26279. var result = {};
  26280. UE.utils.each(whitList, function(val, key) {
  26281. result[key] = function (node) {
  26282. return filter(node);
  26283. };
  26284. });
  26285. return result;
  26286. }();
  26287. }
  26288. var tagList = [];
  26289. UE.utils.each(whitList, function (val, key) {
  26290. tagList.push(key);
  26291. });
  26292. // 添加input过滤规则
  26293. //
  26294. if (whitList && config.inputXssFilter) {
  26295. this.addInputRule(function (root) {
  26296. root.traversal(function(node) {
  26297. if (node.type !== 'element') {
  26298. return false;
  26299. }
  26300. filter(node);
  26301. });
  26302. });
  26303. }
  26304. // 添加output过滤规则
  26305. //
  26306. if (whitList && config.outputXssFilter) {
  26307. this.addOutputRule(function (root) {
  26308. root.traversal(function(node) {
  26309. if (node.type !== 'element') {
  26310. return false;
  26311. }
  26312. filter(node);
  26313. });
  26314. });
  26315. }
  26316. };
  26317. // ui/ui.js
  26318. var baidu = baidu || {};
  26319. baidu.editor = baidu.editor || {};
  26320. UE.ui = baidu.editor.ui = {};
  26321. // ui/uiutils.js
  26322. (function() {
  26323. var browser = baidu.editor.browser,
  26324. domUtils = baidu.editor.dom.domUtils;
  26325. var magic = "$EDITORUI";
  26326. var root = (window[magic] = {});
  26327. var uidMagic = "ID" + magic;
  26328. var uidCount = 0;
  26329. var uiUtils = (baidu.editor.ui.uiUtils = {
  26330. uid: function(obj) {
  26331. return obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount;
  26332. },
  26333. hook: function(fn, callback) {
  26334. var dg;
  26335. if (fn && fn._callbacks) {
  26336. dg = fn;
  26337. } else {
  26338. dg = function() {
  26339. var q;
  26340. if (fn) {
  26341. q = fn.apply(this, arguments);
  26342. }
  26343. var callbacks = dg._callbacks;
  26344. var k = callbacks.length;
  26345. while (k--) {
  26346. var r = callbacks[k].apply(this, arguments);
  26347. if (q === undefined) {
  26348. q = r;
  26349. }
  26350. }
  26351. return q;
  26352. };
  26353. dg._callbacks = [];
  26354. }
  26355. dg._callbacks.push(callback);
  26356. return dg;
  26357. },
  26358. createElementByHtml: function(html) {
  26359. var el = document.createElement("div");
  26360. el.innerHTML = html;
  26361. el = el.firstChild;
  26362. el.parentNode.removeChild(el);
  26363. return el;
  26364. },
  26365. getViewportElement: function() {
  26366. return browser.ie && browser.quirks
  26367. ? document.body
  26368. : document.documentElement;
  26369. },
  26370. getClientRect: function(element) {
  26371. var bcr;
  26372. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  26373. try {
  26374. bcr = element.getBoundingClientRect();
  26375. } catch (e) {
  26376. bcr = { left: 0, top: 0, height: 0, width: 0 };
  26377. }
  26378. var rect = {
  26379. left: Math.round(bcr.left),
  26380. top: Math.round(bcr.top),
  26381. height: Math.round(bcr.bottom - bcr.top),
  26382. width: Math.round(bcr.right - bcr.left)
  26383. };
  26384. var doc;
  26385. while (
  26386. (doc = element.ownerDocument) !== document &&
  26387. (element = domUtils.getWindow(doc).frameElement)
  26388. ) {
  26389. bcr = element.getBoundingClientRect();
  26390. rect.left += bcr.left;
  26391. rect.top += bcr.top;
  26392. }
  26393. rect.bottom = rect.top + rect.height;
  26394. rect.right = rect.left + rect.width;
  26395. return rect;
  26396. },
  26397. getViewportRect: function() {
  26398. var viewportEl = uiUtils.getViewportElement();
  26399. var width = (window.innerWidth || viewportEl.clientWidth) | 0;
  26400. var height = (window.innerHeight || viewportEl.clientHeight) | 0;
  26401. return {
  26402. left: 0,
  26403. top: 0,
  26404. height: height,
  26405. width: width,
  26406. bottom: height,
  26407. right: width
  26408. };
  26409. },
  26410. setViewportOffset: function(element, offset) {
  26411. var rect;
  26412. var fixedLayer = uiUtils.getFixedLayer();
  26413. if (element.parentNode === fixedLayer) {
  26414. element.style.left = offset.left + "px";
  26415. element.style.top = offset.top + "px";
  26416. } else {
  26417. domUtils.setViewportOffset(element, offset);
  26418. }
  26419. },
  26420. getEventOffset: function(evt) {
  26421. var el = evt.target || evt.srcElement;
  26422. var rect = uiUtils.getClientRect(el);
  26423. var offset = uiUtils.getViewportOffsetByEvent(evt);
  26424. return {
  26425. left: offset.left - rect.left,
  26426. top: offset.top - rect.top
  26427. };
  26428. },
  26429. getViewportOffsetByEvent: function(evt) {
  26430. var el = evt.target || evt.srcElement;
  26431. var frameEl = domUtils.getWindow(el).frameElement;
  26432. var offset = {
  26433. left: evt.clientX,
  26434. top: evt.clientY
  26435. };
  26436. if (frameEl && el.ownerDocument !== document) {
  26437. var rect = uiUtils.getClientRect(frameEl);
  26438. offset.left += rect.left;
  26439. offset.top += rect.top;
  26440. }
  26441. return offset;
  26442. },
  26443. setGlobal: function(id, obj) {
  26444. root[id] = obj;
  26445. return magic + '["' + id + '"]';
  26446. },
  26447. unsetGlobal: function(id) {
  26448. delete root[id];
  26449. },
  26450. copyAttributes: function(tgt, src) {
  26451. var attributes = src.attributes;
  26452. var k = attributes.length;
  26453. while (k--) {
  26454. var attrNode = attributes[k];
  26455. if (
  26456. attrNode.nodeName != "style" &&
  26457. attrNode.nodeName != "class" &&
  26458. (!browser.ie || attrNode.specified)
  26459. ) {
  26460. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
  26461. }
  26462. }
  26463. if (src.className) {
  26464. domUtils.addClass(tgt, src.className);
  26465. }
  26466. if (src.style.cssText) {
  26467. tgt.style.cssText += ";" + src.style.cssText;
  26468. }
  26469. },
  26470. removeStyle: function(el, styleName) {
  26471. if (el.style.removeProperty) {
  26472. el.style.removeProperty(styleName);
  26473. } else if (el.style.removeAttribute) {
  26474. el.style.removeAttribute(styleName);
  26475. } else throw "";
  26476. },
  26477. contains: function(elA, elB) {
  26478. return (
  26479. elA &&
  26480. elB &&
  26481. (elA === elB
  26482. ? false
  26483. : elA.contains
  26484. ? elA.contains(elB)
  26485. : elA.compareDocumentPosition(elB) & 16)
  26486. );
  26487. },
  26488. startDrag: function(evt, callbacks, doc) {
  26489. var doc = doc || document;
  26490. var startX = evt.clientX;
  26491. var startY = evt.clientY;
  26492. function handleMouseMove(evt) {
  26493. var x = evt.clientX - startX;
  26494. var y = evt.clientY - startY;
  26495. callbacks.ondragmove(x, y, evt);
  26496. if (evt.stopPropagation) {
  26497. evt.stopPropagation();
  26498. } else {
  26499. evt.cancelBubble = true;
  26500. }
  26501. }
  26502. if (doc.addEventListener) {
  26503. function handleMouseUp(evt) {
  26504. doc.removeEventListener("mousemove", handleMouseMove, true);
  26505. doc.removeEventListener("mouseup", handleMouseUp, true);
  26506. window.removeEventListener("mouseup", handleMouseUp, true);
  26507. callbacks.ondragstop();
  26508. }
  26509. doc.addEventListener("mousemove", handleMouseMove, true);
  26510. doc.addEventListener("mouseup", handleMouseUp, true);
  26511. window.addEventListener("mouseup", handleMouseUp, true);
  26512. evt.preventDefault();
  26513. } else {
  26514. var elm = evt.srcElement;
  26515. elm.setCapture();
  26516. function releaseCaptrue() {
  26517. elm.releaseCapture();
  26518. elm.detachEvent("onmousemove", handleMouseMove);
  26519. elm.detachEvent("onmouseup", releaseCaptrue);
  26520. elm.detachEvent("onlosecaptrue", releaseCaptrue);
  26521. callbacks.ondragstop();
  26522. }
  26523. elm.attachEvent("onmousemove", handleMouseMove);
  26524. elm.attachEvent("onmouseup", releaseCaptrue);
  26525. elm.attachEvent("onlosecaptrue", releaseCaptrue);
  26526. evt.returnValue = false;
  26527. }
  26528. callbacks.ondragstart();
  26529. },
  26530. getFixedLayer: function() {
  26531. var layer = document.getElementById("edui_fixedlayer");
  26532. if (layer == null) {
  26533. layer = document.createElement("div");
  26534. layer.id = "edui_fixedlayer";
  26535. document.body.appendChild(layer);
  26536. if (browser.ie && browser.version <= 8) {
  26537. layer.style.position = "absolute";
  26538. bindFixedLayer();
  26539. setTimeout(updateFixedOffset);
  26540. } else {
  26541. layer.style.position = "fixed";
  26542. }
  26543. layer.style.left = "0";
  26544. layer.style.top = "0";
  26545. layer.style.width = "0";
  26546. layer.style.height = "0";
  26547. }
  26548. return layer;
  26549. },
  26550. makeUnselectable: function(element) {
  26551. if (browser.opera || (browser.ie && browser.version < 9)) {
  26552. element.unselectable = "on";
  26553. if (element.hasChildNodes()) {
  26554. for (var i = 0; i < element.childNodes.length; i++) {
  26555. if (element.childNodes[i].nodeType == 1) {
  26556. uiUtils.makeUnselectable(element.childNodes[i]);
  26557. }
  26558. }
  26559. }
  26560. } else {
  26561. if (element.style.MozUserSelect !== undefined) {
  26562. element.style.MozUserSelect = "none";
  26563. } else if (element.style.WebkitUserSelect !== undefined) {
  26564. element.style.WebkitUserSelect = "none";
  26565. } else if (element.style.KhtmlUserSelect !== undefined) {
  26566. element.style.KhtmlUserSelect = "none";
  26567. }
  26568. }
  26569. }
  26570. });
  26571. function updateFixedOffset() {
  26572. var layer = document.getElementById("edui_fixedlayer");
  26573. uiUtils.setViewportOffset(layer, {
  26574. left: 0,
  26575. top: 0
  26576. });
  26577. // layer.style.display = 'none';
  26578. // layer.style.display = 'block';
  26579. //#trace: 1354
  26580. // setTimeout(updateFixedOffset);
  26581. }
  26582. function bindFixedLayer(adjOffset) {
  26583. domUtils.on(window, "scroll", updateFixedOffset);
  26584. domUtils.on(
  26585. window,
  26586. "resize",
  26587. baidu.editor.utils.defer(updateFixedOffset, 0, true)
  26588. );
  26589. }
  26590. })();
  26591. // ui/uibase.js
  26592. (function() {
  26593. var utils = baidu.editor.utils,
  26594. uiUtils = baidu.editor.ui.uiUtils,
  26595. EventBase = baidu.editor.EventBase,
  26596. UIBase = (baidu.editor.ui.UIBase = function() {});
  26597. UIBase.prototype = {
  26598. className: "",
  26599. uiName: "",
  26600. initOptions: function(options) {
  26601. var me = this;
  26602. for (var k in options) {
  26603. me[k] = options[k];
  26604. }
  26605. this.id = this.id || "edui" + uiUtils.uid();
  26606. },
  26607. initUIBase: function() {
  26608. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  26609. },
  26610. render: function(holder) {
  26611. var html = this.renderHtml();
  26612. var el = uiUtils.createElementByHtml(html);
  26613. //by xuheng 给每个node添加class
  26614. var list = domUtils.getElementsByTagName(el, "*");
  26615. var theme = "edui-" + (this.theme || this.editor.options.theme);
  26616. var layer = document.getElementById("edui_fixedlayer");
  26617. for (var i = 0, node; (node = list[i++]); ) {
  26618. domUtils.addClass(node, theme);
  26619. }
  26620. domUtils.addClass(el, theme);
  26621. if (layer) {
  26622. layer.className = "";
  26623. domUtils.addClass(layer, theme);
  26624. }
  26625. var seatEl = this.getDom();
  26626. if (seatEl != null) {
  26627. seatEl.parentNode.replaceChild(el, seatEl);
  26628. uiUtils.copyAttributes(el, seatEl);
  26629. } else {
  26630. if (typeof holder == "string") {
  26631. holder = document.getElementById(holder);
  26632. }
  26633. holder = holder || uiUtils.getFixedLayer();
  26634. domUtils.addClass(holder, theme);
  26635. holder.appendChild(el);
  26636. }
  26637. this.postRender();
  26638. },
  26639. getDom: function(name) {
  26640. if (!name) {
  26641. return document.getElementById(this.id);
  26642. } else {
  26643. return document.getElementById(this.id + "_" + name);
  26644. }
  26645. },
  26646. postRender: function() {
  26647. this.fireEvent("postrender");
  26648. },
  26649. getHtmlTpl: function() {
  26650. return "";
  26651. },
  26652. formatHtml: function(tpl) {
  26653. var prefix = "edui-" + this.uiName;
  26654. return tpl
  26655. .replace(/##/g, this.id)
  26656. .replace(/%%-/g, this.uiName ? prefix + "-" : "")
  26657. .replace(/%%/g, (this.uiName ? prefix : "") + " " + this.className)
  26658. .replace(/\$\$/g, this._globalKey);
  26659. },
  26660. renderHtml: function() {
  26661. return this.formatHtml(this.getHtmlTpl());
  26662. },
  26663. dispose: function() {
  26664. var box = this.getDom();
  26665. if (box) baidu.editor.dom.domUtils.remove(box);
  26666. uiUtils.unsetGlobal(this.id);
  26667. }
  26668. };
  26669. utils.inherits(UIBase, EventBase);
  26670. })();
  26671. // ui/separator.js
  26672. (function() {
  26673. var utils = baidu.editor.utils,
  26674. UIBase = baidu.editor.ui.UIBase,
  26675. Separator = (baidu.editor.ui.Separator = function(options) {
  26676. this.initOptions(options);
  26677. this.initSeparator();
  26678. });
  26679. Separator.prototype = {
  26680. uiName: "separator",
  26681. initSeparator: function() {
  26682. this.initUIBase();
  26683. },
  26684. getHtmlTpl: function() {
  26685. return '<div id="##" class="edui-box %%"></div>';
  26686. }
  26687. };
  26688. utils.inherits(Separator, UIBase);
  26689. })();
  26690. // ui/mask.js
  26691. ///import core
  26692. ///import uicore
  26693. (function() {
  26694. var utils = baidu.editor.utils,
  26695. domUtils = baidu.editor.dom.domUtils,
  26696. UIBase = baidu.editor.ui.UIBase,
  26697. uiUtils = baidu.editor.ui.uiUtils;
  26698. var Mask = (baidu.editor.ui.Mask = function(options) {
  26699. this.initOptions(options);
  26700. this.initUIBase();
  26701. });
  26702. Mask.prototype = {
  26703. getHtmlTpl: function() {
  26704. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>';
  26705. },
  26706. postRender: function() {
  26707. var me = this;
  26708. domUtils.on(window, "resize", function() {
  26709. setTimeout(function() {
  26710. if (!me.isHidden()) {
  26711. me._fill();
  26712. }
  26713. });
  26714. });
  26715. },
  26716. show: function(zIndex) {
  26717. this._fill();
  26718. this.getDom().style.display = "";
  26719. this.getDom().style.zIndex = zIndex;
  26720. },
  26721. hide: function() {
  26722. this.getDom().style.display = "none";
  26723. this.getDom().style.zIndex = "";
  26724. },
  26725. isHidden: function() {
  26726. return this.getDom().style.display == "none";
  26727. },
  26728. _onMouseDown: function() {
  26729. return false;
  26730. },
  26731. _onClick: function(e, target) {
  26732. this.fireEvent("click", e, target);
  26733. },
  26734. _fill: function() {
  26735. var el = this.getDom();
  26736. var vpRect = uiUtils.getViewportRect();
  26737. el.style.width = vpRect.width + "px";
  26738. el.style.height = vpRect.height + "px";
  26739. }
  26740. };
  26741. utils.inherits(Mask, UIBase);
  26742. })();
  26743. // ui/popup.js
  26744. ///import core
  26745. ///import uicore
  26746. (function() {
  26747. var utils = baidu.editor.utils,
  26748. uiUtils = baidu.editor.ui.uiUtils,
  26749. domUtils = baidu.editor.dom.domUtils,
  26750. UIBase = baidu.editor.ui.UIBase,
  26751. Popup = (baidu.editor.ui.Popup = function(options) {
  26752. this.initOptions(options);
  26753. this.initPopup();
  26754. });
  26755. var allPopups = [];
  26756. function closeAllPopup(evt, el) {
  26757. for (var i = 0; i < allPopups.length; i++) {
  26758. var pop = allPopups[i];
  26759. if (!pop.isHidden()) {
  26760. if (pop.queryAutoHide(el) !== false) {
  26761. if (
  26762. evt &&
  26763. /scroll/gi.test(evt.type) &&
  26764. pop.className == "edui-wordpastepop"
  26765. )
  26766. return;
  26767. pop.hide();
  26768. }
  26769. }
  26770. }
  26771. if (allPopups.length) pop.editor.fireEvent("afterhidepop");
  26772. }
  26773. Popup.postHide = closeAllPopup;
  26774. var ANCHOR_CLASSES = [
  26775. "edui-anchor-topleft",
  26776. "edui-anchor-topright",
  26777. "edui-anchor-bottomleft",
  26778. "edui-anchor-bottomright"
  26779. ];
  26780. Popup.prototype = {
  26781. SHADOW_RADIUS: 5,
  26782. content: null,
  26783. _hidden: false,
  26784. autoRender: true,
  26785. canSideLeft: true,
  26786. canSideUp: true,
  26787. initPopup: function() {
  26788. this.initUIBase();
  26789. allPopups.push(this);
  26790. },
  26791. getHtmlTpl: function() {
  26792. return (
  26793. '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  26794. ' <div id="##_body" class="edui-popup-body">' +
  26795. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  26796. ' <div class="edui-shadow"></div>' +
  26797. ' <div id="##_content" class="edui-popup-content">' +
  26798. this.getContentHtmlTpl() +
  26799. " </div>" +
  26800. " </div>" +
  26801. "</div>"
  26802. );
  26803. },
  26804. getContentHtmlTpl: function() {
  26805. if (this.content) {
  26806. if (typeof this.content == "string") {
  26807. return this.content;
  26808. }
  26809. return this.content.renderHtml();
  26810. } else {
  26811. return "";
  26812. }
  26813. },
  26814. _UIBase_postRender: UIBase.prototype.postRender,
  26815. postRender: function() {
  26816. if (this.content instanceof UIBase) {
  26817. this.content.postRender();
  26818. }
  26819. //捕获鼠标滚轮
  26820. if (this.captureWheel && !this.captured) {
  26821. this.captured = true;
  26822. var winHeight =
  26823. (document.documentElement.clientHeight ||
  26824. document.body.clientHeight) - 80,
  26825. _height = this.getDom().offsetHeight,
  26826. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  26827. content = this.getDom("content"),
  26828. ifr = this.getDom("body").getElementsByTagName("iframe"),
  26829. me = this;
  26830. ifr.length && (ifr = ifr[0]);
  26831. while (_top + _height > winHeight) {
  26832. _height -= 30;
  26833. }
  26834. content.style.height = _height + "px";
  26835. //同步更改iframe高度
  26836. ifr && (ifr.style.height = _height + "px");
  26837. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  26838. if (window.XMLHttpRequest) {
  26839. domUtils.on(
  26840. content,
  26841. "onmousewheel" in document.body ? "mousewheel" : "DOMMouseScroll",
  26842. function(e) {
  26843. if (e.preventDefault) {
  26844. e.preventDefault();
  26845. } else {
  26846. e.returnValue = false;
  26847. }
  26848. if (e.wheelDelta) {
  26849. content.scrollTop -= e.wheelDelta / 120 * 60;
  26850. } else {
  26851. content.scrollTop -= e.detail / -3 * 60;
  26852. }
  26853. }
  26854. );
  26855. } else {
  26856. //ie6
  26857. domUtils.on(this.getDom(), "mousewheel", function(e) {
  26858. e.returnValue = false;
  26859. me.getDom("content").scrollTop -= e.wheelDelta / 120 * 60;
  26860. });
  26861. }
  26862. }
  26863. this.fireEvent("postRenderAfter");
  26864. this.hide(true);
  26865. this._UIBase_postRender();
  26866. },
  26867. _doAutoRender: function() {
  26868. if (!this.getDom() && this.autoRender) {
  26869. this.render();
  26870. }
  26871. },
  26872. mesureSize: function() {
  26873. var box = this.getDom("content");
  26874. return uiUtils.getClientRect(box);
  26875. },
  26876. fitSize: function() {
  26877. if (this.captureWheel && this.sized) {
  26878. return this.__size;
  26879. }
  26880. this.sized = true;
  26881. var popBodyEl = this.getDom("body");
  26882. popBodyEl.style.width = "";
  26883. popBodyEl.style.height = "";
  26884. var size = this.mesureSize();
  26885. if (this.captureWheel) {
  26886. popBodyEl.style.width = -(-20 - size.width) + "px";
  26887. var height = parseInt(this.getDom("content").style.height, 10);
  26888. !window.isNaN(height) && (size.height = height);
  26889. } else {
  26890. popBodyEl.style.width = size.width + "px";
  26891. }
  26892. popBodyEl.style.height = size.height + "px";
  26893. this.__size = size;
  26894. this.captureWheel && (this.getDom("content").style.overflow = "auto");
  26895. return size;
  26896. },
  26897. showAnchor: function(element, hoz) {
  26898. this.showAnchorRect(uiUtils.getClientRect(element), hoz);
  26899. },
  26900. showAnchorRect: function(rect, hoz, adj) {
  26901. this._doAutoRender();
  26902. var vpRect = uiUtils.getViewportRect();
  26903. this.getDom().style.visibility = "hidden";
  26904. this._show();
  26905. var popSize = this.fitSize();
  26906. var sideLeft, sideUp, left, top;
  26907. if (hoz) {
  26908. sideLeft =
  26909. this.canSideLeft &&
  26910. (rect.right + popSize.width > vpRect.right &&
  26911. rect.left > popSize.width);
  26912. sideUp =
  26913. this.canSideUp &&
  26914. (rect.top + popSize.height > vpRect.bottom &&
  26915. rect.bottom > popSize.height);
  26916. left = sideLeft ? rect.left - popSize.width : rect.right;
  26917. top = sideUp ? rect.bottom - popSize.height : rect.top;
  26918. } else {
  26919. sideLeft =
  26920. this.canSideLeft &&
  26921. (rect.right + popSize.width > vpRect.right &&
  26922. rect.left > popSize.width);
  26923. sideUp =
  26924. this.canSideUp &&
  26925. (rect.top + popSize.height > vpRect.bottom &&
  26926. rect.bottom > popSize.height);
  26927. left = sideLeft ? rect.right - popSize.width : rect.left;
  26928. top = sideUp ? rect.top - popSize.height : rect.bottom;
  26929. }
  26930. var popEl = this.getDom();
  26931. uiUtils.setViewportOffset(popEl, {
  26932. left: left,
  26933. top: top
  26934. });
  26935. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  26936. popEl.className +=
  26937. " " + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  26938. if (this.editor) {
  26939. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  26940. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  26941. popEl.style.zIndex - 1;
  26942. }
  26943. this.getDom().style.visibility = "visible";
  26944. },
  26945. showAt: function(offset) {
  26946. var left = offset.left;
  26947. var top = offset.top;
  26948. var rect = {
  26949. left: left,
  26950. top: top,
  26951. right: left,
  26952. bottom: top,
  26953. height: 0,
  26954. width: 0
  26955. };
  26956. this.showAnchorRect(rect, false, true);
  26957. },
  26958. _show: function() {
  26959. if (this._hidden) {
  26960. var box = this.getDom();
  26961. box.style.display = "";
  26962. this._hidden = false;
  26963. // if (box.setActive) {
  26964. // box.setActive();
  26965. // }
  26966. this.fireEvent("show");
  26967. }
  26968. },
  26969. isHidden: function() {
  26970. return this._hidden;
  26971. },
  26972. show: function() {
  26973. this._doAutoRender();
  26974. this._show();
  26975. },
  26976. hide: function(notNofity) {
  26977. if (!this._hidden && this.getDom()) {
  26978. this.getDom().style.display = "none";
  26979. this._hidden = true;
  26980. if (!notNofity) {
  26981. this.fireEvent("hide");
  26982. }
  26983. }
  26984. },
  26985. queryAutoHide: function(el) {
  26986. return !el || !uiUtils.contains(this.getDom(), el);
  26987. }
  26988. };
  26989. utils.inherits(Popup, UIBase);
  26990. domUtils.on(document, "mousedown", function(evt) {
  26991. var el = evt.target || evt.srcElement;
  26992. closeAllPopup(evt, el);
  26993. });
  26994. domUtils.on(window, "scroll", function(evt, el) {
  26995. closeAllPopup(evt, el);
  26996. });
  26997. })();
  26998. // ui/colorpicker.js
  26999. ///import core
  27000. ///import uicore
  27001. (function() {
  27002. var utils = baidu.editor.utils,
  27003. UIBase = baidu.editor.ui.UIBase,
  27004. ColorPicker = (baidu.editor.ui.ColorPicker = function(options) {
  27005. this.initOptions(options);
  27006. this.noColorText = this.noColorText || this.editor.getLang("clearColor");
  27007. this.initUIBase();
  27008. });
  27009. ColorPicker.prototype = {
  27010. getHtmlTpl: function() {
  27011. return genColorPicker(this.noColorText, this.editor);
  27012. },
  27013. _onTableClick: function(evt) {
  27014. var tgt = evt.target || evt.srcElement;
  27015. var color = tgt.getAttribute("data-color");
  27016. if (color) {
  27017. this.fireEvent("pickcolor", color);
  27018. }
  27019. },
  27020. _onTableOver: function(evt) {
  27021. var tgt = evt.target || evt.srcElement;
  27022. var color = tgt.getAttribute("data-color");
  27023. if (color) {
  27024. this.getDom("preview").style.backgroundColor = color;
  27025. }
  27026. },
  27027. _onTableOut: function() {
  27028. this.getDom("preview").style.backgroundColor = "";
  27029. },
  27030. _onPickNoColor: function() {
  27031. this.fireEvent("picknocolor");
  27032. }
  27033. };
  27034. utils.inherits(ColorPicker, UIBase);
  27035. var COLORS = ("ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646," +
  27036. "f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada," +
  27037. "d8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5," +
  27038. "bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f," +
  27039. "a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09," +
  27040. "7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806," +
  27041. "c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,").split(
  27042. ","
  27043. );
  27044. function genColorPicker(noColorText, editor) {
  27045. var html =
  27046. '<div id="##" class="edui-colorpicker %%">' +
  27047. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  27048. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  27049. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' +
  27050. noColorText +
  27051. "</div>" +
  27052. "</div>" +
  27053. '<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">' +
  27054. '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;padding-top: 2px"><td colspan="10">' +
  27055. editor.getLang("themeColor") +
  27056. "</td> </tr>" +
  27057. '<tr class="edui-colorpicker-tablefirstrow" >';
  27058. for (var i = 0; i < COLORS.length; i++) {
  27059. if (i && i % 10 === 0) {
  27060. html +=
  27061. "</tr>" +
  27062. (i == 60
  27063. ? '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;"><td colspan="10">' +
  27064. editor.getLang("standardColor") +
  27065. "</td></tr>"
  27066. : "") +
  27067. "<tr" +
  27068. (i == 60 ? ' class="edui-colorpicker-tablefirstrow"' : "") +
  27069. ">";
  27070. }
  27071. html += i < 70
  27072. ? '<td style="padding: 0 2px;"><a hidefocus title="' +
  27073. COLORS[i] +
  27074. '" onclick="return false;" href="javascript:" unselectable="on" class="edui-box edui-colorpicker-colorcell"' +
  27075. ' data-color="#' +
  27076. COLORS[i] +
  27077. '"' +
  27078. ' style="background-color:#' +
  27079. COLORS[i] +
  27080. ";border:solid #ccc;" +
  27081. (i < 10 || i >= 60
  27082. ? "border-width:1px;"
  27083. : i >= 10 && i < 20
  27084. ? "border-width:1px 1px 0 1px;"
  27085. : "border-width:0 1px 0 1px;") +
  27086. '"' +
  27087. "></a></td>"
  27088. : "";
  27089. }
  27090. html += "</tr></table></div>";
  27091. return html;
  27092. }
  27093. })();
  27094. // ui/tablepicker.js
  27095. ///import core
  27096. ///import uicore
  27097. (function() {
  27098. var utils = baidu.editor.utils,
  27099. uiUtils = baidu.editor.ui.uiUtils,
  27100. UIBase = baidu.editor.ui.UIBase;
  27101. var TablePicker = (baidu.editor.ui.TablePicker = function(options) {
  27102. this.initOptions(options);
  27103. this.initTablePicker();
  27104. });
  27105. TablePicker.prototype = {
  27106. defaultNumRows: 10,
  27107. defaultNumCols: 10,
  27108. maxNumRows: 20,
  27109. maxNumCols: 20,
  27110. numRows: 10,
  27111. numCols: 10,
  27112. lengthOfCellSide: 22,
  27113. initTablePicker: function() {
  27114. this.initUIBase();
  27115. },
  27116. getHtmlTpl: function() {
  27117. var me = this;
  27118. return (
  27119. '<div id="##" class="edui-tablepicker %%">' +
  27120. '<div class="edui-tablepicker-body">' +
  27121. '<div class="edui-infoarea">' +
  27122. '<span id="##_label" class="edui-label"></span>' +
  27123. "</div>" +
  27124. '<div class="edui-pickarea"' +
  27125. ' onmousemove="$$._onMouseMove(event, this);"' +
  27126. ' onmouseover="$$._onMouseOver(event, this);"' +
  27127. ' onmouseout="$$._onMouseOut(event, this);"' +
  27128. ' onclick="$$._onClick(event, this);"' +
  27129. ">" +
  27130. '<div id="##_overlay" class="edui-overlay"></div>' +
  27131. "</div>" +
  27132. "</div>" +
  27133. "</div>"
  27134. );
  27135. },
  27136. _UIBase_render: UIBase.prototype.render,
  27137. render: function(holder) {
  27138. this._UIBase_render(holder);
  27139. this.getDom("label").innerHTML =
  27140. "0" +
  27141. this.editor.getLang("t_row") +
  27142. " x 0" +
  27143. this.editor.getLang("t_col");
  27144. },
  27145. _track: function(numCols, numRows) {
  27146. var style = this.getDom("overlay").style;
  27147. var sideLen = this.lengthOfCellSide;
  27148. style.width = numCols * sideLen + "px";
  27149. style.height = numRows * sideLen + "px";
  27150. var label = this.getDom("label");
  27151. label.innerHTML =
  27152. numCols +
  27153. this.editor.getLang("t_col") +
  27154. " x " +
  27155. numRows +
  27156. this.editor.getLang("t_row");
  27157. this.numCols = numCols;
  27158. this.numRows = numRows;
  27159. },
  27160. _onMouseOver: function(evt, el) {
  27161. var rel = evt.relatedTarget || evt.fromElement;
  27162. if (!uiUtils.contains(el, rel) && el !== rel) {
  27163. this.getDom("label").innerHTML =
  27164. "0" +
  27165. this.editor.getLang("t_col") +
  27166. " x 0" +
  27167. this.editor.getLang("t_row");
  27168. this.getDom("overlay").style.visibility = "";
  27169. }
  27170. },
  27171. _onMouseOut: function(evt, el) {
  27172. var rel = evt.relatedTarget || evt.toElement;
  27173. if (!uiUtils.contains(el, rel) && el !== rel) {
  27174. this.getDom("label").innerHTML =
  27175. "0" +
  27176. this.editor.getLang("t_col") +
  27177. " x 0" +
  27178. this.editor.getLang("t_row");
  27179. this.getDom("overlay").style.visibility = "hidden";
  27180. }
  27181. },
  27182. _onMouseMove: function(evt, el) {
  27183. var style = this.getDom("overlay").style;
  27184. var offset = uiUtils.getEventOffset(evt);
  27185. var sideLen = this.lengthOfCellSide;
  27186. var numCols = Math.ceil(offset.left / sideLen);
  27187. var numRows = Math.ceil(offset.top / sideLen);
  27188. this._track(numCols, numRows);
  27189. },
  27190. _onClick: function() {
  27191. this.fireEvent("picktable", this.numCols, this.numRows);
  27192. }
  27193. };
  27194. utils.inherits(TablePicker, UIBase);
  27195. })();
  27196. // ui/stateful.js
  27197. (function() {
  27198. var browser = baidu.editor.browser,
  27199. domUtils = baidu.editor.dom.domUtils,
  27200. uiUtils = baidu.editor.ui.uiUtils;
  27201. var TPL_STATEFUL =
  27202. 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  27203. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  27204. (browser.ie
  27205. ? ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  27206. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"'
  27207. : ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  27208. ' onmouseout="$$.Stateful_onMouseOut(event, this);"');
  27209. baidu.editor.ui.Stateful = {
  27210. alwalysHoverable: false,
  27211. target: null, //目标元素和this指向dom不一样
  27212. Stateful_init: function() {
  27213. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  27214. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  27215. },
  27216. Stateful_getHtmlTpl: function() {
  27217. var tpl = this._Stateful_dGetHtmlTpl();
  27218. // 使用function避免$转义
  27219. return tpl.replace(/stateful/g, function() {
  27220. return TPL_STATEFUL;
  27221. });
  27222. },
  27223. Stateful_onMouseEnter: function(evt, el) {
  27224. this.target = el;
  27225. if (!this.isDisabled() || this.alwalysHoverable) {
  27226. this.addState("hover");
  27227. this.fireEvent("over");
  27228. }
  27229. },
  27230. Stateful_onMouseLeave: function(evt, el) {
  27231. if (!this.isDisabled() || this.alwalysHoverable) {
  27232. this.removeState("hover");
  27233. this.removeState("active");
  27234. this.fireEvent("out");
  27235. }
  27236. },
  27237. Stateful_onMouseOver: function(evt, el) {
  27238. var rel = evt.relatedTarget;
  27239. if (!uiUtils.contains(el, rel) && el !== rel) {
  27240. this.Stateful_onMouseEnter(evt, el);
  27241. }
  27242. },
  27243. Stateful_onMouseOut: function(evt, el) {
  27244. var rel = evt.relatedTarget;
  27245. if (!uiUtils.contains(el, rel) && el !== rel) {
  27246. this.Stateful_onMouseLeave(evt, el);
  27247. }
  27248. },
  27249. Stateful_onMouseDown: function(evt, el) {
  27250. if (!this.isDisabled()) {
  27251. this.addState("active");
  27252. }
  27253. },
  27254. Stateful_onMouseUp: function(evt, el) {
  27255. if (!this.isDisabled()) {
  27256. this.removeState("active");
  27257. }
  27258. },
  27259. Stateful_postRender: function() {
  27260. if (this.disabled && !this.hasState("disabled")) {
  27261. this.addState("disabled");
  27262. }
  27263. },
  27264. hasState: function(state) {
  27265. return domUtils.hasClass(this.getStateDom(), "edui-state-" + state);
  27266. },
  27267. addState: function(state) {
  27268. if (!this.hasState(state)) {
  27269. this.getStateDom().className += " edui-state-" + state;
  27270. }
  27271. },
  27272. removeState: function(state) {
  27273. if (this.hasState(state)) {
  27274. domUtils.removeClasses(this.getStateDom(), ["edui-state-" + state]);
  27275. }
  27276. },
  27277. getStateDom: function() {
  27278. return this.getDom("state");
  27279. },
  27280. isChecked: function() {
  27281. return this.hasState("checked");
  27282. },
  27283. setChecked: function(checked) {
  27284. if (!this.isDisabled() && checked) {
  27285. this.addState("checked");
  27286. } else {
  27287. this.removeState("checked");
  27288. }
  27289. },
  27290. isDisabled: function() {
  27291. return this.hasState("disabled");
  27292. },
  27293. setDisabled: function(disabled) {
  27294. if (disabled) {
  27295. this.removeState("hover");
  27296. this.removeState("checked");
  27297. this.removeState("active");
  27298. this.addState("disabled");
  27299. } else {
  27300. this.removeState("disabled");
  27301. }
  27302. }
  27303. };
  27304. })();
  27305. // ui/button.js
  27306. ///import core
  27307. ///import uicore
  27308. ///import ui/stateful.js
  27309. (function() {
  27310. var utils = baidu.editor.utils,
  27311. UIBase = baidu.editor.ui.UIBase,
  27312. Stateful = baidu.editor.ui.Stateful,
  27313. Button = (baidu.editor.ui.Button = function(options) {
  27314. if (options.name) {
  27315. var btnName = options.name;
  27316. var cssRules = options.cssRules;
  27317. if (!options.className) {
  27318. options.className = "edui-for-" + btnName;
  27319. }
  27320. options.cssRules =
  27321. ".edui-" +
  27322. (options.theme || "default") +
  27323. " .edui-toolbar .edui-button.edui-for-" +
  27324. btnName +
  27325. " .edui-icon {" +
  27326. cssRules +
  27327. "}";
  27328. }
  27329. this.initOptions(options);
  27330. this.initButton();
  27331. });
  27332. Button.prototype = {
  27333. uiName: "button",
  27334. label: "",
  27335. title: "",
  27336. showIcon: true,
  27337. showText: true,
  27338. cssRules: "",
  27339. initButton: function() {
  27340. this.initUIBase();
  27341. this.Stateful_init();
  27342. if (this.cssRules) {
  27343. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  27344. }
  27345. },
  27346. getHtmlTpl: function() {
  27347. return (
  27348. '<div id="##" class="edui-box %%">' +
  27349. '<div id="##_state" stateful>' +
  27350. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' +
  27351. (this.title ? 'title="' + this.title + '"' : "") +
  27352. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  27353. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : "") +
  27354. (this.showText
  27355. ? '<div class="edui-box edui-label">' + this.label + "</div>"
  27356. : "") +
  27357. "</div>" +
  27358. "</div>" +
  27359. "</div></div>"
  27360. );
  27361. },
  27362. postRender: function() {
  27363. this.Stateful_postRender();
  27364. this.setDisabled(this.disabled);
  27365. },
  27366. _onMouseDown: function(e) {
  27367. var target = e.target || e.srcElement,
  27368. tagName = target && target.tagName && target.tagName.toLowerCase();
  27369. if (tagName == "input" || tagName == "object" || tagName == "object") {
  27370. return false;
  27371. }
  27372. },
  27373. _onClick: function() {
  27374. if (!this.isDisabled()) {
  27375. this.fireEvent("click");
  27376. }
  27377. },
  27378. setTitle: function(text) {
  27379. var label = this.getDom("label");
  27380. label.innerHTML = text;
  27381. }
  27382. };
  27383. utils.inherits(Button, UIBase);
  27384. utils.extend(Button.prototype, Stateful);
  27385. })();
  27386. // ui/splitbutton.js
  27387. ///import core
  27388. ///import uicore
  27389. ///import ui/stateful.js
  27390. (function() {
  27391. var utils = baidu.editor.utils,
  27392. uiUtils = baidu.editor.ui.uiUtils,
  27393. domUtils = baidu.editor.dom.domUtils,
  27394. UIBase = baidu.editor.ui.UIBase,
  27395. Stateful = baidu.editor.ui.Stateful,
  27396. SplitButton = (baidu.editor.ui.SplitButton = function(options) {
  27397. this.initOptions(options);
  27398. this.initSplitButton();
  27399. });
  27400. SplitButton.prototype = {
  27401. popup: null,
  27402. uiName: "splitbutton",
  27403. title: "",
  27404. initSplitButton: function() {
  27405. this.initUIBase();
  27406. this.Stateful_init();
  27407. var me = this;
  27408. if (this.popup != null) {
  27409. var popup = this.popup;
  27410. this.popup = null;
  27411. this.setPopup(popup);
  27412. }
  27413. },
  27414. _UIBase_postRender: UIBase.prototype.postRender,
  27415. postRender: function() {
  27416. this.Stateful_postRender();
  27417. this._UIBase_postRender();
  27418. },
  27419. setPopup: function(popup) {
  27420. if (this.popup === popup) return;
  27421. if (this.popup != null) {
  27422. this.popup.dispose();
  27423. }
  27424. popup.addListener("show", utils.bind(this._onPopupShow, this));
  27425. popup.addListener("hide", utils.bind(this._onPopupHide, this));
  27426. popup.addListener(
  27427. "postrender",
  27428. utils.bind(function() {
  27429. popup
  27430. .getDom("body")
  27431. .appendChild(
  27432. uiUtils.createElementByHtml(
  27433. '<div id="' +
  27434. this.popup.id +
  27435. '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  27436. (uiUtils.getClientRect(this.getDom()).width + 20) +
  27437. 'px"></div>'
  27438. )
  27439. );
  27440. popup.getDom().className += " " + this.className;
  27441. }, this)
  27442. );
  27443. this.popup = popup;
  27444. },
  27445. _onPopupShow: function() {
  27446. this.addState("opened");
  27447. },
  27448. _onPopupHide: function() {
  27449. this.removeState("opened");
  27450. },
  27451. getHtmlTpl: function() {
  27452. return (
  27453. '<div id="##" class="edui-box %%">' +
  27454. "<div " +
  27455. (this.title ? 'title="' + this.title + '"' : "") +
  27456. ' id="##_state" stateful><div class="%%-body">' +
  27457. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  27458. '<div class="edui-box edui-icon"></div>' +
  27459. "</div>" +
  27460. '<div class="edui-box edui-splitborder"></div>' +
  27461. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  27462. "</div></div></div>"
  27463. );
  27464. },
  27465. showPopup: function() {
  27466. // 当popup往上弹出的时候,做特殊处理
  27467. var rect = uiUtils.getClientRect(this.getDom());
  27468. rect.top -= this.popup.SHADOW_RADIUS;
  27469. rect.height += this.popup.SHADOW_RADIUS;
  27470. this.popup.showAnchorRect(rect);
  27471. },
  27472. _onArrowClick: function(event, el) {
  27473. if (!this.isDisabled()) {
  27474. this.showPopup();
  27475. }
  27476. },
  27477. _onButtonClick: function() {
  27478. if (!this.isDisabled()) {
  27479. this.fireEvent("buttonclick");
  27480. }
  27481. }
  27482. };
  27483. utils.inherits(SplitButton, UIBase);
  27484. utils.extend(SplitButton.prototype, Stateful, true);
  27485. })();
  27486. // ui/colorbutton.js
  27487. ///import core
  27488. ///import uicore
  27489. ///import ui/colorpicker.js
  27490. ///import ui/popup.js
  27491. ///import ui/splitbutton.js
  27492. (function() {
  27493. var utils = baidu.editor.utils,
  27494. uiUtils = baidu.editor.ui.uiUtils,
  27495. ColorPicker = baidu.editor.ui.ColorPicker,
  27496. Popup = baidu.editor.ui.Popup,
  27497. SplitButton = baidu.editor.ui.SplitButton,
  27498. ColorButton = (baidu.editor.ui.ColorButton = function(options) {
  27499. this.initOptions(options);
  27500. this.initColorButton();
  27501. });
  27502. ColorButton.prototype = {
  27503. initColorButton: function() {
  27504. var me = this;
  27505. this.popup = new Popup({
  27506. content: new ColorPicker({
  27507. noColorText: me.editor.getLang("clearColor"),
  27508. editor: me.editor,
  27509. onpickcolor: function(t, color) {
  27510. me._onPickColor(color);
  27511. },
  27512. onpicknocolor: function(t, color) {
  27513. me._onPickNoColor(color);
  27514. }
  27515. }),
  27516. editor: me.editor
  27517. });
  27518. this.initSplitButton();
  27519. },
  27520. _SplitButton_postRender: SplitButton.prototype.postRender,
  27521. postRender: function() {
  27522. this._SplitButton_postRender();
  27523. this.getDom("button_body").appendChild(
  27524. uiUtils.createElementByHtml(
  27525. '<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>'
  27526. )
  27527. );
  27528. this.getDom().className += " edui-colorbutton";
  27529. },
  27530. setColor: function(color) {
  27531. this.getDom("colorlump").style.backgroundColor = color;
  27532. this.color = color;
  27533. },
  27534. _onPickColor: function(color) {
  27535. if (this.fireEvent("pickcolor", color) !== false) {
  27536. this.setColor(color);
  27537. this.popup.hide();
  27538. }
  27539. },
  27540. _onPickNoColor: function(color) {
  27541. if (this.fireEvent("picknocolor") !== false) {
  27542. this.popup.hide();
  27543. }
  27544. }
  27545. };
  27546. utils.inherits(ColorButton, SplitButton);
  27547. })();
  27548. // ui/tablebutton.js
  27549. ///import core
  27550. ///import uicore
  27551. ///import ui/popup.js
  27552. ///import ui/tablepicker.js
  27553. ///import ui/splitbutton.js
  27554. (function() {
  27555. var utils = baidu.editor.utils,
  27556. Popup = baidu.editor.ui.Popup,
  27557. TablePicker = baidu.editor.ui.TablePicker,
  27558. SplitButton = baidu.editor.ui.SplitButton,
  27559. TableButton = (baidu.editor.ui.TableButton = function(options) {
  27560. this.initOptions(options);
  27561. this.initTableButton();
  27562. });
  27563. TableButton.prototype = {
  27564. initTableButton: function() {
  27565. var me = this;
  27566. this.popup = new Popup({
  27567. content: new TablePicker({
  27568. editor: me.editor,
  27569. onpicktable: function(t, numCols, numRows) {
  27570. me._onPickTable(numCols, numRows);
  27571. }
  27572. }),
  27573. editor: me.editor
  27574. });
  27575. this.initSplitButton();
  27576. },
  27577. _onPickTable: function(numCols, numRows) {
  27578. if (this.fireEvent("picktable", numCols, numRows) !== false) {
  27579. this.popup.hide();
  27580. }
  27581. }
  27582. };
  27583. utils.inherits(TableButton, SplitButton);
  27584. })();
  27585. // ui/autotypesetpicker.js
  27586. ///import core
  27587. ///import uicore
  27588. (function() {
  27589. var utils = baidu.editor.utils,
  27590. UIBase = baidu.editor.ui.UIBase;
  27591. var AutoTypeSetPicker = (baidu.editor.ui.AutoTypeSetPicker = function(
  27592. options
  27593. ) {
  27594. this.initOptions(options);
  27595. this.initAutoTypeSetPicker();
  27596. });
  27597. AutoTypeSetPicker.prototype = {
  27598. initAutoTypeSetPicker: function() {
  27599. this.initUIBase();
  27600. },
  27601. getHtmlTpl: function() {
  27602. var me = this.editor,
  27603. opt = me.options.autotypeset,
  27604. lang = me.getLang("autoTypeSet");
  27605. var textAlignInputName = "textAlignValue" + me.uid,
  27606. imageBlockInputName = "imageBlockLineValue" + me.uid,
  27607. symbolConverInputName = "symbolConverValue" + me.uid;
  27608. return (
  27609. '<div id="##" class="edui-autotypesetpicker %%">' +
  27610. '<div class="edui-autotypesetpicker-body">' +
  27611. "<table >" +
  27612. '<tr><td nowrap><input type="checkbox" name="mergeEmptyline" ' +
  27613. (opt["mergeEmptyline"] ? "checked" : "") +
  27614. ">" +
  27615. lang.mergeLine +
  27616. '</td><td colspan="2"><input type="checkbox" name="removeEmptyline" ' +
  27617. (opt["removeEmptyline"] ? "checked" : "") +
  27618. ">" +
  27619. lang.delLine +
  27620. "</td></tr>" +
  27621. '<tr><td nowrap><input type="checkbox" name="removeClass" ' +
  27622. (opt["removeClass"] ? "checked" : "") +
  27623. ">" +
  27624. lang.removeFormat +
  27625. '</td><td colspan="2"><input type="checkbox" name="indent" ' +
  27626. (opt["indent"] ? "checked" : "") +
  27627. ">" +
  27628. lang.indent +
  27629. "</td></tr>" +
  27630. "<tr>" +
  27631. '<td nowrap><input type="checkbox" name="textAlign" ' +
  27632. (opt["textAlign"] ? "checked" : "") +
  27633. ">" +
  27634. lang.alignment +
  27635. "</td>" +
  27636. '<td colspan="2" id="' +
  27637. textAlignInputName +
  27638. '">' +
  27639. '<input type="radio" name="' +
  27640. textAlignInputName +
  27641. '" value="left" ' +
  27642. (opt["textAlign"] && opt["textAlign"] == "left" ? "checked" : "") +
  27643. ">" +
  27644. me.getLang("justifyleft") +
  27645. '<input type="radio" name="' +
  27646. textAlignInputName +
  27647. '" value="center" ' +
  27648. (opt["textAlign"] && opt["textAlign"] == "center" ? "checked" : "") +
  27649. ">" +
  27650. me.getLang("justifycenter") +
  27651. '<input type="radio" name="' +
  27652. textAlignInputName +
  27653. '" value="right" ' +
  27654. (opt["textAlign"] && opt["textAlign"] == "right" ? "checked" : "") +
  27655. ">" +
  27656. me.getLang("justifyright") +
  27657. "</td>" +
  27658. "</tr>" +
  27659. "<tr>" +
  27660. '<td nowrap><input type="checkbox" name="imageBlockLine" ' +
  27661. (opt["imageBlockLine"] ? "checked" : "") +
  27662. ">" +
  27663. lang.imageFloat +
  27664. "</td>" +
  27665. '<td nowrap id="' +
  27666. imageBlockInputName +
  27667. '">' +
  27668. '<input type="radio" name="' +
  27669. imageBlockInputName +
  27670. '" value="none" ' +
  27671. (opt["imageBlockLine"] && opt["imageBlockLine"] == "none"
  27672. ? "checked"
  27673. : "") +
  27674. ">" +
  27675. me.getLang("default") +
  27676. '<input type="radio" name="' +
  27677. imageBlockInputName +
  27678. '" value="left" ' +
  27679. (opt["imageBlockLine"] && opt["imageBlockLine"] == "left"
  27680. ? "checked"
  27681. : "") +
  27682. ">" +
  27683. me.getLang("justifyleft") +
  27684. '<input type="radio" name="' +
  27685. imageBlockInputName +
  27686. '" value="center" ' +
  27687. (opt["imageBlockLine"] && opt["imageBlockLine"] == "center"
  27688. ? "checked"
  27689. : "") +
  27690. ">" +
  27691. me.getLang("justifycenter") +
  27692. '<input type="radio" name="' +
  27693. imageBlockInputName +
  27694. '" value="right" ' +
  27695. (opt["imageBlockLine"] && opt["imageBlockLine"] == "right"
  27696. ? "checked"
  27697. : "") +
  27698. ">" +
  27699. me.getLang("justifyright") +
  27700. "</td>" +
  27701. "</tr>" +
  27702. '<tr><td nowrap><input type="checkbox" name="clearFontSize" ' +
  27703. (opt["clearFontSize"] ? "checked" : "") +
  27704. ">" +
  27705. lang.removeFontsize +
  27706. '</td><td colspan="2"><input type="checkbox" name="clearFontFamily" ' +
  27707. (opt["clearFontFamily"] ? "checked" : "") +
  27708. ">" +
  27709. lang.removeFontFamily +
  27710. "</td></tr>" +
  27711. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' +
  27712. (opt["removeEmptyNode"] ? "checked" : "") +
  27713. ">" +
  27714. lang.removeHtml +
  27715. "</td></tr>" +
  27716. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' +
  27717. (opt["pasteFilter"] ? "checked" : "") +
  27718. ">" +
  27719. lang.pasteFilter +
  27720. "</td></tr>" +
  27721. "<tr>" +
  27722. '<td nowrap><input type="checkbox" name="symbolConver" ' +
  27723. (opt["bdc2sb"] || opt["tobdc"] ? "checked" : "") +
  27724. ">" +
  27725. lang.symbol +
  27726. "</td>" +
  27727. '<td id="' +
  27728. symbolConverInputName +
  27729. '">' +
  27730. '<input type="radio" name="bdc" value="bdc2sb" ' +
  27731. (opt["bdc2sb"] ? "checked" : "") +
  27732. ">" +
  27733. lang.bdc2sb +
  27734. '<input type="radio" name="bdc" value="tobdc" ' +
  27735. (opt["tobdc"] ? "checked" : "") +
  27736. ">" +
  27737. lang.tobdc +
  27738. "" +
  27739. "</td>" +
  27740. '<td nowrap align="right"><button >' +
  27741. lang.run +
  27742. "</button></td>" +
  27743. "</tr>" +
  27744. "</table>" +
  27745. "</div>" +
  27746. "</div>"
  27747. );
  27748. },
  27749. _UIBase_render: UIBase.prototype.render
  27750. };
  27751. utils.inherits(AutoTypeSetPicker, UIBase);
  27752. })();
  27753. // ui/autotypesetbutton.js
  27754. ///import core
  27755. ///import uicore
  27756. ///import ui/popup.js
  27757. ///import ui/autotypesetpicker.js
  27758. ///import ui/splitbutton.js
  27759. (function() {
  27760. var utils = baidu.editor.utils,
  27761. Popup = baidu.editor.ui.Popup,
  27762. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  27763. SplitButton = baidu.editor.ui.SplitButton,
  27764. AutoTypeSetButton = (baidu.editor.ui.AutoTypeSetButton = function(options) {
  27765. this.initOptions(options);
  27766. this.initAutoTypeSetButton();
  27767. });
  27768. function getPara(me) {
  27769. var opt = {},
  27770. cont = me.getDom(),
  27771. editorId = me.editor.uid,
  27772. inputType = null,
  27773. attrName = null,
  27774. ipts = domUtils.getElementsByTagName(cont, "input");
  27775. for (var i = ipts.length - 1, ipt; (ipt = ipts[i--]); ) {
  27776. inputType = ipt.getAttribute("type");
  27777. if (inputType == "checkbox") {
  27778. attrName = ipt.getAttribute("name");
  27779. opt[attrName] && delete opt[attrName];
  27780. if (ipt.checked) {
  27781. var attrValue = document.getElementById(
  27782. attrName + "Value" + editorId
  27783. );
  27784. if (attrValue) {
  27785. if (/input/gi.test(attrValue.tagName)) {
  27786. opt[attrName] = attrValue.value;
  27787. } else {
  27788. var iptChilds = attrValue.getElementsByTagName("input");
  27789. for (
  27790. var j = iptChilds.length - 1, iptchild;
  27791. (iptchild = iptChilds[j--]);
  27792. ) {
  27793. if (iptchild.checked) {
  27794. opt[attrName] = iptchild.value;
  27795. break;
  27796. }
  27797. }
  27798. }
  27799. } else {
  27800. opt[attrName] = true;
  27801. }
  27802. } else {
  27803. opt[attrName] = false;
  27804. }
  27805. } else {
  27806. opt[ipt.getAttribute("value")] = ipt.checked;
  27807. }
  27808. }
  27809. var selects = domUtils.getElementsByTagName(cont, "select");
  27810. for (var i = 0, si; (si = selects[i++]); ) {
  27811. var attr = si.getAttribute("name");
  27812. opt[attr] = opt[attr] ? si.value : "";
  27813. }
  27814. utils.extend(me.editor.options.autotypeset, opt);
  27815. me.editor.setPreferences("autotypeset", opt);
  27816. }
  27817. AutoTypeSetButton.prototype = {
  27818. initAutoTypeSetButton: function() {
  27819. var me = this;
  27820. this.popup = new Popup({
  27821. //传入配置参数
  27822. content: new AutoTypeSetPicker({ editor: me.editor }),
  27823. editor: me.editor,
  27824. hide: function() {
  27825. if (!this._hidden && this.getDom()) {
  27826. getPara(this);
  27827. this.getDom().style.display = "none";
  27828. this._hidden = true;
  27829. this.fireEvent("hide");
  27830. }
  27831. }
  27832. });
  27833. var flag = 0;
  27834. this.popup.addListener("postRenderAfter", function() {
  27835. var popupUI = this;
  27836. if (flag) return;
  27837. var cont = this.getDom(),
  27838. btn = cont.getElementsByTagName("button")[0];
  27839. btn.onclick = function() {
  27840. getPara(popupUI);
  27841. me.editor.execCommand("autotypeset");
  27842. popupUI.hide();
  27843. };
  27844. domUtils.on(cont, "click", function(e) {
  27845. var target = e.target || e.srcElement,
  27846. editorId = me.editor.uid;
  27847. if (target && target.tagName == "INPUT") {
  27848. // 点击图片浮动的checkbox,去除对应的radio
  27849. if (
  27850. target.name == "imageBlockLine" ||
  27851. target.name == "textAlign" ||
  27852. target.name == "symbolConver"
  27853. ) {
  27854. var checked = target.checked,
  27855. radioTd = document.getElementById(
  27856. target.name + "Value" + editorId
  27857. ),
  27858. radios = radioTd.getElementsByTagName("input"),
  27859. defalutSelect = {
  27860. imageBlockLine: "none",
  27861. textAlign: "left",
  27862. symbolConver: "tobdc"
  27863. };
  27864. for (var i = 0; i < radios.length; i++) {
  27865. if (checked) {
  27866. if (radios[i].value == defalutSelect[target.name]) {
  27867. radios[i].checked = "checked";
  27868. }
  27869. } else {
  27870. radios[i].checked = false;
  27871. }
  27872. }
  27873. }
  27874. // 点击radio,选中对应的checkbox
  27875. if (
  27876. target.name == "imageBlockLineValue" + editorId ||
  27877. target.name == "textAlignValue" + editorId ||
  27878. target.name == "bdc"
  27879. ) {
  27880. var checkboxs = target.parentNode.previousSibling.getElementsByTagName(
  27881. "input"
  27882. );
  27883. checkboxs && (checkboxs[0].checked = true);
  27884. }
  27885. getPara(popupUI);
  27886. }
  27887. });
  27888. flag = 1;
  27889. });
  27890. this.initSplitButton();
  27891. }
  27892. };
  27893. utils.inherits(AutoTypeSetButton, SplitButton);
  27894. })();
  27895. // ui/cellalignpicker.js
  27896. ///import core
  27897. ///import uicore
  27898. (function() {
  27899. var utils = baidu.editor.utils,
  27900. Popup = baidu.editor.ui.Popup,
  27901. Stateful = baidu.editor.ui.Stateful,
  27902. UIBase = baidu.editor.ui.UIBase;
  27903. /**
  27904. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  27905. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  27906. * @update 2013/4/2 hancong03@baidu.com
  27907. */
  27908. var CellAlignPicker = (baidu.editor.ui.CellAlignPicker = function(options) {
  27909. this.initOptions(options);
  27910. this.initSelected();
  27911. this.initCellAlignPicker();
  27912. });
  27913. CellAlignPicker.prototype = {
  27914. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  27915. initSelected: function() {
  27916. var status = {
  27917. valign: {
  27918. top: 0,
  27919. middle: 1,
  27920. bottom: 2
  27921. },
  27922. align: {
  27923. left: 0,
  27924. center: 1,
  27925. right: 2
  27926. },
  27927. count: 3
  27928. },
  27929. result = -1;
  27930. if (this.selected) {
  27931. this.selectedIndex =
  27932. status.valign[this.selected.valign] * status.count +
  27933. status.align[this.selected.align];
  27934. }
  27935. },
  27936. initCellAlignPicker: function() {
  27937. this.initUIBase();
  27938. this.Stateful_init();
  27939. },
  27940. getHtmlTpl: function() {
  27941. var alignType = ["left", "center", "right"],
  27942. COUNT = 9,
  27943. tempClassName = null,
  27944. tempIndex = -1,
  27945. tmpl = [];
  27946. for (var i = 0; i < COUNT; i++) {
  27947. tempClassName = this.selectedIndex === i
  27948. ? ' class="edui-cellalign-selected" '
  27949. : "";
  27950. tempIndex = i % 3;
  27951. tempIndex === 0 && tmpl.push("<tr>");
  27952. tmpl.push(
  27953. '<td index="' +
  27954. i +
  27955. '" ' +
  27956. tempClassName +
  27957. ' stateful><div class="edui-icon edui-' +
  27958. alignType[tempIndex] +
  27959. '"></div></td>'
  27960. );
  27961. tempIndex === 2 && tmpl.push("</tr>");
  27962. }
  27963. return (
  27964. '<div id="##" class="edui-cellalignpicker %%">' +
  27965. '<div class="edui-cellalignpicker-body">' +
  27966. '<table onclick="$$._onClick(event);">' +
  27967. tmpl.join("") +
  27968. "</table>" +
  27969. "</div>" +
  27970. "</div>"
  27971. );
  27972. },
  27973. getStateDom: function() {
  27974. return this.target;
  27975. },
  27976. _onClick: function(evt) {
  27977. var target = evt.target || evt.srcElement;
  27978. if (/icon/.test(target.className)) {
  27979. this.items[target.parentNode.getAttribute("index")].onclick();
  27980. Popup.postHide(evt);
  27981. }
  27982. },
  27983. _UIBase_render: UIBase.prototype.render
  27984. };
  27985. utils.inherits(CellAlignPicker, UIBase);
  27986. utils.extend(CellAlignPicker.prototype, Stateful, true);
  27987. })();
  27988. // ui/pastepicker.js
  27989. ///import core
  27990. ///import uicore
  27991. (function() {
  27992. var utils = baidu.editor.utils,
  27993. Stateful = baidu.editor.ui.Stateful,
  27994. uiUtils = baidu.editor.ui.uiUtils,
  27995. UIBase = baidu.editor.ui.UIBase;
  27996. var PastePicker = (baidu.editor.ui.PastePicker = function(options) {
  27997. this.initOptions(options);
  27998. this.initPastePicker();
  27999. });
  28000. PastePicker.prototype = {
  28001. initPastePicker: function() {
  28002. this.initUIBase();
  28003. this.Stateful_init();
  28004. },
  28005. getHtmlTpl: function() {
  28006. return (
  28007. '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  28008. '<div class="edui-pastecontainer">' +
  28009. '<div class="edui-title">' +
  28010. this.editor.getLang("pasteOpt") +
  28011. "</div>" +
  28012. '<div class="edui-button">' +
  28013. '<div title="' +
  28014. this.editor.getLang("pasteSourceFormat") +
  28015. '" onclick="$$.format(false)" stateful>' +
  28016. '<div class="edui-richtxticon"></div></div>' +
  28017. '<div title="' +
  28018. this.editor.getLang("tagFormat") +
  28019. '" onclick="$$.format(2)" stateful>' +
  28020. '<div class="edui-tagicon"></div></div>' +
  28021. '<div title="' +
  28022. this.editor.getLang("pasteTextFormat") +
  28023. '" onclick="$$.format(true)" stateful>' +
  28024. '<div class="edui-plaintxticon"></div></div>' +
  28025. "</div>" +
  28026. "</div>" +
  28027. "</div>"
  28028. );
  28029. },
  28030. getStateDom: function() {
  28031. return this.target;
  28032. },
  28033. format: function(param) {
  28034. this.editor.ui._isTransfer = true;
  28035. this.editor.fireEvent("pasteTransfer", param);
  28036. },
  28037. _onClick: function(cur) {
  28038. var node = domUtils.getNextDomNode(cur),
  28039. screenHt = uiUtils.getViewportRect().height,
  28040. subPop = uiUtils.getClientRect(node);
  28041. if (subPop.top + subPop.height > screenHt)
  28042. node.style.top = -subPop.height - cur.offsetHeight + "px";
  28043. else node.style.top = "";
  28044. if (/hidden/gi.test(domUtils.getComputedStyle(node, "visibility"))) {
  28045. node.style.visibility = "visible";
  28046. domUtils.addClass(cur, "edui-state-opened");
  28047. } else {
  28048. node.style.visibility = "hidden";
  28049. domUtils.removeClasses(cur, "edui-state-opened");
  28050. }
  28051. },
  28052. _UIBase_render: UIBase.prototype.render
  28053. };
  28054. utils.inherits(PastePicker, UIBase);
  28055. utils.extend(PastePicker.prototype, Stateful, true);
  28056. })();
  28057. // ui/toolbar.js
  28058. (function() {
  28059. var utils = baidu.editor.utils,
  28060. uiUtils = baidu.editor.ui.uiUtils,
  28061. UIBase = baidu.editor.ui.UIBase,
  28062. Toolbar = (baidu.editor.ui.Toolbar = function(options) {
  28063. this.initOptions(options);
  28064. this.initToolbar();
  28065. });
  28066. Toolbar.prototype = {
  28067. items: null,
  28068. initToolbar: function() {
  28069. this.items = this.items || [];
  28070. this.initUIBase();
  28071. },
  28072. add: function(item, index) {
  28073. if (index === undefined) {
  28074. this.items.push(item);
  28075. } else {
  28076. this.items.splice(index, 0, item);
  28077. }
  28078. },
  28079. getHtmlTpl: function() {
  28080. var buff = [];
  28081. for (var i = 0; i < this.items.length; i++) {
  28082. buff[i] = this.items[i].renderHtml();
  28083. }
  28084. return (
  28085. '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  28086. buff.join("") +
  28087. "</div>"
  28088. );
  28089. },
  28090. postRender: function() {
  28091. var box = this.getDom();
  28092. for (var i = 0; i < this.items.length; i++) {
  28093. this.items[i].postRender();
  28094. }
  28095. uiUtils.makeUnselectable(box);
  28096. },
  28097. _onMouseDown: function(e) {
  28098. var target = e.target || e.srcElement,
  28099. tagName = target && target.tagName && target.tagName.toLowerCase();
  28100. if (tagName == "input" || tagName == "object" || tagName == "object") {
  28101. return false;
  28102. }
  28103. }
  28104. };
  28105. utils.inherits(Toolbar, UIBase);
  28106. })();
  28107. // ui/menu.js
  28108. ///import core
  28109. ///import uicore
  28110. ///import ui\popup.js
  28111. ///import ui\stateful.js
  28112. (function() {
  28113. var utils = baidu.editor.utils,
  28114. domUtils = baidu.editor.dom.domUtils,
  28115. uiUtils = baidu.editor.ui.uiUtils,
  28116. UIBase = baidu.editor.ui.UIBase,
  28117. Popup = baidu.editor.ui.Popup,
  28118. Stateful = baidu.editor.ui.Stateful,
  28119. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  28120. Menu = (baidu.editor.ui.Menu = function(options) {
  28121. this.initOptions(options);
  28122. this.initMenu();
  28123. });
  28124. var menuSeparator = {
  28125. renderHtml: function() {
  28126. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  28127. },
  28128. postRender: function() {},
  28129. queryAutoHide: function() {
  28130. return true;
  28131. }
  28132. };
  28133. Menu.prototype = {
  28134. items: null,
  28135. uiName: "menu",
  28136. initMenu: function() {
  28137. this.items = this.items || [];
  28138. this.initPopup();
  28139. this.initItems();
  28140. },
  28141. initItems: function() {
  28142. for (var i = 0; i < this.items.length; i++) {
  28143. var item = this.items[i];
  28144. if (item == "-") {
  28145. this.items[i] = this.getSeparator();
  28146. } else if (!(item instanceof MenuItem)) {
  28147. item.editor = this.editor;
  28148. item.theme = this.editor.options.theme;
  28149. this.items[i] = this.createItem(item);
  28150. }
  28151. }
  28152. },
  28153. getSeparator: function() {
  28154. return menuSeparator;
  28155. },
  28156. createItem: function(item) {
  28157. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28158. item.menu = this;
  28159. return new MenuItem(item);
  28160. },
  28161. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  28162. getContentHtmlTpl: function() {
  28163. if (this.items.length == 0) {
  28164. return this._Popup_getContentHtmlTpl();
  28165. }
  28166. var buff = [];
  28167. for (var i = 0; i < this.items.length; i++) {
  28168. var item = this.items[i];
  28169. buff[i] = item.renderHtml();
  28170. }
  28171. return '<div class="%%-body">' + buff.join("") + "</div>";
  28172. },
  28173. _Popup_postRender: Popup.prototype.postRender,
  28174. postRender: function() {
  28175. var me = this;
  28176. for (var i = 0; i < this.items.length; i++) {
  28177. var item = this.items[i];
  28178. item.ownerMenu = this;
  28179. item.postRender();
  28180. }
  28181. domUtils.on(this.getDom(), "mouseover", function(evt) {
  28182. evt = evt || event;
  28183. var rel = evt.relatedTarget || evt.fromElement;
  28184. var el = me.getDom();
  28185. if (!uiUtils.contains(el, rel) && el !== rel) {
  28186. me.fireEvent("over");
  28187. }
  28188. });
  28189. this._Popup_postRender();
  28190. },
  28191. queryAutoHide: function(el) {
  28192. if (el) {
  28193. if (uiUtils.contains(this.getDom(), el)) {
  28194. return false;
  28195. }
  28196. for (var i = 0; i < this.items.length; i++) {
  28197. var item = this.items[i];
  28198. if (item.queryAutoHide(el) === false) {
  28199. return false;
  28200. }
  28201. }
  28202. }
  28203. },
  28204. clearItems: function() {
  28205. for (var i = 0; i < this.items.length; i++) {
  28206. var item = this.items[i];
  28207. clearTimeout(item._showingTimer);
  28208. clearTimeout(item._closingTimer);
  28209. if (item.subMenu) {
  28210. item.subMenu.destroy();
  28211. }
  28212. }
  28213. this.items = [];
  28214. },
  28215. destroy: function() {
  28216. if (this.getDom()) {
  28217. domUtils.remove(this.getDom());
  28218. }
  28219. this.clearItems();
  28220. },
  28221. dispose: function() {
  28222. this.destroy();
  28223. }
  28224. };
  28225. utils.inherits(Menu, Popup);
  28226. /**
  28227. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28228. * @type {Function}
  28229. */
  28230. var MenuItem = (baidu.editor.ui.MenuItem = function(options) {
  28231. this.initOptions(options);
  28232. this.initUIBase();
  28233. this.Stateful_init();
  28234. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  28235. if (options.className && options.className.indexOf("aligntd") != -1) {
  28236. var me = this;
  28237. //获取单元格对齐初始状态
  28238. this.subMenu.selected = this.editor.queryCommandValue("cellalignment");
  28239. this.subMenu = new Popup({
  28240. content: new CellAlignPicker(this.subMenu),
  28241. parentMenu: me,
  28242. editor: me.editor,
  28243. destroy: function() {
  28244. if (this.getDom()) {
  28245. domUtils.remove(this.getDom());
  28246. }
  28247. }
  28248. });
  28249. this.subMenu.addListener("postRenderAfter", function() {
  28250. domUtils.on(this.getDom(), "mouseover", function() {
  28251. me.addState("opened");
  28252. });
  28253. });
  28254. } else {
  28255. this.subMenu = new Menu(this.subMenu);
  28256. }
  28257. }
  28258. });
  28259. MenuItem.prototype = {
  28260. label: "",
  28261. subMenu: null,
  28262. ownerMenu: null,
  28263. uiName: "menuitem",
  28264. alwalysHoverable: true,
  28265. getHtmlTpl: function() {
  28266. return (
  28267. '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  28268. '<div class="%%-body">' +
  28269. this.renderLabelHtml() +
  28270. "</div>" +
  28271. "</div>"
  28272. );
  28273. },
  28274. postRender: function() {
  28275. var me = this;
  28276. this.addListener("over", function() {
  28277. me.ownerMenu.fireEvent("submenuover", me);
  28278. if (me.subMenu) {
  28279. me.delayShowSubMenu();
  28280. }
  28281. });
  28282. if (this.subMenu) {
  28283. this.getDom().className += " edui-hassubmenu";
  28284. this.subMenu.render();
  28285. this.addListener("out", function() {
  28286. me.delayHideSubMenu();
  28287. });
  28288. this.subMenu.addListener("over", function() {
  28289. clearTimeout(me._closingTimer);
  28290. me._closingTimer = null;
  28291. me.addState("opened");
  28292. });
  28293. this.ownerMenu.addListener("hide", function() {
  28294. me.hideSubMenu();
  28295. });
  28296. this.ownerMenu.addListener("submenuover", function(t, subMenu) {
  28297. if (subMenu !== me) {
  28298. me.delayHideSubMenu();
  28299. }
  28300. });
  28301. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  28302. this.subMenu.queryAutoHide = function(el) {
  28303. if (el && uiUtils.contains(me.getDom(), el)) {
  28304. return false;
  28305. }
  28306. return this._bakQueryAutoHide(el);
  28307. };
  28308. }
  28309. this.getDom().style.tabIndex = "-1";
  28310. uiUtils.makeUnselectable(this.getDom());
  28311. this.Stateful_postRender();
  28312. },
  28313. delayShowSubMenu: function() {
  28314. var me = this;
  28315. if (!me.isDisabled()) {
  28316. me.addState("opened");
  28317. clearTimeout(me._showingTimer);
  28318. clearTimeout(me._closingTimer);
  28319. me._closingTimer = null;
  28320. me._showingTimer = setTimeout(function() {
  28321. me.showSubMenu();
  28322. }, 250);
  28323. }
  28324. },
  28325. delayHideSubMenu: function() {
  28326. var me = this;
  28327. if (!me.isDisabled()) {
  28328. me.removeState("opened");
  28329. clearTimeout(me._showingTimer);
  28330. if (!me._closingTimer) {
  28331. me._closingTimer = setTimeout(function() {
  28332. if (!me.hasState("opened")) {
  28333. me.hideSubMenu();
  28334. }
  28335. me._closingTimer = null;
  28336. }, 400);
  28337. }
  28338. }
  28339. },
  28340. renderLabelHtml: function() {
  28341. return (
  28342. '<div class="edui-arrow"></div>' +
  28343. '<div class="edui-box edui-icon"></div>' +
  28344. '<div class="edui-box edui-label %%-label">' +
  28345. (this.label || "") +
  28346. "</div>"
  28347. );
  28348. },
  28349. getStateDom: function() {
  28350. return this.getDom();
  28351. },
  28352. queryAutoHide: function(el) {
  28353. if (this.subMenu && this.hasState("opened")) {
  28354. return this.subMenu.queryAutoHide(el);
  28355. }
  28356. },
  28357. _onClick: function(event, this_) {
  28358. if (this.hasState("disabled")) return;
  28359. if (this.fireEvent("click", event, this_) !== false) {
  28360. if (this.subMenu) {
  28361. this.showSubMenu();
  28362. } else {
  28363. Popup.postHide(event);
  28364. }
  28365. }
  28366. },
  28367. showSubMenu: function() {
  28368. var rect = uiUtils.getClientRect(this.getDom());
  28369. rect.right -= 5;
  28370. rect.left += 2;
  28371. rect.width -= 7;
  28372. rect.top -= 4;
  28373. rect.bottom += 4;
  28374. rect.height += 8;
  28375. this.subMenu.showAnchorRect(rect, true, true);
  28376. },
  28377. hideSubMenu: function() {
  28378. this.subMenu.hide();
  28379. }
  28380. };
  28381. utils.inherits(MenuItem, UIBase);
  28382. utils.extend(MenuItem.prototype, Stateful, true);
  28383. })();
  28384. // ui/combox.js
  28385. ///import core
  28386. ///import uicore
  28387. ///import ui/menu.js
  28388. ///import ui/splitbutton.js
  28389. (function() {
  28390. // todo: menu和item提成通用list
  28391. var utils = baidu.editor.utils,
  28392. uiUtils = baidu.editor.ui.uiUtils,
  28393. Menu = baidu.editor.ui.Menu,
  28394. SplitButton = baidu.editor.ui.SplitButton,
  28395. Combox = (baidu.editor.ui.Combox = function(options) {
  28396. this.initOptions(options);
  28397. this.initCombox();
  28398. });
  28399. Combox.prototype = {
  28400. uiName: "combox",
  28401. onbuttonclick: function() {
  28402. this.showPopup();
  28403. },
  28404. initCombox: function() {
  28405. var me = this;
  28406. this.items = this.items || [];
  28407. for (var i = 0; i < this.items.length; i++) {
  28408. var item = this.items[i];
  28409. item.uiName = "listitem";
  28410. item.index = i;
  28411. item.onclick = function() {
  28412. me.selectByIndex(this.index);
  28413. };
  28414. }
  28415. this.popup = new Menu({
  28416. items: this.items,
  28417. uiName: "list",
  28418. editor: this.editor,
  28419. captureWheel: true,
  28420. combox: this
  28421. });
  28422. this.initSplitButton();
  28423. },
  28424. _SplitButton_postRender: SplitButton.prototype.postRender,
  28425. postRender: function() {
  28426. this._SplitButton_postRender();
  28427. this.setLabel(this.label || "");
  28428. this.setValue(this.initValue || "");
  28429. },
  28430. showPopup: function() {
  28431. var rect = uiUtils.getClientRect(this.getDom());
  28432. rect.top += 1;
  28433. rect.bottom -= 1;
  28434. rect.height -= 2;
  28435. this.popup.showAnchorRect(rect);
  28436. },
  28437. getValue: function() {
  28438. return this.value;
  28439. },
  28440. setValue: function(value) {
  28441. var index = this.indexByValue(value);
  28442. if (index != -1) {
  28443. this.selectedIndex = index;
  28444. this.setLabel(this.items[index].label);
  28445. this.value = this.items[index].value;
  28446. } else {
  28447. this.selectedIndex = -1;
  28448. this.setLabel(this.getLabelForUnknowValue(value));
  28449. this.value = value;
  28450. }
  28451. },
  28452. setLabel: function(label) {
  28453. this.getDom("button_body").innerHTML = label;
  28454. this.label = label;
  28455. },
  28456. getLabelForUnknowValue: function(value) {
  28457. return value;
  28458. },
  28459. indexByValue: function(value) {
  28460. for (var i = 0; i < this.items.length; i++) {
  28461. if (value == this.items[i].value) {
  28462. return i;
  28463. }
  28464. }
  28465. return -1;
  28466. },
  28467. getItem: function(index) {
  28468. return this.items[index];
  28469. },
  28470. selectByIndex: function(index) {
  28471. if (
  28472. index < this.items.length &&
  28473. this.fireEvent("select", index) !== false
  28474. ) {
  28475. this.selectedIndex = index;
  28476. this.value = this.items[index].value;
  28477. this.setLabel(this.items[index].label);
  28478. }
  28479. }
  28480. };
  28481. utils.inherits(Combox, SplitButton);
  28482. })();
  28483. // ui/dialog.js
  28484. ///import core
  28485. ///import uicore
  28486. ///import ui/mask.js
  28487. ///import ui/button.js
  28488. (function() {
  28489. var utils = baidu.editor.utils,
  28490. domUtils = baidu.editor.dom.domUtils,
  28491. uiUtils = baidu.editor.ui.uiUtils,
  28492. Mask = baidu.editor.ui.Mask,
  28493. UIBase = baidu.editor.ui.UIBase,
  28494. Button = baidu.editor.ui.Button,
  28495. Dialog = (baidu.editor.ui.Dialog = function(options) {
  28496. if (options.name) {
  28497. var name = options.name;
  28498. var cssRules = options.cssRules;
  28499. if (!options.className) {
  28500. options.className = "edui-for-" + name;
  28501. }
  28502. if (cssRules) {
  28503. options.cssRules =
  28504. ".edui-for-" + name + " .edui-dialog-content {" + cssRules + "}";
  28505. }
  28506. }
  28507. this.initOptions(
  28508. utils.extend(
  28509. {
  28510. autoReset: true,
  28511. draggable: true,
  28512. onok: function() {},
  28513. oncancel: function() {},
  28514. onclose: function(t, ok) {
  28515. return ok ? this.onok() : this.oncancel();
  28516. },
  28517. //是否控制dialog中的scroll事件, 默认为不阻止
  28518. holdScroll: false
  28519. },
  28520. options
  28521. )
  28522. );
  28523. this.initDialog();
  28524. });
  28525. var modalMask;
  28526. var dragMask;
  28527. var activeDialog;
  28528. Dialog.prototype = {
  28529. draggable: false,
  28530. uiName: "dialog",
  28531. initDialog: function() {
  28532. var me = this,
  28533. theme = this.editor.options.theme;
  28534. if (this.cssRules) {
  28535. this.cssRules = ".edui-" + theme + " " + this.cssRules;
  28536. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  28537. }
  28538. this.initUIBase();
  28539. this.modalMask =
  28540. modalMask ||
  28541. (modalMask = new Mask({
  28542. className: "edui-dialog-modalmask",
  28543. theme: theme,
  28544. onclick: function() {
  28545. activeDialog && activeDialog.close(false);
  28546. }
  28547. }));
  28548. this.dragMask =
  28549. dragMask ||
  28550. (dragMask = new Mask({
  28551. className: "edui-dialog-dragmask",
  28552. theme: theme
  28553. }));
  28554. this.closeButton = new Button({
  28555. className: "edui-dialog-closebutton",
  28556. title: me.closeDialog,
  28557. theme: theme,
  28558. onclick: function() {
  28559. me.close(false);
  28560. }
  28561. });
  28562. this.fullscreen && this.initResizeEvent();
  28563. if (this.buttons) {
  28564. for (var i = 0; i < this.buttons.length; i++) {
  28565. if (!(this.buttons[i] instanceof Button)) {
  28566. this.buttons[i] = new Button(
  28567. utils.extend(
  28568. this.buttons[i],
  28569. {
  28570. editor: this.editor
  28571. },
  28572. true
  28573. )
  28574. );
  28575. }
  28576. }
  28577. }
  28578. },
  28579. initResizeEvent: function() {
  28580. var me = this;
  28581. domUtils.on(window, "resize", function() {
  28582. if (me._hidden || me._hidden === undefined) {
  28583. return;
  28584. }
  28585. if (me.__resizeTimer) {
  28586. window.clearTimeout(me.__resizeTimer);
  28587. }
  28588. me.__resizeTimer = window.setTimeout(function() {
  28589. me.__resizeTimer = null;
  28590. var dialogWrapNode = me.getDom(),
  28591. contentNode = me.getDom("content"),
  28592. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28593. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  28594. vpRect = uiUtils.getViewportRect();
  28595. contentNode.style.width =
  28596. vpRect.width - wrapRect.width + contentRect.width + "px";
  28597. contentNode.style.height =
  28598. vpRect.height - wrapRect.height + contentRect.height + "px";
  28599. dialogWrapNode.style.width = vpRect.width + "px";
  28600. dialogWrapNode.style.height = vpRect.height + "px";
  28601. me.fireEvent("resize");
  28602. }, 100);
  28603. });
  28604. },
  28605. fitSize: function() {
  28606. var popBodyEl = this.getDom("body");
  28607. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  28608. // uiUtils.removeStyle(popBodyEl, 'width');
  28609. // uiUtils.removeStyle(popBodyEl, 'height');
  28610. // }
  28611. var size = this.mesureSize();
  28612. popBodyEl.style.width = size.width + "px";
  28613. popBodyEl.style.height = size.height + "px";
  28614. return size;
  28615. },
  28616. safeSetOffset: function(offset) {
  28617. var me = this;
  28618. var el = me.getDom();
  28619. var vpRect = uiUtils.getViewportRect();
  28620. var rect = uiUtils.getClientRect(el);
  28621. var left = offset.left;
  28622. if (left + rect.width > vpRect.right) {
  28623. left = vpRect.right - rect.width;
  28624. }
  28625. var top = offset.top;
  28626. if (top + rect.height > vpRect.bottom) {
  28627. top = vpRect.bottom - rect.height;
  28628. }
  28629. el.style.left = Math.max(left, 0) + "px";
  28630. el.style.top = Math.max(top, 0) + "px";
  28631. },
  28632. showAtCenter: function() {
  28633. var vpRect = uiUtils.getViewportRect();
  28634. if (!this.fullscreen) {
  28635. this.getDom().style.display = "";
  28636. var popSize = this.fitSize();
  28637. var titleHeight = this.getDom("titlebar").offsetHeight | 0;
  28638. var left = vpRect.width / 2 - popSize.width / 2;
  28639. var top =
  28640. vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  28641. var popEl = this.getDom();
  28642. this.safeSetOffset({
  28643. left: Math.max(left | 0, 0),
  28644. top: Math.max(top | 0, 0)
  28645. });
  28646. if (!domUtils.hasClass(popEl, "edui-state-centered")) {
  28647. popEl.className += " edui-state-centered";
  28648. }
  28649. } else {
  28650. var dialogWrapNode = this.getDom(),
  28651. contentNode = this.getDom("content");
  28652. dialogWrapNode.style.display = "block";
  28653. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28654. contentRect = UE.ui.uiUtils.getClientRect(contentNode);
  28655. dialogWrapNode.style.left = "-100000px";
  28656. contentNode.style.width =
  28657. vpRect.width - wrapRect.width + contentRect.width + "px";
  28658. contentNode.style.height =
  28659. vpRect.height - wrapRect.height + contentRect.height + "px";
  28660. dialogWrapNode.style.width = vpRect.width + "px";
  28661. dialogWrapNode.style.height = vpRect.height + "px";
  28662. dialogWrapNode.style.left = 0;
  28663. //保存环境的overflow值
  28664. this._originalContext = {
  28665. html: {
  28666. overflowX: document.documentElement.style.overflowX,
  28667. overflowY: document.documentElement.style.overflowY
  28668. },
  28669. body: {
  28670. overflowX: document.body.style.overflowX,
  28671. overflowY: document.body.style.overflowY
  28672. }
  28673. };
  28674. document.documentElement.style.overflowX = "hidden";
  28675. document.documentElement.style.overflowY = "hidden";
  28676. document.body.style.overflowX = "hidden";
  28677. document.body.style.overflowY = "hidden";
  28678. }
  28679. this._show();
  28680. },
  28681. getContentHtml: function() {
  28682. var contentHtml = "";
  28683. if (typeof this.content == "string") {
  28684. contentHtml = this.content;
  28685. } else if (this.iframeUrl) {
  28686. contentHtml =
  28687. '<span id="' +
  28688. this.id +
  28689. '_contmask" class="dialogcontmask"></span><iframe id="' +
  28690. this.id +
  28691. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' +
  28692. this.iframeUrl +
  28693. '"></iframe>';
  28694. }
  28695. return contentHtml;
  28696. },
  28697. getHtmlTpl: function() {
  28698. var footHtml = "";
  28699. if (this.buttons) {
  28700. var buff = [];
  28701. for (var i = 0; i < this.buttons.length; i++) {
  28702. buff[i] = this.buttons[i].renderHtml();
  28703. }
  28704. footHtml =
  28705. '<div class="%%-foot">' +
  28706. '<div id="##_buttons" class="%%-buttons">' +
  28707. buff.join("") +
  28708. "</div>" +
  28709. "</div>";
  28710. }
  28711. return (
  28712. '<div id="##" class="%%"><div ' +
  28713. (!this.fullscreen
  28714. ? 'class="%%"'
  28715. : 'class="%%-wrap edui-dialog-fullscreen-flag"') +
  28716. '><div id="##_body" class="%%-body">' +
  28717. '<div class="%%-shadow"></div>' +
  28718. '<div id="##_titlebar" class="%%-titlebar">' +
  28719. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  28720. '<span class="%%-caption">' +
  28721. (this.title || "") +
  28722. "</span>" +
  28723. "</div>" +
  28724. this.closeButton.renderHtml() +
  28725. "</div>" +
  28726. '<div id="##_content" class="%%-content">' +
  28727. (this.autoReset ? "" : this.getContentHtml()) +
  28728. "</div>" +
  28729. footHtml +
  28730. "</div></div></div>"
  28731. );
  28732. },
  28733. postRender: function() {
  28734. // todo: 保持居中/记住上次关闭位置选项
  28735. if (!this.modalMask.getDom()) {
  28736. this.modalMask.render();
  28737. this.modalMask.hide();
  28738. }
  28739. if (!this.dragMask.getDom()) {
  28740. this.dragMask.render();
  28741. this.dragMask.hide();
  28742. }
  28743. var me = this;
  28744. this.addListener("show", function() {
  28745. me.modalMask.show(this.getDom().style.zIndex - 2);
  28746. });
  28747. this.addListener("hide", function() {
  28748. me.modalMask.hide();
  28749. });
  28750. if (this.buttons) {
  28751. for (var i = 0; i < this.buttons.length; i++) {
  28752. this.buttons[i].postRender();
  28753. }
  28754. }
  28755. domUtils.on(window, "resize", function() {
  28756. setTimeout(function() {
  28757. if (!me.isHidden()) {
  28758. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  28759. }
  28760. });
  28761. });
  28762. //hold住scroll事件,防止dialog的滚动影响页面
  28763. // if( this.holdScroll ) {
  28764. //
  28765. // if( !me.iframeUrl ) {
  28766. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28767. // domUtils.preventDefault(e);
  28768. // } );
  28769. // } else {
  28770. // me.addListener('dialogafterreset', function(){
  28771. // window.setTimeout(function(){
  28772. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  28773. //
  28774. // if( browser.ie ) {
  28775. //
  28776. // var timer = window.setInterval(function(){
  28777. //
  28778. // if( iframeWindow.document && iframeWindow.document.body ) {
  28779. // window.clearInterval( timer );
  28780. // timer = null;
  28781. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28782. // domUtils.preventDefault(e);
  28783. // } );
  28784. // }
  28785. //
  28786. // }, 100);
  28787. //
  28788. // } else {
  28789. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28790. // domUtils.preventDefault(e);
  28791. // } );
  28792. // }
  28793. //
  28794. // }, 1);
  28795. // });
  28796. // }
  28797. //
  28798. // }
  28799. this._hide();
  28800. },
  28801. mesureSize: function() {
  28802. var body = this.getDom("body");
  28803. var width = uiUtils.getClientRect(this.getDom("content")).width;
  28804. var dialogBodyStyle = body.style;
  28805. dialogBodyStyle.width = width;
  28806. return uiUtils.getClientRect(body);
  28807. },
  28808. _onTitlebarMouseDown: function(evt, el) {
  28809. if (this.draggable) {
  28810. var rect;
  28811. var vpRect = uiUtils.getViewportRect();
  28812. var me = this;
  28813. uiUtils.startDrag(evt, {
  28814. ondragstart: function() {
  28815. rect = uiUtils.getClientRect(me.getDom());
  28816. me.getDom("contmask").style.visibility = "visible";
  28817. me.dragMask.show(me.getDom().style.zIndex - 1);
  28818. },
  28819. ondragmove: function(x, y) {
  28820. var left = rect.left + x;
  28821. var top = rect.top + y;
  28822. me.safeSetOffset({
  28823. left: left,
  28824. top: top
  28825. });
  28826. },
  28827. ondragstop: function() {
  28828. me.getDom("contmask").style.visibility = "hidden";
  28829. domUtils.removeClasses(me.getDom(), ["edui-state-centered"]);
  28830. me.dragMask.hide();
  28831. }
  28832. });
  28833. }
  28834. },
  28835. reset: function() {
  28836. this.getDom("content").innerHTML = this.getContentHtml();
  28837. this.fireEvent("dialogafterreset");
  28838. },
  28839. _show: function() {
  28840. if (this._hidden) {
  28841. this.getDom().style.display = "";
  28842. //要高过编辑器的zindxe
  28843. this.editor.container.style.zIndex &&
  28844. (this.getDom().style.zIndex =
  28845. this.editor.container.style.zIndex * 1 + 10);
  28846. this._hidden = false;
  28847. this.fireEvent("show");
  28848. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  28849. this.getDom().style.zIndex - 4;
  28850. }
  28851. },
  28852. isHidden: function() {
  28853. return this._hidden;
  28854. },
  28855. _hide: function() {
  28856. if (!this._hidden) {
  28857. var wrapNode = this.getDom();
  28858. wrapNode.style.display = "none";
  28859. wrapNode.style.zIndex = "";
  28860. wrapNode.style.width = "";
  28861. wrapNode.style.height = "";
  28862. this._hidden = true;
  28863. this.fireEvent("hide");
  28864. }
  28865. },
  28866. open: function() {
  28867. if (this.autoReset) {
  28868. //有可能还没有渲染
  28869. try {
  28870. this.reset();
  28871. } catch (e) {
  28872. this.render();
  28873. this.open();
  28874. }
  28875. }
  28876. this.showAtCenter();
  28877. if (this.iframeUrl) {
  28878. try {
  28879. this.getDom("iframe").focus();
  28880. } catch (ex) {}
  28881. }
  28882. activeDialog = this;
  28883. },
  28884. _onCloseButtonClick: function(evt, el) {
  28885. this.close(false);
  28886. },
  28887. close: function(ok) {
  28888. if (this.fireEvent("close", ok) !== false) {
  28889. //还原环境
  28890. if (this.fullscreen) {
  28891. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  28892. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  28893. document.body.style.overflowX = this._originalContext.body.overflowX;
  28894. document.body.style.overflowY = this._originalContext.body.overflowY;
  28895. delete this._originalContext;
  28896. }
  28897. this._hide();
  28898. //销毁content
  28899. var content = this.getDom("content");
  28900. var iframe = this.getDom("iframe");
  28901. if (content && iframe) {
  28902. var doc = iframe.contentDocument || iframe.contentWindow.document;
  28903. doc && (doc.body.innerHTML = "");
  28904. domUtils.remove(content);
  28905. }
  28906. }
  28907. }
  28908. };
  28909. utils.inherits(Dialog, UIBase);
  28910. })();
  28911. // ui/menubutton.js
  28912. ///import core
  28913. ///import uicore
  28914. ///import ui/menu.js
  28915. ///import ui/splitbutton.js
  28916. (function() {
  28917. var utils = baidu.editor.utils,
  28918. Menu = baidu.editor.ui.Menu,
  28919. SplitButton = baidu.editor.ui.SplitButton,
  28920. MenuButton = (baidu.editor.ui.MenuButton = function(options) {
  28921. this.initOptions(options);
  28922. this.initMenuButton();
  28923. });
  28924. MenuButton.prototype = {
  28925. initMenuButton: function() {
  28926. var me = this;
  28927. this.uiName = "menubutton";
  28928. this.popup = new Menu({
  28929. items: me.items,
  28930. className: me.className,
  28931. editor: me.editor
  28932. });
  28933. this.popup.addListener("show", function() {
  28934. var list = this;
  28935. for (var i = 0; i < list.items.length; i++) {
  28936. list.items[i].removeState("checked");
  28937. if (list.items[i].value == me._value) {
  28938. list.items[i].addState("checked");
  28939. this.value = me._value;
  28940. }
  28941. }
  28942. });
  28943. this.initSplitButton();
  28944. },
  28945. setValue: function(value) {
  28946. this._value = value;
  28947. }
  28948. };
  28949. utils.inherits(MenuButton, SplitButton);
  28950. })();
  28951. // ui/multiMenu.js
  28952. ///import core
  28953. ///import uicore
  28954. ///commands 表情
  28955. (function() {
  28956. var utils = baidu.editor.utils,
  28957. Popup = baidu.editor.ui.Popup,
  28958. SplitButton = baidu.editor.ui.SplitButton,
  28959. MultiMenuPop = (baidu.editor.ui.MultiMenuPop = function(options) {
  28960. this.initOptions(options);
  28961. this.initMultiMenu();
  28962. });
  28963. MultiMenuPop.prototype = {
  28964. initMultiMenu: function() {
  28965. var me = this;
  28966. this.popup = new Popup({
  28967. content: "",
  28968. editor: me.editor,
  28969. iframe_rendered: false,
  28970. onshow: function() {
  28971. if (!this.iframe_rendered) {
  28972. this.iframe_rendered = true;
  28973. this.getDom("content").innerHTML =
  28974. '<iframe id="' +
  28975. me.id +
  28976. '_iframe" src="' +
  28977. me.iframeUrl +
  28978. '" frameborder="0"></iframe>';
  28979. me.editor.container.style.zIndex &&
  28980. (this.getDom().style.zIndex =
  28981. me.editor.container.style.zIndex * 1 + 1);
  28982. }
  28983. }
  28984. // canSideUp:false,
  28985. // canSideLeft:false
  28986. });
  28987. this.onbuttonclick = function() {
  28988. this.showPopup();
  28989. };
  28990. this.initSplitButton();
  28991. }
  28992. };
  28993. utils.inherits(MultiMenuPop, SplitButton);
  28994. })();
  28995. // ui/shortcutmenu.js
  28996. (function() {
  28997. var UI = baidu.editor.ui,
  28998. UIBase = UI.UIBase,
  28999. uiUtils = UI.uiUtils,
  29000. utils = baidu.editor.utils,
  29001. domUtils = baidu.editor.dom.domUtils;
  29002. var allMenus = [], //存储所有快捷菜单
  29003. timeID,
  29004. isSubMenuShow = false; //是否有子pop显示
  29005. var ShortCutMenu = (UI.ShortCutMenu = function(options) {
  29006. this.initOptions(options);
  29007. this.initShortCutMenu();
  29008. });
  29009. ShortCutMenu.postHide = hideAllMenu;
  29010. ShortCutMenu.prototype = {
  29011. isHidden: true,
  29012. SPACE: 5,
  29013. initShortCutMenu: function() {
  29014. this.items = this.items || [];
  29015. this.initUIBase();
  29016. this.initItems();
  29017. this.initEvent();
  29018. allMenus.push(this);
  29019. },
  29020. initEvent: function() {
  29021. var me = this,
  29022. doc = me.editor.document;
  29023. domUtils.on(doc, "mousemove", function(e) {
  29024. if (me.isHidden === false) {
  29025. //有pop显示就不隐藏快捷菜单
  29026. if (me.getSubMenuMark() || me.eventType == "contextmenu") return;
  29027. var flag = true,
  29028. el = me.getDom(),
  29029. wt = el.offsetWidth,
  29030. ht = el.offsetHeight,
  29031. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  29032. distanceY = ht / 2, //距离中心Y标准
  29033. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  29034. y = Math.abs(e.screenY - me.top); //离中心距离纵坐标
  29035. clearTimeout(timeID);
  29036. timeID = setTimeout(function() {
  29037. if (y > 0 && y < distanceY) {
  29038. me.setOpacity(el, "1");
  29039. } else if (y > distanceY && y < distanceY + 70) {
  29040. me.setOpacity(el, "0.5");
  29041. flag = false;
  29042. } else if (y > distanceY + 70 && y < distanceY + 140) {
  29043. me.hide();
  29044. }
  29045. if (flag && x > 0 && x < distanceX) {
  29046. me.setOpacity(el, "1");
  29047. } else if (x > distanceX && x < distanceX + 70) {
  29048. me.setOpacity(el, "0.5");
  29049. } else if (x > distanceX + 70 && x < distanceX + 140) {
  29050. me.hide();
  29051. }
  29052. });
  29053. }
  29054. });
  29055. //ie\ff下 mouseout不准
  29056. if (browser.chrome) {
  29057. domUtils.on(doc, "mouseout", function(e) {
  29058. var relatedTgt = e.relatedTarget || e.toElement;
  29059. if (relatedTgt == null || relatedTgt.tagName == "HTML") {
  29060. me.hide();
  29061. }
  29062. });
  29063. }
  29064. me.editor.addListener("afterhidepop", function() {
  29065. if (!me.isHidden) {
  29066. isSubMenuShow = true;
  29067. }
  29068. });
  29069. },
  29070. initItems: function() {
  29071. if (utils.isArray(this.items)) {
  29072. for (var i = 0, len = this.items.length; i < len; i++) {
  29073. var item = this.items[i].toLowerCase();
  29074. if (UI[item]) {
  29075. this.items[i] = new UI[item](this.editor);
  29076. this.items[i].className += " edui-shortcutsubmenu ";
  29077. }
  29078. }
  29079. }
  29080. },
  29081. setOpacity: function(el, value) {
  29082. if (browser.ie && browser.version < 9) {
  29083. el.style.filter = "alpha(opacity = " + parseFloat(value) * 100 + ");";
  29084. } else {
  29085. el.style.opacity = value;
  29086. }
  29087. },
  29088. getSubMenuMark: function() {
  29089. isSubMenuShow = false;
  29090. var layerEle = uiUtils.getFixedLayer();
  29091. var list = domUtils.getElementsByTagName(layerEle, "div", function(node) {
  29092. return domUtils.hasClass(node, "edui-shortcutsubmenu edui-popup");
  29093. });
  29094. for (var i = 0, node; (node = list[i++]); ) {
  29095. if (node.style.display != "none") {
  29096. isSubMenuShow = true;
  29097. }
  29098. }
  29099. return isSubMenuShow;
  29100. },
  29101. show: function(e, hasContextmenu) {
  29102. var me = this,
  29103. offset = {},
  29104. el = this.getDom(),
  29105. fixedlayer = uiUtils.getFixedLayer();
  29106. function setPos(offset) {
  29107. if (offset.left < 0) {
  29108. offset.left = 0;
  29109. }
  29110. if (offset.top < 0) {
  29111. offset.top = 0;
  29112. }
  29113. el.style.cssText =
  29114. "position:absolute;left:" +
  29115. offset.left +
  29116. "px;top:" +
  29117. offset.top +
  29118. "px;";
  29119. }
  29120. function setPosByCxtMenu(menu) {
  29121. if (!menu.tagName) {
  29122. menu = menu.getDom();
  29123. }
  29124. offset.left = parseInt(menu.style.left);
  29125. offset.top = parseInt(menu.style.top);
  29126. offset.top -= el.offsetHeight + 15;
  29127. setPos(offset);
  29128. }
  29129. me.eventType = e.type;
  29130. el.style.cssText = "display:block;left:-9999px";
  29131. if (e.type == "contextmenu" && hasContextmenu) {
  29132. var menu = domUtils.getElementsByTagName(
  29133. fixedlayer,
  29134. "div",
  29135. "edui-contextmenu"
  29136. )[0];
  29137. if (menu) {
  29138. setPosByCxtMenu(menu);
  29139. } else {
  29140. me.editor.addListener("aftershowcontextmenu", function(type, menu) {
  29141. setPosByCxtMenu(menu);
  29142. });
  29143. }
  29144. } else {
  29145. offset = uiUtils.getViewportOffsetByEvent(e);
  29146. offset.top -= el.offsetHeight + me.SPACE;
  29147. offset.left += me.SPACE + 20;
  29148. setPos(offset);
  29149. me.setOpacity(el, 0.2);
  29150. }
  29151. me.isHidden = false;
  29152. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE;
  29153. me.top = e.screenY - el.offsetHeight / 2 - me.SPACE;
  29154. if (me.editor) {
  29155. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10;
  29156. fixedlayer.style.zIndex = el.style.zIndex - 1;
  29157. }
  29158. },
  29159. hide: function() {
  29160. if (this.getDom()) {
  29161. this.getDom().style.display = "none";
  29162. }
  29163. this.isHidden = true;
  29164. },
  29165. postRender: function() {
  29166. if (utils.isArray(this.items)) {
  29167. for (var i = 0, item; (item = this.items[i++]); ) {
  29168. item.postRender();
  29169. }
  29170. }
  29171. },
  29172. getHtmlTpl: function() {
  29173. var buff;
  29174. if (utils.isArray(this.items)) {
  29175. buff = [];
  29176. for (var i = 0; i < this.items.length; i++) {
  29177. buff[i] = this.items[i].renderHtml();
  29178. }
  29179. buff = buff.join("");
  29180. } else {
  29181. buff = this.items;
  29182. }
  29183. return (
  29184. '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  29185. buff +
  29186. "</div>"
  29187. );
  29188. }
  29189. };
  29190. utils.inherits(ShortCutMenu, UIBase);
  29191. function hideAllMenu(e) {
  29192. var tgt = e.target || e.srcElement,
  29193. cur = domUtils.findParent(
  29194. tgt,
  29195. function(node) {
  29196. return (
  29197. domUtils.hasClass(node, "edui-shortcutmenu") ||
  29198. domUtils.hasClass(node, "edui-popup")
  29199. );
  29200. },
  29201. true
  29202. );
  29203. if (!cur) {
  29204. for (var i = 0, menu; (menu = allMenus[i++]); ) {
  29205. menu.hide();
  29206. }
  29207. }
  29208. }
  29209. domUtils.on(document, "mousedown", function(e) {
  29210. hideAllMenu(e);
  29211. });
  29212. domUtils.on(window, "scroll", function(e) {
  29213. hideAllMenu(e);
  29214. });
  29215. })();
  29216. // ui/breakline.js
  29217. (function() {
  29218. var utils = baidu.editor.utils,
  29219. UIBase = baidu.editor.ui.UIBase,
  29220. Breakline = (baidu.editor.ui.Breakline = function(options) {
  29221. this.initOptions(options);
  29222. this.initSeparator();
  29223. });
  29224. Breakline.prototype = {
  29225. uiName: "Breakline",
  29226. initSeparator: function() {
  29227. this.initUIBase();
  29228. },
  29229. getHtmlTpl: function() {
  29230. return "<br/>";
  29231. }
  29232. };
  29233. utils.inherits(Breakline, UIBase);
  29234. })();
  29235. // ui/message.js
  29236. ///import core
  29237. ///import uicore
  29238. (function() {
  29239. var utils = baidu.editor.utils,
  29240. domUtils = baidu.editor.dom.domUtils,
  29241. UIBase = baidu.editor.ui.UIBase,
  29242. Message = (baidu.editor.ui.Message = function(options) {
  29243. this.initOptions(options);
  29244. this.initMessage();
  29245. });
  29246. Message.prototype = {
  29247. initMessage: function() {
  29248. this.initUIBase();
  29249. },
  29250. getHtmlTpl: function() {
  29251. return (
  29252. '<div id="##" class="edui-message %%">' +
  29253. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  29254. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  29255. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  29256. ' <div class="edui-shadow"></div>' +
  29257. ' <div id="##_content" class="edui-message-content">' +
  29258. " </div>" +
  29259. " </div>" +
  29260. "</div>"
  29261. );
  29262. },
  29263. reset: function(opt) {
  29264. var me = this;
  29265. if (!opt.keepshow) {
  29266. clearTimeout(this.timer);
  29267. me.timer = setTimeout(function() {
  29268. me.hide();
  29269. }, opt.timeout || 4000);
  29270. }
  29271. opt.content !== undefined && me.setContent(opt.content);
  29272. opt.type !== undefined && me.setType(opt.type);
  29273. me.show();
  29274. },
  29275. postRender: function() {
  29276. var me = this,
  29277. closer = this.getDom("closer");
  29278. closer &&
  29279. domUtils.on(closer, "click", function() {
  29280. me.hide();
  29281. });
  29282. },
  29283. setContent: function(content) {
  29284. this.getDom("content").innerHTML = content;
  29285. },
  29286. setType: function(type) {
  29287. type = type || "info";
  29288. var body = this.getDom("body");
  29289. body.className = body.className.replace(
  29290. /edui-message-type-[\w-]+/,
  29291. "edui-message-type-" + type
  29292. );
  29293. },
  29294. getContent: function() {
  29295. return this.getDom("content").innerHTML;
  29296. },
  29297. getType: function() {
  29298. var arr = this.getDom("body").match(/edui-message-type-([\w-]+)/);
  29299. return arr ? arr[1] : "";
  29300. },
  29301. show: function() {
  29302. this.getDom().style.display = "block";
  29303. },
  29304. hide: function() {
  29305. var dom = this.getDom();
  29306. if (dom) {
  29307. dom.style.display = "none";
  29308. dom.parentNode && dom.parentNode.removeChild(dom);
  29309. }
  29310. }
  29311. };
  29312. utils.inherits(Message, UIBase);
  29313. })();
  29314. // adapter/editorui.js
  29315. //ui跟编辑器的适配層
  29316. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  29317. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  29318. ;(function() {
  29319. var utils = baidu.editor.utils;
  29320. var editorui = baidu.editor.ui;
  29321. var _Dialog = editorui.Dialog;
  29322. editorui.buttons = {};
  29323. editorui.Dialog = function(options) {
  29324. var dialog = new _Dialog(options);
  29325. dialog.addListener("hide", function() {
  29326. if (dialog.editor) {
  29327. var editor = dialog.editor;
  29328. try {
  29329. if (browser.gecko) {
  29330. var y = editor.window.scrollY,
  29331. x = editor.window.scrollX;
  29332. editor.body.focus();
  29333. editor.window.scrollTo(x, y);
  29334. } else {
  29335. editor.focus();
  29336. }
  29337. } catch (ex) {}
  29338. }
  29339. });
  29340. return dialog;
  29341. };
  29342. var iframeUrlMap = {
  29343. anchor: "~/dialogs/anchor/anchor.html",
  29344. insertimage: "~/dialogs/image/image.html",
  29345. link: "~/dialogs/link/link.html",
  29346. spechars: "~/dialogs/spechars/spechars.html",
  29347. searchreplace: "~/dialogs/searchreplace/searchreplace.html",
  29348. map: "~/dialogs/map/map.html",
  29349. gmap: "~/dialogs/gmap/gmap.html",
  29350. insertvideo: "~/dialogs/video/video.html",
  29351. help: "~/dialogs/help/help.html",
  29352. preview: "~/dialogs/preview/preview.html",
  29353. emotion: "~/dialogs/emotion/emotion.html",
  29354. wordimage: "~/dialogs/wordimage/wordimage.html",
  29355. attachment: "~/dialogs/attachment/attachment.html",
  29356. insertframe: "~/dialogs/insertframe/insertframe.html",
  29357. edittip: "~/dialogs/table/edittip.html",
  29358. edittable: "~/dialogs/table/edittable.html",
  29359. edittd: "~/dialogs/table/edittd.html",
  29360. webapp: "~/dialogs/webapp/webapp.html",
  29361. snapscreen: "~/dialogs/snapscreen/snapscreen.html",
  29362. scrawl: "~/dialogs/scrawl/scrawl.html",
  29363. music: "~/dialogs/music/music.html",
  29364. template: "~/dialogs/template/template.html",
  29365. background: "~/dialogs/background/background.html",
  29366. charts: "~/dialogs/charts/charts.html"
  29367. };
  29368. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  29369. var btnCmds = [
  29370. "undo",
  29371. "redo",
  29372. "formatmatch",
  29373. "bold",
  29374. "italic",
  29375. "underline",
  29376. "fontborder",
  29377. "touppercase",
  29378. "tolowercase",
  29379. "strikethrough",
  29380. "subscript",
  29381. "superscript",
  29382. "source",
  29383. "indent",
  29384. "outdent",
  29385. "blockquote",
  29386. "pasteplain",
  29387. "pagebreak",
  29388. "selectall",
  29389. "print",
  29390. "horizontal",
  29391. "removeformat",
  29392. "time",
  29393. "date",
  29394. "unlink",
  29395. "insertparagraphbeforetable",
  29396. "insertrow",
  29397. "insertcol",
  29398. "mergeright",
  29399. "mergedown",
  29400. "deleterow",
  29401. "deletecol",
  29402. "splittorows",
  29403. "splittocols",
  29404. "splittocells",
  29405. "mergecells",
  29406. "deletetable",
  29407. "drafts"
  29408. ];
  29409. for (var i = 0, ci; (ci = btnCmds[i++]); ) {
  29410. ci = ci.toLowerCase();
  29411. editorui[ci] = (function(cmd) {
  29412. return function(editor) {
  29413. var ui = new editorui.Button({
  29414. className: "edui-for-" + cmd,
  29415. title:
  29416. editor.options.labelMap[cmd] ||
  29417. editor.getLang("labelMap." + cmd) ||
  29418. "",
  29419. onclick: function() {
  29420. editor.execCommand(cmd);
  29421. },
  29422. theme: editor.options.theme,
  29423. showText: false
  29424. });
  29425. editorui.buttons[cmd] = ui;
  29426. editor.addListener("selectionchange", function(
  29427. type,
  29428. causeByUi,
  29429. uiReady
  29430. ) {
  29431. var state = editor.queryCommandState(cmd);
  29432. if (state == -1) {
  29433. ui.setDisabled(true);
  29434. ui.setChecked(false);
  29435. } else {
  29436. if (!uiReady) {
  29437. ui.setDisabled(false);
  29438. ui.setChecked(state);
  29439. }
  29440. }
  29441. });
  29442. return ui;
  29443. };
  29444. })(ci);
  29445. }
  29446. //清除文档
  29447. editorui.cleardoc = function(editor) {
  29448. var ui = new editorui.Button({
  29449. className: "edui-for-cleardoc",
  29450. title:
  29451. editor.options.labelMap.cleardoc ||
  29452. editor.getLang("labelMap.cleardoc") ||
  29453. "",
  29454. theme: editor.options.theme,
  29455. onclick: function() {
  29456. if (confirm(editor.getLang("confirmClear"))) {
  29457. editor.execCommand("cleardoc");
  29458. }
  29459. }
  29460. });
  29461. editorui.buttons["cleardoc"] = ui;
  29462. editor.addListener("selectionchange", function() {
  29463. ui.setDisabled(editor.queryCommandState("cleardoc") == -1);
  29464. });
  29465. return ui;
  29466. };
  29467. //排版,图片排版,文字方向
  29468. var typeset = {
  29469. justify: ["left", "right", "center", "justify"],
  29470. imagefloat: ["none", "left", "center", "right"],
  29471. directionality: ["ltr", "rtl"]
  29472. };
  29473. for (var p in typeset) {
  29474. (function(cmd, val) {
  29475. for (var i = 0, ci; (ci = val[i++]); ) {
  29476. (function(cmd2) {
  29477. editorui[cmd.replace("float", "") + cmd2] = function(editor) {
  29478. var ui = new editorui.Button({
  29479. className: "edui-for-" + cmd.replace("float", "") + cmd2,
  29480. title:
  29481. editor.options.labelMap[cmd.replace("float", "") + cmd2] ||
  29482. editor.getLang(
  29483. "labelMap." + cmd.replace("float", "") + cmd2
  29484. ) ||
  29485. "",
  29486. theme: editor.options.theme,
  29487. onclick: function() {
  29488. editor.execCommand(cmd, cmd2);
  29489. }
  29490. });
  29491. editorui.buttons[cmd] = ui;
  29492. editor.addListener("selectionchange", function(
  29493. type,
  29494. causeByUi,
  29495. uiReady
  29496. ) {
  29497. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  29498. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  29499. });
  29500. return ui;
  29501. };
  29502. })(ci);
  29503. }
  29504. })(p, typeset[p]);
  29505. }
  29506. //字体颜色和背景颜色
  29507. for (var i = 0, ci; (ci = ["backcolor", "forecolor"][i++]); ) {
  29508. editorui[ci] = (function(cmd) {
  29509. return function(editor) {
  29510. var ui = new editorui.ColorButton({
  29511. className: "edui-for-" + cmd,
  29512. color: "default",
  29513. title:
  29514. editor.options.labelMap[cmd] ||
  29515. editor.getLang("labelMap." + cmd) ||
  29516. "",
  29517. editor: editor,
  29518. onpickcolor: function(t, color) {
  29519. editor.execCommand(cmd, color);
  29520. },
  29521. onpicknocolor: function() {
  29522. editor.execCommand(cmd, "default");
  29523. this.setColor("transparent");
  29524. this.color = "default";
  29525. },
  29526. onbuttonclick: function() {
  29527. editor.execCommand(cmd, this.color);
  29528. }
  29529. });
  29530. editorui.buttons[cmd] = ui;
  29531. editor.addListener("selectionchange", function() {
  29532. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  29533. });
  29534. return ui;
  29535. };
  29536. })(ci);
  29537. }
  29538. var dialogBtns = {
  29539. noOk: ["searchreplace", "help", "spechars", "webapp", "preview"],
  29540. ok: [
  29541. "attachment",
  29542. "anchor",
  29543. "link",
  29544. "insertimage",
  29545. "map",
  29546. "gmap",
  29547. "insertframe",
  29548. "wordimage",
  29549. "insertvideo",
  29550. "insertframe",
  29551. "edittip",
  29552. "edittable",
  29553. "edittd",
  29554. "scrawl",
  29555. "template",
  29556. "music",
  29557. "background",
  29558. "charts"
  29559. ]
  29560. };
  29561. for (var p in dialogBtns) {
  29562. (function(type, vals) {
  29563. for (var i = 0, ci; (ci = vals[i++]); ) {
  29564. //todo opera下存在问题
  29565. if (browser.opera && ci === "searchreplace") {
  29566. continue;
  29567. }
  29568. (function(cmd) {
  29569. editorui[cmd] = function(editor, iframeUrl, title) {
  29570. iframeUrl =
  29571. iframeUrl ||
  29572. (editor.options.iframeUrlMap || {})[cmd] ||
  29573. iframeUrlMap[cmd];
  29574. title =
  29575. editor.options.labelMap[cmd] ||
  29576. editor.getLang("labelMap." + cmd) ||
  29577. "";
  29578. var dialog;
  29579. //没有iframeUrl不创建dialog
  29580. if (iframeUrl) {
  29581. dialog = new editorui.Dialog(
  29582. utils.extend(
  29583. {
  29584. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29585. editor: editor,
  29586. className: "edui-for-" + cmd,
  29587. title: title,
  29588. holdScroll: cmd === "insertimage",
  29589. fullscreen: /charts|preview/.test(cmd),
  29590. closeDialog: editor.getLang("closeDialog")
  29591. },
  29592. type == "ok"
  29593. ? {
  29594. buttons: [
  29595. {
  29596. className: "edui-okbutton",
  29597. label: editor.getLang("ok"),
  29598. editor: editor,
  29599. onclick: function() {
  29600. dialog.close(true);
  29601. }
  29602. },
  29603. {
  29604. className: "edui-cancelbutton",
  29605. label: editor.getLang("cancel"),
  29606. editor: editor,
  29607. onclick: function() {
  29608. dialog.close(false);
  29609. }
  29610. }
  29611. ]
  29612. }
  29613. : {}
  29614. )
  29615. );
  29616. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  29617. }
  29618. var ui = new editorui.Button({
  29619. className: "edui-for-" + cmd,
  29620. title: title,
  29621. onclick: function() {
  29622. if (dialog) {
  29623. switch (cmd) {
  29624. case "wordimage":
  29625. var images = editor.execCommand("wordimage");
  29626. if (images && images.length) {
  29627. dialog.render();
  29628. dialog.open();
  29629. }
  29630. break;
  29631. case "scrawl":
  29632. if (editor.queryCommandState("scrawl") != -1) {
  29633. dialog.render();
  29634. dialog.open();
  29635. }
  29636. break;
  29637. default:
  29638. dialog.render();
  29639. dialog.open();
  29640. }
  29641. }
  29642. },
  29643. theme: editor.options.theme,
  29644. disabled:
  29645. (cmd == "scrawl" && editor.queryCommandState("scrawl") == -1) ||
  29646. cmd == "charts"
  29647. });
  29648. editorui.buttons[cmd] = ui;
  29649. editor.addListener("selectionchange", function() {
  29650. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  29651. var unNeedCheckState = { edittable: 1 };
  29652. if (cmd in unNeedCheckState) return;
  29653. var state = editor.queryCommandState(cmd);
  29654. if (ui.getDom()) {
  29655. ui.setDisabled(state == -1);
  29656. ui.setChecked(state);
  29657. }
  29658. });
  29659. return ui;
  29660. };
  29661. })(ci.toLowerCase());
  29662. }
  29663. })(p, dialogBtns[p]);
  29664. }
  29665. editorui.snapscreen = function(editor, iframeUrl, title) {
  29666. title =
  29667. editor.options.labelMap["snapscreen"] ||
  29668. editor.getLang("labelMap.snapscreen") ||
  29669. "";
  29670. var ui = new editorui.Button({
  29671. className: "edui-for-snapscreen",
  29672. title: title,
  29673. onclick: function() {
  29674. editor.execCommand("snapscreen");
  29675. },
  29676. theme: editor.options.theme
  29677. });
  29678. editorui.buttons["snapscreen"] = ui;
  29679. iframeUrl =
  29680. iframeUrl ||
  29681. (editor.options.iframeUrlMap || {})["snapscreen"] ||
  29682. iframeUrlMap["snapscreen"];
  29683. if (iframeUrl) {
  29684. var dialog = new editorui.Dialog({
  29685. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29686. editor: editor,
  29687. className: "edui-for-snapscreen",
  29688. title: title,
  29689. buttons: [
  29690. {
  29691. className: "edui-okbutton",
  29692. label: editor.getLang("ok"),
  29693. editor: editor,
  29694. onclick: function() {
  29695. dialog.close(true);
  29696. }
  29697. },
  29698. {
  29699. className: "edui-cancelbutton",
  29700. label: editor.getLang("cancel"),
  29701. editor: editor,
  29702. onclick: function() {
  29703. dialog.close(false);
  29704. }
  29705. }
  29706. ]
  29707. });
  29708. dialog.render();
  29709. editor.ui._dialogs["snapscreenDialog"] = dialog;
  29710. }
  29711. editor.addListener("selectionchange", function() {
  29712. ui.setDisabled(editor.queryCommandState("snapscreen") == -1);
  29713. });
  29714. return ui;
  29715. };
  29716. editorui.insertcode = function(editor, list, title) {
  29717. list = editor.options["insertcode"] || [];
  29718. title =
  29719. editor.options.labelMap["insertcode"] ||
  29720. editor.getLang("labelMap.insertcode") ||
  29721. "";
  29722. // if (!list.length) return;
  29723. var items = [];
  29724. utils.each(list, function(key, val) {
  29725. items.push({
  29726. label: key,
  29727. value: val,
  29728. theme: editor.options.theme,
  29729. renderLabelHtml: function() {
  29730. return (
  29731. '<div class="edui-label %%-label" >' + (this.label || "") + "</div>"
  29732. );
  29733. }
  29734. });
  29735. });
  29736. var ui = new editorui.Combox({
  29737. editor: editor,
  29738. items: items,
  29739. onselect: function(t, index) {
  29740. editor.execCommand("insertcode", this.items[index].value);
  29741. },
  29742. onbuttonclick: function() {
  29743. this.showPopup();
  29744. },
  29745. title: title,
  29746. initValue: title,
  29747. className: "edui-for-insertcode",
  29748. indexByValue: function(value) {
  29749. if (value) {
  29750. for (var i = 0, ci; (ci = this.items[i]); i++) {
  29751. if (ci.value.indexOf(value) != -1) return i;
  29752. }
  29753. }
  29754. return -1;
  29755. }
  29756. });
  29757. editorui.buttons["insertcode"] = ui;
  29758. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29759. if (!uiReady) {
  29760. var state = editor.queryCommandState("insertcode");
  29761. if (state == -1) {
  29762. ui.setDisabled(true);
  29763. } else {
  29764. ui.setDisabled(false);
  29765. var value = editor.queryCommandValue("insertcode");
  29766. if (!value) {
  29767. ui.setValue(title);
  29768. return;
  29769. }
  29770. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  29771. value && (value = value.replace(/['"]/g, "").split(",")[0]);
  29772. ui.setValue(value);
  29773. }
  29774. }
  29775. });
  29776. return ui;
  29777. };
  29778. editorui.fontfamily = function(editor, list, title) {
  29779. list = editor.options["fontfamily"] || [];
  29780. title =
  29781. editor.options.labelMap["fontfamily"] ||
  29782. editor.getLang("labelMap.fontfamily") ||
  29783. "";
  29784. if (!list.length) return;
  29785. for (var i = 0, ci, items = []; (ci = list[i]); i++) {
  29786. var langLabel = editor.getLang("fontfamily")[ci.name] || "";
  29787. (function(key, val) {
  29788. items.push({
  29789. label: key,
  29790. value: val,
  29791. theme: editor.options.theme,
  29792. renderLabelHtml: function() {
  29793. return (
  29794. '<div class="edui-label %%-label" style="font-family:' +
  29795. utils.unhtml(this.value) +
  29796. '">' +
  29797. (this.label || "") +
  29798. "</div>"
  29799. );
  29800. }
  29801. });
  29802. })(ci.label || langLabel, ci.val);
  29803. }
  29804. var ui = new editorui.Combox({
  29805. editor: editor,
  29806. items: items,
  29807. onselect: function(t, index) {
  29808. editor.execCommand("FontFamily", this.items[index].value);
  29809. },
  29810. onbuttonclick: function() {
  29811. this.showPopup();
  29812. },
  29813. title: title,
  29814. initValue: title,
  29815. className: "edui-for-fontfamily",
  29816. indexByValue: function(value) {
  29817. if (value) {
  29818. for (var i = 0, ci; (ci = this.items[i]); i++) {
  29819. if (ci.value.indexOf(value) != -1) return i;
  29820. }
  29821. }
  29822. return -1;
  29823. }
  29824. });
  29825. editorui.buttons["fontfamily"] = ui;
  29826. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29827. if (!uiReady) {
  29828. var state = editor.queryCommandState("FontFamily");
  29829. if (state == -1) {
  29830. ui.setDisabled(true);
  29831. } else {
  29832. ui.setDisabled(false);
  29833. var value = editor.queryCommandValue("FontFamily");
  29834. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  29835. value && (value = value.replace(/['"]/g, "").split(",")[0]);
  29836. ui.setValue(value);
  29837. }
  29838. }
  29839. });
  29840. return ui;
  29841. };
  29842. editorui.fontsize = function(editor, list, title) {
  29843. title =
  29844. editor.options.labelMap["fontsize"] ||
  29845. editor.getLang("labelMap.fontsize") ||
  29846. "";
  29847. list = list || editor.options["fontsize"] || [];
  29848. if (!list.length) return;
  29849. var items = [];
  29850. for (var i = 0; i < list.length; i++) {
  29851. var size = list[i] + "px";
  29852. items.push({
  29853. label: size,
  29854. value: size,
  29855. theme: editor.options.theme,
  29856. renderLabelHtml: function() {
  29857. return (
  29858. '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  29859. this.value +
  29860. '">' +
  29861. (this.label || "") +
  29862. "</div>"
  29863. );
  29864. }
  29865. });
  29866. }
  29867. var ui = new editorui.Combox({
  29868. editor: editor,
  29869. items: items,
  29870. title: title,
  29871. initValue: title,
  29872. onselect: function(t, index) {
  29873. editor.execCommand("FontSize", this.items[index].value);
  29874. },
  29875. onbuttonclick: function() {
  29876. this.showPopup();
  29877. },
  29878. className: "edui-for-fontsize"
  29879. });
  29880. editorui.buttons["fontsize"] = ui;
  29881. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29882. if (!uiReady) {
  29883. var state = editor.queryCommandState("FontSize");
  29884. if (state == -1) {
  29885. ui.setDisabled(true);
  29886. } else {
  29887. ui.setDisabled(false);
  29888. ui.setValue(editor.queryCommandValue("FontSize"));
  29889. }
  29890. }
  29891. });
  29892. return ui;
  29893. };
  29894. editorui.paragraph = function(editor, list, title) {
  29895. title =
  29896. editor.options.labelMap["paragraph"] ||
  29897. editor.getLang("labelMap.paragraph") ||
  29898. "";
  29899. list = editor.options["paragraph"] || [];
  29900. if (utils.isEmptyObject(list)) return;
  29901. var items = [];
  29902. for (var i in list) {
  29903. items.push({
  29904. value: i,
  29905. label: list[i] || editor.getLang("paragraph")[i],
  29906. theme: editor.options.theme,
  29907. renderLabelHtml: function() {
  29908. return (
  29909. '<div class="edui-label %%-label"><span class="edui-for-' +
  29910. this.value +
  29911. '">' +
  29912. (this.label || "") +
  29913. "</span></div>"
  29914. );
  29915. }
  29916. });
  29917. }
  29918. var ui = new editorui.Combox({
  29919. editor: editor,
  29920. items: items,
  29921. title: title,
  29922. initValue: title,
  29923. className: "edui-for-paragraph",
  29924. onselect: function(t, index) {
  29925. editor.execCommand("Paragraph", this.items[index].value);
  29926. },
  29927. onbuttonclick: function() {
  29928. this.showPopup();
  29929. }
  29930. });
  29931. editorui.buttons["paragraph"] = ui;
  29932. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29933. if (!uiReady) {
  29934. var state = editor.queryCommandState("Paragraph");
  29935. if (state == -1) {
  29936. ui.setDisabled(true);
  29937. } else {
  29938. ui.setDisabled(false);
  29939. var value = editor.queryCommandValue("Paragraph");
  29940. var index = ui.indexByValue(value);
  29941. if (index != -1) {
  29942. ui.setValue(value);
  29943. } else {
  29944. ui.setValue(ui.initValue);
  29945. }
  29946. }
  29947. }
  29948. });
  29949. return ui;
  29950. };
  29951. //自定义标题
  29952. editorui.customstyle = function(editor) {
  29953. var list = editor.options["customstyle"] || [],
  29954. title =
  29955. editor.options.labelMap["customstyle"] ||
  29956. editor.getLang("labelMap.customstyle") ||
  29957. "";
  29958. if (!list.length) return;
  29959. var langCs = editor.getLang("customstyle");
  29960. for (var i = 0, items = [], t; (t = list[i++]); ) {
  29961. (function(t) {
  29962. var ck = {};
  29963. ck.label = t.label ? t.label : langCs[t.name];
  29964. ck.style = t.style;
  29965. ck.className = t.className;
  29966. ck.tag = t.tag;
  29967. items.push({
  29968. label: ck.label,
  29969. value: ck,
  29970. theme: editor.options.theme,
  29971. renderLabelHtml: function() {
  29972. return (
  29973. '<div class="edui-label %%-label">' +
  29974. "<" +
  29975. ck.tag +
  29976. " " +
  29977. (ck.className ? ' class="' + ck.className + '"' : "") +
  29978. (ck.style ? ' style="' + ck.style + '"' : "") +
  29979. ">" +
  29980. ck.label +
  29981. "</" +
  29982. ck.tag +
  29983. ">" +
  29984. "</div>"
  29985. );
  29986. }
  29987. });
  29988. })(t);
  29989. }
  29990. var ui = new editorui.Combox({
  29991. editor: editor,
  29992. items: items,
  29993. title: title,
  29994. initValue: title,
  29995. className: "edui-for-customstyle",
  29996. onselect: function(t, index) {
  29997. editor.execCommand("customstyle", this.items[index].value);
  29998. },
  29999. onbuttonclick: function() {
  30000. this.showPopup();
  30001. },
  30002. indexByValue: function(value) {
  30003. for (var i = 0, ti; (ti = this.items[i++]); ) {
  30004. if (ti.label == value) {
  30005. return i - 1;
  30006. }
  30007. }
  30008. return -1;
  30009. }
  30010. });
  30011. editorui.buttons["customstyle"] = ui;
  30012. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  30013. if (!uiReady) {
  30014. var state = editor.queryCommandState("customstyle");
  30015. if (state == -1) {
  30016. ui.setDisabled(true);
  30017. } else {
  30018. ui.setDisabled(false);
  30019. var value = editor.queryCommandValue("customstyle");
  30020. var index = ui.indexByValue(value);
  30021. if (index != -1) {
  30022. ui.setValue(value);
  30023. } else {
  30024. ui.setValue(ui.initValue);
  30025. }
  30026. }
  30027. }
  30028. });
  30029. return ui;
  30030. };
  30031. editorui.inserttable = function(editor, iframeUrl, title) {
  30032. title =
  30033. editor.options.labelMap["inserttable"] ||
  30034. editor.getLang("labelMap.inserttable") ||
  30035. "";
  30036. var ui = new editorui.TableButton({
  30037. editor: editor,
  30038. title: title,
  30039. className: "edui-for-inserttable",
  30040. onpicktable: function(t, numCols, numRows) {
  30041. editor.execCommand("InsertTable", {
  30042. numRows: numRows,
  30043. numCols: numCols,
  30044. border: 1
  30045. });
  30046. },
  30047. onbuttonclick: function() {
  30048. this.showPopup();
  30049. }
  30050. });
  30051. editorui.buttons["inserttable"] = ui;
  30052. editor.addListener("selectionchange", function() {
  30053. ui.setDisabled(editor.queryCommandState("inserttable") == -1);
  30054. });
  30055. return ui;
  30056. };
  30057. editorui.lineheight = function(editor) {
  30058. var val = editor.options.lineheight || [];
  30059. if (!val.length) return;
  30060. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30061. items.push({
  30062. //todo:写死了
  30063. label: ci,
  30064. value: ci,
  30065. theme: editor.options.theme,
  30066. onclick: function() {
  30067. editor.execCommand("lineheight", this.value);
  30068. }
  30069. });
  30070. }
  30071. var ui = new editorui.MenuButton({
  30072. editor: editor,
  30073. className: "edui-for-lineheight",
  30074. title:
  30075. editor.options.labelMap["lineheight"] ||
  30076. editor.getLang("labelMap.lineheight") ||
  30077. "",
  30078. items: items,
  30079. onbuttonclick: function() {
  30080. var value = editor.queryCommandValue("LineHeight") || this.value;
  30081. editor.execCommand("LineHeight", value);
  30082. }
  30083. });
  30084. editorui.buttons["lineheight"] = ui;
  30085. editor.addListener("selectionchange", function() {
  30086. var state = editor.queryCommandState("LineHeight");
  30087. if (state == -1) {
  30088. ui.setDisabled(true);
  30089. } else {
  30090. ui.setDisabled(false);
  30091. var value = editor.queryCommandValue("LineHeight");
  30092. value && ui.setValue((value + "").replace(/cm/, ""));
  30093. ui.setChecked(state);
  30094. }
  30095. });
  30096. return ui;
  30097. };
  30098. var rowspacings = ["top", "bottom"];
  30099. for (var r = 0, ri; (ri = rowspacings[r++]); ) {
  30100. (function(cmd) {
  30101. editorui["rowspacing" + cmd] = function(editor) {
  30102. var val = editor.options["rowspacing" + cmd] || [];
  30103. if (!val.length) return null;
  30104. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30105. items.push({
  30106. label: ci,
  30107. value: ci,
  30108. theme: editor.options.theme,
  30109. onclick: function() {
  30110. editor.execCommand("rowspacing", this.value, cmd);
  30111. }
  30112. });
  30113. }
  30114. var ui = new editorui.MenuButton({
  30115. editor: editor,
  30116. className: "edui-for-rowspacing" + cmd,
  30117. title:
  30118. editor.options.labelMap["rowspacing" + cmd] ||
  30119. editor.getLang("labelMap.rowspacing" + cmd) ||
  30120. "",
  30121. items: items,
  30122. onbuttonclick: function() {
  30123. var value =
  30124. editor.queryCommandValue("rowspacing", cmd) || this.value;
  30125. editor.execCommand("rowspacing", value, cmd);
  30126. }
  30127. });
  30128. editorui.buttons[cmd] = ui;
  30129. editor.addListener("selectionchange", function() {
  30130. var state = editor.queryCommandState("rowspacing", cmd);
  30131. if (state == -1) {
  30132. ui.setDisabled(true);
  30133. } else {
  30134. ui.setDisabled(false);
  30135. var value = editor.queryCommandValue("rowspacing", cmd);
  30136. value && ui.setValue((value + "").replace(/%/, ""));
  30137. ui.setChecked(state);
  30138. }
  30139. });
  30140. return ui;
  30141. };
  30142. })(ri);
  30143. }
  30144. //有序,无序列表
  30145. var lists = ["insertorderedlist", "insertunorderedlist"];
  30146. for (var l = 0, cl; (cl = lists[l++]); ) {
  30147. (function(cmd) {
  30148. editorui[cmd] = function(editor) {
  30149. var vals = editor.options[cmd],
  30150. _onMenuClick = function() {
  30151. editor.execCommand(cmd, this.value);
  30152. },
  30153. items = [];
  30154. for (var i in vals) {
  30155. items.push({
  30156. label: vals[i] || editor.getLang()[cmd][i] || "",
  30157. value: i,
  30158. theme: editor.options.theme,
  30159. onclick: _onMenuClick
  30160. });
  30161. }
  30162. var ui = new editorui.MenuButton({
  30163. editor: editor,
  30164. className: "edui-for-" + cmd,
  30165. title: editor.getLang("labelMap." + cmd) || "",
  30166. items: items,
  30167. onbuttonclick: function() {
  30168. var value = editor.queryCommandValue(cmd) || this.value;
  30169. editor.execCommand(cmd, value);
  30170. }
  30171. });
  30172. editorui.buttons[cmd] = ui;
  30173. editor.addListener("selectionchange", function() {
  30174. var state = editor.queryCommandState(cmd);
  30175. if (state == -1) {
  30176. ui.setDisabled(true);
  30177. } else {
  30178. ui.setDisabled(false);
  30179. var value = editor.queryCommandValue(cmd);
  30180. ui.setValue(value);
  30181. ui.setChecked(state);
  30182. }
  30183. });
  30184. return ui;
  30185. };
  30186. })(cl);
  30187. }
  30188. editorui.fullscreen = function(editor, title) {
  30189. title =
  30190. editor.options.labelMap["fullscreen"] ||
  30191. editor.getLang("labelMap.fullscreen") ||
  30192. "";
  30193. var ui = new editorui.Button({
  30194. className: "edui-for-fullscreen",
  30195. title: title,
  30196. theme: editor.options.theme,
  30197. onclick: function() {
  30198. if (editor.ui) {
  30199. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  30200. }
  30201. this.setChecked(editor.ui.isFullScreen());
  30202. }
  30203. });
  30204. editorui.buttons["fullscreen"] = ui;
  30205. editor.addListener("selectionchange", function() {
  30206. var state = editor.queryCommandState("fullscreen");
  30207. ui.setDisabled(state == -1);
  30208. ui.setChecked(editor.ui.isFullScreen());
  30209. });
  30210. return ui;
  30211. };
  30212. // 表情
  30213. editorui["emotion"] = function(editor, iframeUrl) {
  30214. var cmd = "emotion";
  30215. var ui = new editorui.MultiMenuPop({
  30216. title:
  30217. editor.options.labelMap[cmd] ||
  30218. editor.getLang("labelMap." + cmd + "") ||
  30219. "",
  30220. editor: editor,
  30221. className: "edui-for-" + cmd,
  30222. iframeUrl: editor.ui.mapUrl(
  30223. iframeUrl ||
  30224. (editor.options.iframeUrlMap || {})[cmd] ||
  30225. iframeUrlMap[cmd]
  30226. )
  30227. });
  30228. editorui.buttons[cmd] = ui;
  30229. editor.addListener("selectionchange", function() {
  30230. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  30231. });
  30232. return ui;
  30233. };
  30234. editorui.autotypeset = function(editor) {
  30235. var ui = new editorui.AutoTypeSetButton({
  30236. editor: editor,
  30237. title:
  30238. editor.options.labelMap["autotypeset"] ||
  30239. editor.getLang("labelMap.autotypeset") ||
  30240. "",
  30241. className: "edui-for-autotypeset",
  30242. onbuttonclick: function() {
  30243. editor.execCommand("autotypeset");
  30244. }
  30245. });
  30246. editorui.buttons["autotypeset"] = ui;
  30247. editor.addListener("selectionchange", function() {
  30248. ui.setDisabled(editor.queryCommandState("autotypeset") == -1);
  30249. });
  30250. return ui;
  30251. };
  30252. /* 简单上传插件 */
  30253. editorui["simpleupload"] = function(editor) {
  30254. var name = "simpleupload",
  30255. ui = new editorui.Button({
  30256. className: "edui-for-" + name,
  30257. title:
  30258. editor.options.labelMap[name] ||
  30259. editor.getLang("labelMap." + name) ||
  30260. "",
  30261. onclick: function() {},
  30262. theme: editor.options.theme,
  30263. showText: false
  30264. });
  30265. editorui.buttons[name] = ui;
  30266. editor.addListener("ready", function() {
  30267. var b = ui.getDom("body"),
  30268. iconSpan = b.children[0];
  30269. editor.fireEvent("simpleuploadbtnready", iconSpan);
  30270. });
  30271. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  30272. var state = editor.queryCommandState(name);
  30273. if (state == -1) {
  30274. ui.setDisabled(true);
  30275. ui.setChecked(false);
  30276. } else {
  30277. if (!uiReady) {
  30278. ui.setDisabled(false);
  30279. ui.setChecked(state);
  30280. }
  30281. }
  30282. });
  30283. return ui;
  30284. };
  30285. })();
  30286. // adapter/editor.js
  30287. ///import core
  30288. ///commands 全屏
  30289. ///commandsName FullScreen
  30290. ///commandsTitle 全屏
  30291. ;(function() {
  30292. var utils = baidu.editor.utils,
  30293. uiUtils = baidu.editor.ui.uiUtils,
  30294. UIBase = baidu.editor.ui.UIBase,
  30295. domUtils = baidu.editor.dom.domUtils;
  30296. var nodeStack = [];
  30297. function EditorUI(options) {
  30298. this.initOptions(options);
  30299. this.initEditorUI();
  30300. }
  30301. EditorUI.prototype = {
  30302. uiName: "editor",
  30303. initEditorUI: function() {
  30304. this.editor.ui = this;
  30305. this._dialogs = {};
  30306. this.initUIBase();
  30307. this._initToolbars();
  30308. var editor = this.editor,
  30309. me = this;
  30310. editor.addListener("ready", function() {
  30311. //提供getDialog方法
  30312. editor.getDialog = function(name) {
  30313. return editor.ui._dialogs[name + "Dialog"];
  30314. };
  30315. domUtils.on(editor.window, "scroll", function(evt) {
  30316. baidu.editor.ui.Popup.postHide(evt);
  30317. });
  30318. //提供编辑器实时宽高(全屏时宽高不变化)
  30319. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  30320. UE.browser.ie &&
  30321. UE.browser.version === 6 &&
  30322. editor.container.ownerDocument.execCommand(
  30323. "BackgroundImageCache",
  30324. false,
  30325. true
  30326. );
  30327. //display bottom-bar label based on config
  30328. if (editor.options.elementPathEnabled) {
  30329. editor.ui.getDom("elementpath").innerHTML =
  30330. '<div class="edui-editor-breadcrumb">' +
  30331. editor.getLang("elementPathTip") +
  30332. ":</div>";
  30333. }
  30334. if (editor.options.wordCount) {
  30335. function countFn() {
  30336. setCount(editor, me);
  30337. domUtils.un(editor.document, "click", arguments.callee);
  30338. }
  30339. domUtils.on(editor.document, "click", countFn);
  30340. editor.ui.getDom("wordcount").innerHTML = editor.getLang(
  30341. "wordCountTip"
  30342. );
  30343. }
  30344. editor.ui._scale();
  30345. if (editor.options.scaleEnabled) {
  30346. if (editor.autoHeightEnabled) {
  30347. editor.disableAutoHeight();
  30348. }
  30349. me.enableScale();
  30350. } else {
  30351. me.disableScale();
  30352. }
  30353. if (
  30354. !editor.options.elementPathEnabled &&
  30355. !editor.options.wordCount &&
  30356. !editor.options.scaleEnabled
  30357. ) {
  30358. editor.ui.getDom("elementpath").style.display = "none";
  30359. editor.ui.getDom("wordcount").style.display = "none";
  30360. editor.ui.getDom("scale").style.display = "none";
  30361. }
  30362. if (!editor.selection.isFocus()) return;
  30363. editor.fireEvent("selectionchange", false, true);
  30364. });
  30365. editor.addListener("mousedown", function(t, evt) {
  30366. var el = evt.target || evt.srcElement;
  30367. baidu.editor.ui.Popup.postHide(evt, el);
  30368. baidu.editor.ui.ShortCutMenu.postHide(evt);
  30369. });
  30370. editor.addListener("delcells", function() {
  30371. if (UE.ui["edittip"]) {
  30372. new UE.ui["edittip"](editor);
  30373. }
  30374. editor.getDialog("edittip").open();
  30375. });
  30376. var pastePop,
  30377. isPaste = false,
  30378. timer;
  30379. editor.addListener("afterpaste", function() {
  30380. if (editor.queryCommandState("pasteplain")) return;
  30381. if (baidu.editor.ui.PastePicker) {
  30382. pastePop = new baidu.editor.ui.Popup({
  30383. content: new baidu.editor.ui.PastePicker({ editor: editor }),
  30384. editor: editor,
  30385. className: "edui-wordpastepop"
  30386. });
  30387. pastePop.render();
  30388. }
  30389. isPaste = true;
  30390. });
  30391. editor.addListener("afterinserthtml", function() {
  30392. clearTimeout(timer);
  30393. timer = setTimeout(function() {
  30394. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  30395. if (pastePop.isHidden()) {
  30396. var span = domUtils.createElement(editor.document, "span", {
  30397. style: "line-height:0px;",
  30398. innerHTML: "\ufeff"
  30399. }),
  30400. range = editor.selection.getRange();
  30401. range.insertNode(span);
  30402. var tmp = getDomNode(span, "firstChild", "previousSibling");
  30403. tmp &&
  30404. pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  30405. domUtils.remove(span);
  30406. } else {
  30407. pastePop.show();
  30408. }
  30409. delete editor.ui._isTransfer;
  30410. isPaste = false;
  30411. }
  30412. }, 200);
  30413. });
  30414. editor.addListener("contextmenu", function(t, evt) {
  30415. baidu.editor.ui.Popup.postHide(evt);
  30416. });
  30417. editor.addListener("keydown", function(t, evt) {
  30418. if (pastePop) pastePop.dispose(evt);
  30419. var keyCode = evt.keyCode || evt.which;
  30420. if (evt.altKey && keyCode == 90) {
  30421. UE.ui.buttons["fullscreen"].onclick();
  30422. }
  30423. });
  30424. editor.addListener("wordcount", function(type) {
  30425. setCount(this, me);
  30426. });
  30427. function setCount(editor, ui) {
  30428. editor.setOpt({
  30429. wordCount: true,
  30430. maximumWords: 10000,
  30431. wordCountMsg:
  30432. editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  30433. wordOverFlowMsg:
  30434. editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  30435. });
  30436. var opt = editor.options,
  30437. max = opt.maximumWords,
  30438. msg = opt.wordCountMsg,
  30439. errMsg = opt.wordOverFlowMsg,
  30440. countDom = ui.getDom("wordcount");
  30441. if (!opt.wordCount) {
  30442. return;
  30443. }
  30444. var count = editor.getContentLength(true);
  30445. if (count > max) {
  30446. countDom.innerHTML = errMsg;
  30447. editor.fireEvent("wordcountoverflow");
  30448. } else {
  30449. countDom.innerHTML = msg
  30450. .replace("{#leave}", max - count)
  30451. .replace("{#count}", count);
  30452. }
  30453. }
  30454. editor.addListener("selectionchange", function() {
  30455. if (editor.options.elementPathEnabled) {
  30456. me[
  30457. (editor.queryCommandState("elementpath") == -1 ? "dis" : "en") +
  30458. "ableElementPath"
  30459. ]();
  30460. }
  30461. if (editor.options.scaleEnabled) {
  30462. me[
  30463. (editor.queryCommandState("scale") == -1 ? "dis" : "en") +
  30464. "ableScale"
  30465. ]();
  30466. }
  30467. });
  30468. var popup = new baidu.editor.ui.Popup({
  30469. editor: editor,
  30470. content: "",
  30471. className: "edui-bubble",
  30472. _onEditButtonClick: function() {
  30473. this.hide();
  30474. editor.ui._dialogs.linkDialog.open();
  30475. },
  30476. _onImgEditButtonClick: function(name) {
  30477. this.hide();
  30478. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  30479. },
  30480. _onImgSetFloat: function(value) {
  30481. this.hide();
  30482. editor.execCommand("imagefloat", value);
  30483. },
  30484. _setIframeAlign: function(value) {
  30485. var frame = popup.anchorEl;
  30486. var newFrame = frame.cloneNode(true);
  30487. switch (value) {
  30488. case -2:
  30489. newFrame.setAttribute("align", "");
  30490. break;
  30491. case -1:
  30492. newFrame.setAttribute("align", "left");
  30493. break;
  30494. case 1:
  30495. newFrame.setAttribute("align", "right");
  30496. break;
  30497. }
  30498. frame.parentNode.insertBefore(newFrame, frame);
  30499. domUtils.remove(frame);
  30500. popup.anchorEl = newFrame;
  30501. popup.showAnchor(popup.anchorEl);
  30502. },
  30503. _updateIframe: function() {
  30504. var frame = (editor._iframe = popup.anchorEl);
  30505. if (domUtils.hasClass(frame, "ueditor_baidumap")) {
  30506. editor.selection.getRange().selectNode(frame).select();
  30507. editor.ui._dialogs.mapDialog.open();
  30508. popup.hide();
  30509. } else {
  30510. editor.ui._dialogs.insertframeDialog.open();
  30511. popup.hide();
  30512. }
  30513. },
  30514. _onRemoveButtonClick: function(cmdName) {
  30515. editor.execCommand(cmdName);
  30516. this.hide();
  30517. },
  30518. queryAutoHide: function(el) {
  30519. if (el && el.ownerDocument == editor.document) {
  30520. if (
  30521. el.tagName.toLowerCase() == "img" ||
  30522. domUtils.findParentByTagName(el, "a", true)
  30523. ) {
  30524. return el !== popup.anchorEl;
  30525. }
  30526. }
  30527. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  30528. }
  30529. });
  30530. popup.render();
  30531. if (editor.options.imagePopup) {
  30532. editor.addListener("mouseover", function(t, evt) {
  30533. evt = evt || window.event;
  30534. var el = evt.target || evt.srcElement;
  30535. if (
  30536. editor.ui._dialogs.insertframeDialog &&
  30537. /iframe/gi.test(el.tagName)
  30538. ) {
  30539. var html = popup.formatHtml(
  30540. "<nobr>" +
  30541. editor.getLang("property") +
  30542. ': <span onclick=$$._setIframeAlign(-2) class="edui-clickable">' +
  30543. editor.getLang("default") +
  30544. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' +
  30545. editor.getLang("justifyleft") +
  30546. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' +
  30547. editor.getLang("justifyright") +
  30548. "</span>&nbsp;&nbsp;" +
  30549. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' +
  30550. editor.getLang("modify") +
  30551. "</span></nobr>"
  30552. );
  30553. if (html) {
  30554. popup.getDom("content").innerHTML = html;
  30555. popup.anchorEl = el;
  30556. popup.showAnchor(popup.anchorEl);
  30557. } else {
  30558. popup.hide();
  30559. }
  30560. }
  30561. });
  30562. editor.addListener("selectionchange", function(t, causeByUi) {
  30563. if (!causeByUi) return;
  30564. var html = "",
  30565. str = "",
  30566. img = editor.selection.getRange().getClosedNode(),
  30567. dialogs = editor.ui._dialogs;
  30568. if (img && img.tagName == "IMG") {
  30569. var dialogName = "insertimageDialog";
  30570. if (
  30571. img.className.indexOf("edui-faked-video") != -1 ||
  30572. img.className.indexOf("edui-upload-video") != -1
  30573. ) {
  30574. dialogName = "insertvideoDialog";
  30575. }
  30576. if (img.className.indexOf("edui-faked-webapp") != -1) {
  30577. dialogName = "webappDialog";
  30578. }
  30579. if (img.src.indexOf("https://api.map.baidu.com") != -1) {
  30580. dialogName = "mapDialog";
  30581. }
  30582. if (img.className.indexOf("edui-faked-music") != -1) {
  30583. dialogName = "musicDialog";
  30584. }
  30585. if (
  30586. img.src.indexOf("http://maps.google.com/maps/api/staticmap") != -1
  30587. ) {
  30588. dialogName = "gmapDialog";
  30589. }
  30590. if (img.getAttribute("anchorname")) {
  30591. dialogName = "anchorDialog";
  30592. html = popup.formatHtml(
  30593. "<nobr>" +
  30594. editor.getLang("property") +
  30595. ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' +
  30596. editor.getLang("modify") +
  30597. "</span>&nbsp;&nbsp;" +
  30598. "<span onclick=$$._onRemoveButtonClick('anchor') class=\"edui-clickable\">" +
  30599. editor.getLang("delete") +
  30600. "</span></nobr>"
  30601. );
  30602. }
  30603. if (img.getAttribute("word_img")) {
  30604. //todo 放到dialog去做查询
  30605. editor.word_img = [img.getAttribute("word_img")];
  30606. dialogName = "wordimageDialog";
  30607. }
  30608. if (
  30609. domUtils.hasClass(img, "loadingclass") ||
  30610. domUtils.hasClass(img, "loaderrorclass")
  30611. ) {
  30612. dialogName = "";
  30613. }
  30614. if (!dialogs[dialogName]) {
  30615. return;
  30616. }
  30617. str =
  30618. "<nobr>" +
  30619. editor.getLang("property") +
  30620. ": " +
  30621. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' +
  30622. editor.getLang("default") +
  30623. "</span>&nbsp;&nbsp;" +
  30624. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' +
  30625. editor.getLang("justifyleft") +
  30626. "</span>&nbsp;&nbsp;" +
  30627. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' +
  30628. editor.getLang("justifyright") +
  30629. "</span>&nbsp;&nbsp;" +
  30630. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' +
  30631. editor.getLang("justifycenter") +
  30632. "</span>&nbsp;&nbsp;" +
  30633. "<span onclick=\"$$._onImgEditButtonClick('" +
  30634. dialogName +
  30635. '\');" class="edui-clickable">' +
  30636. editor.getLang("modify") +
  30637. "</span></nobr>";
  30638. !html && (html = popup.formatHtml(str));
  30639. }
  30640. if (editor.ui._dialogs.linkDialog) {
  30641. var link = editor.queryCommandValue("link");
  30642. var url;
  30643. if (
  30644. link &&
  30645. (url = link.getAttribute("_href") || link.getAttribute("href", 2))
  30646. ) {
  30647. var txt = url;
  30648. if (url.length > 30) {
  30649. txt = url.substring(0, 20) + "...";
  30650. }
  30651. if (html) {
  30652. html += '<div style="height:5px;"></div>';
  30653. }
  30654. html += popup.formatHtml(
  30655. "<nobr>" +
  30656. editor.getLang("anthorMsg") +
  30657. ': <a target="_blank" href="' +
  30658. url +
  30659. '" title="' +
  30660. url +
  30661. '" >' +
  30662. txt +
  30663. "</a>" +
  30664. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' +
  30665. editor.getLang("modify") +
  30666. "</span>" +
  30667. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' +
  30668. editor.getLang("clear") +
  30669. "</span></nobr>"
  30670. );
  30671. popup.showAnchor(link);
  30672. }
  30673. }
  30674. if (html) {
  30675. popup.getDom("content").innerHTML = html;
  30676. popup.anchorEl = img || link;
  30677. popup.showAnchor(popup.anchorEl);
  30678. } else {
  30679. popup.hide();
  30680. }
  30681. });
  30682. }
  30683. },
  30684. _initToolbars: function() {
  30685. var editor = this.editor;
  30686. var toolbars = this.toolbars || [];
  30687. var toolbarUis = [];
  30688. var extraUIs = [];
  30689. for (var i = 0; i < toolbars.length; i++) {
  30690. var toolbar = toolbars[i];
  30691. var toolbarUi = new baidu.editor.ui.Toolbar({
  30692. theme: editor.options.theme
  30693. });
  30694. for (var j = 0; j < toolbar.length; j++) {
  30695. var toolbarItem = toolbar[j];
  30696. var toolbarItemUi = null;
  30697. if (typeof toolbarItem == "string") {
  30698. toolbarItem = toolbarItem.toLowerCase();
  30699. if (toolbarItem == "|") {
  30700. toolbarItem = "Separator";
  30701. }
  30702. if (toolbarItem == "||") {
  30703. toolbarItem = "Breakline";
  30704. }
  30705. var ui = baidu.editor.ui[toolbarItem];
  30706. if (ui) {
  30707. if (utils.isFunction(ui)) {
  30708. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  30709. } else {
  30710. if (ui.id && ui.id != editor.key) {
  30711. continue;
  30712. }
  30713. var itemUI = ui.execFn.call(editor, editor, toolbarItem);
  30714. if (itemUI) {
  30715. if (ui.index === undefined) {
  30716. toolbarUi.add(itemUI);
  30717. continue;
  30718. } else {
  30719. extraUIs.push({
  30720. index: ui.index,
  30721. itemUI: itemUI
  30722. });
  30723. }
  30724. }
  30725. }
  30726. }
  30727. //fullscreen这里单独处理一下,放到首行去
  30728. if (toolbarItem == "fullscreen") {
  30729. if (toolbarUis && toolbarUis[0]) {
  30730. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  30731. } else {
  30732. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  30733. }
  30734. continue;
  30735. }
  30736. } else {
  30737. toolbarItemUi = toolbarItem;
  30738. }
  30739. if (toolbarItemUi && toolbarItemUi.id) {
  30740. toolbarUi.add(toolbarItemUi);
  30741. }
  30742. }
  30743. toolbarUis[i] = toolbarUi;
  30744. }
  30745. //接受外部定制的UI
  30746. utils.each(extraUIs, function(obj) {
  30747. toolbarUi.add(obj.itemUI, obj.index);
  30748. });
  30749. this.toolbars = toolbarUis;
  30750. },
  30751. getHtmlTpl: function() {
  30752. return (
  30753. '<div id="##" class="%%">' +
  30754. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  30755. (this.toolbars.length
  30756. ? '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  30757. this.renderToolbarBoxHtml() +
  30758. "</div></div>"
  30759. : "") +
  30760. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  30761. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' +
  30762. this.editor.getLang("clickToUpload") +
  30763. "</div>" +
  30764. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  30765. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  30766. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  30767. "</div>" +
  30768. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  30769. "</div>" +
  30770. '<div id="##_iframeholder" class="%%-iframeholder">' +
  30771. "</div>" +
  30772. //modify wdcount by matao
  30773. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  30774. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  30775. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  30776. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  30777. "</tr></table></div>" +
  30778. '<div id="##_scalelayer"></div>' +
  30779. "</div>"
  30780. );
  30781. },
  30782. showWordImageDialog: function() {
  30783. this._dialogs["wordimageDialog"].open();
  30784. },
  30785. renderToolbarBoxHtml: function() {
  30786. var buff = [];
  30787. for (var i = 0; i < this.toolbars.length; i++) {
  30788. buff.push(this.toolbars[i].renderHtml());
  30789. }
  30790. return buff.join("");
  30791. },
  30792. setFullScreen: function(fullscreen) {
  30793. var editor = this.editor,
  30794. container = editor.container.parentNode.parentNode;
  30795. if (this._fullscreen != fullscreen) {
  30796. this._fullscreen = fullscreen;
  30797. this.editor.fireEvent("beforefullscreenchange", fullscreen);
  30798. if (baidu.editor.browser.gecko) {
  30799. var bk = editor.selection.getRange().createBookmark();
  30800. }
  30801. if (fullscreen) {
  30802. while (container.tagName != "BODY") {
  30803. var position = baidu.editor.dom.domUtils.getComputedStyle(
  30804. container,
  30805. "position"
  30806. );
  30807. nodeStack.push(position);
  30808. container.style.position = "static";
  30809. container = container.parentNode;
  30810. }
  30811. this._bakHtmlOverflow = document.documentElement.style.overflow;
  30812. this._bakBodyOverflow = document.body.style.overflow;
  30813. this._bakAutoHeight = this.editor.autoHeightEnabled;
  30814. this._bakScrollTop = Math.max(
  30815. document.documentElement.scrollTop,
  30816. document.body.scrollTop
  30817. );
  30818. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  30819. if (this._bakAutoHeight) {
  30820. //当全屏时不能执行自动长高
  30821. editor.autoHeightEnabled = false;
  30822. this.editor.disableAutoHeight();
  30823. }
  30824. document.documentElement.style.overflow = "hidden";
  30825. //修复,滚动条不收起的问题
  30826. window.scrollTo(0, window.scrollY);
  30827. this._bakCssText = this.getDom().style.cssText;
  30828. this._bakCssText1 = this.getDom("iframeholder").style.cssText;
  30829. editor.iframe.parentNode.style.width = "";
  30830. this._updateFullScreen();
  30831. } else {
  30832. while (container.tagName != "BODY") {
  30833. container.style.position = nodeStack.shift();
  30834. container = container.parentNode;
  30835. }
  30836. this.getDom().style.cssText = this._bakCssText;
  30837. this.getDom("iframeholder").style.cssText = this._bakCssText1;
  30838. if (this._bakAutoHeight) {
  30839. editor.autoHeightEnabled = true;
  30840. this.editor.enableAutoHeight();
  30841. }
  30842. document.documentElement.style.overflow = this._bakHtmlOverflow;
  30843. document.body.style.overflow = this._bakBodyOverflow;
  30844. editor.iframe.parentNode.style.width =
  30845. this._bakEditorContaninerWidth + "px";
  30846. window.scrollTo(0, this._bakScrollTop);
  30847. }
  30848. if (browser.gecko && editor.body.contentEditable === "true") {
  30849. var input = document.createElement("input");
  30850. document.body.appendChild(input);
  30851. editor.body.contentEditable = false;
  30852. setTimeout(function() {
  30853. input.focus();
  30854. setTimeout(function() {
  30855. editor.body.contentEditable = true;
  30856. editor.fireEvent("fullscreenchanged", fullscreen);
  30857. editor.selection.getRange().moveToBookmark(bk).select(true);
  30858. baidu.editor.dom.domUtils.remove(input);
  30859. fullscreen && window.scroll(0, 0);
  30860. }, 0);
  30861. }, 0);
  30862. }
  30863. if (editor.body.contentEditable === "true") {
  30864. this.editor.fireEvent("fullscreenchanged", fullscreen);
  30865. this.triggerLayout();
  30866. }
  30867. }
  30868. },
  30869. _updateFullScreen: function() {
  30870. if (this._fullscreen) {
  30871. var vpRect = uiUtils.getViewportRect();
  30872. this.getDom().style.cssText =
  30873. "border:0;position:absolute;left:0;top:" +
  30874. (this.editor.options.topOffset || 0) +
  30875. "px;width:" +
  30876. vpRect.width +
  30877. "px;height:" +
  30878. vpRect.height +
  30879. "px;z-index:" +
  30880. (this.getDom().style.zIndex * 1 + 100);
  30881. uiUtils.setViewportOffset(this.getDom(), {
  30882. left: 0,
  30883. top: this.editor.options.topOffset || 0
  30884. });
  30885. this.editor.setHeight(
  30886. vpRect.height -
  30887. this.getDom("toolbarbox").offsetHeight -
  30888. this.getDom("bottombar").offsetHeight -
  30889. (this.editor.options.topOffset || 0),
  30890. true
  30891. );
  30892. //不手动调一下,会导致全屏失效
  30893. if (browser.gecko) {
  30894. try {
  30895. window.onresize();
  30896. } catch (e) {}
  30897. }
  30898. }
  30899. },
  30900. _updateElementPath: function() {
  30901. var bottom = this.getDom("elementpath"),
  30902. list;
  30903. if (
  30904. this.elementPathEnabled &&
  30905. (list = this.editor.queryCommandValue("elementpath"))
  30906. ) {
  30907. var buff = [];
  30908. for (var i = 0, ci; (ci = list[i]); i++) {
  30909. buff[i] = this.formatHtml(
  30910. '<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' +
  30911. i +
  30912. '&quot;);">' +
  30913. ci +
  30914. "</span>"
  30915. );
  30916. }
  30917. bottom.innerHTML =
  30918. '<div class="edui-editor-breadcrumb" onmousedown="return false;">' +
  30919. this.editor.getLang("elementPathTip") +
  30920. ": " +
  30921. buff.join(" &gt; ") +
  30922. "</div>";
  30923. } else {
  30924. bottom.style.display = "none";
  30925. }
  30926. },
  30927. disableElementPath: function() {
  30928. var bottom = this.getDom("elementpath");
  30929. bottom.innerHTML = "";
  30930. bottom.style.display = "none";
  30931. this.elementPathEnabled = false;
  30932. },
  30933. enableElementPath: function() {
  30934. var bottom = this.getDom("elementpath");
  30935. bottom.style.display = "";
  30936. this.elementPathEnabled = true;
  30937. this._updateElementPath();
  30938. },
  30939. _scale: function() {
  30940. var doc = document,
  30941. editor = this.editor,
  30942. editorHolder = editor.container,
  30943. editorDocument = editor.document,
  30944. toolbarBox = this.getDom("toolbarbox"),
  30945. bottombar = this.getDom("bottombar"),
  30946. scale = this.getDom("scale"),
  30947. scalelayer = this.getDom("scalelayer");
  30948. var isMouseMove = false,
  30949. position = null,
  30950. minEditorHeight = 0,
  30951. minEditorWidth = editor.options.minFrameWidth,
  30952. pageX = 0,
  30953. pageY = 0,
  30954. scaleWidth = 0,
  30955. scaleHeight = 0;
  30956. function down() {
  30957. position = domUtils.getXY(editorHolder);
  30958. if (!minEditorHeight) {
  30959. minEditorHeight =
  30960. editor.options.minFrameHeight +
  30961. toolbarBox.offsetHeight +
  30962. bottombar.offsetHeight;
  30963. }
  30964. scalelayer.style.cssText =
  30965. "position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:" +
  30966. editorHolder.offsetWidth +
  30967. "px;height:" +
  30968. editorHolder.offsetHeight +
  30969. "px;z-index:" +
  30970. (editor.options.zIndex + 1);
  30971. domUtils.on(doc, "mousemove", move);
  30972. domUtils.on(editorDocument, "mouseup", up);
  30973. domUtils.on(doc, "mouseup", up);
  30974. }
  30975. var me = this;
  30976. //by xuheng 全屏时关掉缩放
  30977. this.editor.addListener("fullscreenchanged", function(e, fullScreen) {
  30978. if (fullScreen) {
  30979. me.disableScale();
  30980. } else {
  30981. if (me.editor.options.scaleEnabled) {
  30982. me.enableScale();
  30983. var tmpNode = me.editor.document.createElement("span");
  30984. me.editor.body.appendChild(tmpNode);
  30985. me.editor.body.style.height =
  30986. Math.max(
  30987. domUtils.getXY(tmpNode).y,
  30988. me.editor.iframe.offsetHeight - 20
  30989. ) + "px";
  30990. domUtils.remove(tmpNode);
  30991. }
  30992. }
  30993. });
  30994. function move(event) {
  30995. clearSelection();
  30996. var e = event || window.event;
  30997. pageX = e.pageX || doc.documentElement.scrollLeft + e.clientX;
  30998. pageY = e.pageY || doc.documentElement.scrollTop + e.clientY;
  30999. scaleWidth = pageX - position.x;
  31000. scaleHeight = pageY - position.y;
  31001. if (scaleWidth >= minEditorWidth) {
  31002. isMouseMove = true;
  31003. scalelayer.style.width = scaleWidth + "px";
  31004. }
  31005. if (scaleHeight >= minEditorHeight) {
  31006. isMouseMove = true;
  31007. scalelayer.style.height = scaleHeight + "px";
  31008. }
  31009. }
  31010. function up() {
  31011. if (isMouseMove) {
  31012. isMouseMove = false;
  31013. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  31014. editorHolder.style.width = editor.ui._actualFrameWidth + "px";
  31015. editor.setHeight(
  31016. scalelayer.offsetHeight -
  31017. bottombar.offsetHeight -
  31018. toolbarBox.offsetHeight -
  31019. 2,
  31020. true
  31021. );
  31022. }
  31023. if (scalelayer) {
  31024. scalelayer.style.display = "none";
  31025. }
  31026. clearSelection();
  31027. domUtils.un(doc, "mousemove", move);
  31028. domUtils.un(editorDocument, "mouseup", up);
  31029. domUtils.un(doc, "mouseup", up);
  31030. }
  31031. function clearSelection() {
  31032. if (browser.ie) doc.selection.clear();
  31033. else window.getSelection().removeAllRanges();
  31034. }
  31035. this.enableScale = function() {
  31036. //trace:2868
  31037. if (editor.queryCommandState("source") == 1) return;
  31038. scale.style.display = "";
  31039. this.scaleEnabled = true;
  31040. domUtils.on(scale, "mousedown", down);
  31041. };
  31042. this.disableScale = function() {
  31043. scale.style.display = "none";
  31044. this.scaleEnabled = false;
  31045. domUtils.un(scale, "mousedown", down);
  31046. };
  31047. },
  31048. isFullScreen: function() {
  31049. return this._fullscreen;
  31050. },
  31051. postRender: function() {
  31052. UIBase.prototype.postRender.call(this);
  31053. for (var i = 0; i < this.toolbars.length; i++) {
  31054. this.toolbars[i].postRender();
  31055. }
  31056. var me = this;
  31057. var timerId,
  31058. domUtils = baidu.editor.dom.domUtils,
  31059. updateFullScreenTime = function() {
  31060. clearTimeout(timerId);
  31061. timerId = setTimeout(function() {
  31062. me._updateFullScreen();
  31063. });
  31064. };
  31065. domUtils.on(window, "resize", updateFullScreenTime);
  31066. me.addListener("destroy", function() {
  31067. domUtils.un(window, "resize", updateFullScreenTime);
  31068. clearTimeout(timerId);
  31069. });
  31070. },
  31071. showToolbarMsg: function(msg, flag) {
  31072. this.getDom("toolbarmsg_label").innerHTML = msg;
  31073. this.getDom("toolbarmsg").style.display = "";
  31074. //
  31075. if (!flag) {
  31076. var w = this.getDom("upload_dialog");
  31077. w.style.display = "none";
  31078. }
  31079. },
  31080. hideToolbarMsg: function() {
  31081. this.getDom("toolbarmsg").style.display = "none";
  31082. },
  31083. mapUrl: function(url) {
  31084. return url
  31085. ? url.replace("~/", this.editor.options.UEDITOR_HOME_URL || "")
  31086. : "";
  31087. },
  31088. triggerLayout: function() {
  31089. var dom = this.getDom();
  31090. if (dom.style.zoom == "1") {
  31091. dom.style.zoom = "100%";
  31092. } else {
  31093. dom.style.zoom = "1";
  31094. }
  31095. }
  31096. };
  31097. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  31098. var instances = {};
  31099. UE.ui.Editor = function(options) {
  31100. var editor = new UE.Editor(options);
  31101. editor.options.editor = editor;
  31102. utils.loadFile(document, {
  31103. href:
  31104. editor.options.themePath + editor.options.theme + "/css/ueditor.css",
  31105. tag: "link",
  31106. type: "text/css",
  31107. rel: "stylesheet"
  31108. });
  31109. var oldRender = editor.render;
  31110. editor.render = function(holder) {
  31111. if (holder.constructor === String) {
  31112. editor.key = holder;
  31113. instances[holder] = editor;
  31114. }
  31115. utils.domReady(function() {
  31116. editor.langIsReady
  31117. ? renderUI()
  31118. : editor.addListener("langReady", renderUI);
  31119. function renderUI() {
  31120. editor.setOpt({
  31121. labelMap: editor.options.labelMap || editor.getLang("labelMap")
  31122. });
  31123. new EditorUI(editor.options);
  31124. if (holder) {
  31125. if (holder.constructor === String) {
  31126. holder = document.getElementById(holder);
  31127. }
  31128. holder &&
  31129. holder.getAttribute("name") &&
  31130. (editor.options.textarea = holder.getAttribute("name"));
  31131. if (holder && /script|textarea/gi.test(holder.tagName)) {
  31132. var newDiv = document.createElement("div");
  31133. holder.parentNode.insertBefore(newDiv, holder);
  31134. var cont = holder.value || holder.innerHTML;
  31135. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont)
  31136. ? editor.options.initialContent
  31137. : cont
  31138. .replace(/>[\n\r\t]+([ ]{4})+/g, ">")
  31139. .replace(/[\n\r\t]+([ ]{4})+</g, "<")
  31140. .replace(/>[\n\r\t]+</g, "><");
  31141. holder.className && (newDiv.className = holder.className);
  31142. holder.style.cssText &&
  31143. (newDiv.style.cssText = holder.style.cssText);
  31144. if (/textarea/i.test(holder.tagName)) {
  31145. editor.textarea = holder;
  31146. editor.textarea.style.display = "none";
  31147. } else {
  31148. holder.parentNode.removeChild(holder);
  31149. }
  31150. if (holder.id) {
  31151. newDiv.id = holder.id;
  31152. domUtils.removeAttributes(holder, "id");
  31153. }
  31154. holder = newDiv;
  31155. holder.innerHTML = "";
  31156. }
  31157. }
  31158. domUtils.addClass(holder, "edui-" + editor.options.theme);
  31159. editor.ui.render(holder);
  31160. var opt = editor.options;
  31161. //给实例添加一个编辑器的容器引用
  31162. editor.container = editor.ui.getDom();
  31163. var parents = domUtils.findParents(holder, true);
  31164. var displays = [];
  31165. for (var i = 0, ci; (ci = parents[i]); i++) {
  31166. displays[i] = ci.style.display;
  31167. ci.style.display = "block";
  31168. }
  31169. if (opt.initialFrameWidth) {
  31170. opt.minFrameWidth = opt.initialFrameWidth;
  31171. } else {
  31172. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  31173. var styleWidth = holder.style.width;
  31174. if (/%$/.test(styleWidth)) {
  31175. opt.initialFrameWidth = styleWidth;
  31176. }
  31177. }
  31178. if (opt.initialFrameHeight) {
  31179. opt.minFrameHeight = opt.initialFrameHeight;
  31180. } else {
  31181. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  31182. }
  31183. for (var i = 0, ci; (ci = parents[i]); i++) {
  31184. ci.style.display = displays[i];
  31185. }
  31186. //编辑器最外容器设置了高度,会导致,编辑器不占位
  31187. //todo 先去掉,没有找到原因
  31188. if (holder.style.height) {
  31189. holder.style.height = "";
  31190. }
  31191. editor.container.style.width =
  31192. opt.initialFrameWidth +
  31193. (/%$/.test(opt.initialFrameWidth) ? "" : "px");
  31194. editor.container.style.zIndex = opt.zIndex;
  31195. oldRender.call(editor, editor.ui.getDom("iframeholder"));
  31196. editor.fireEvent("afteruiready");
  31197. }
  31198. });
  31199. };
  31200. return editor;
  31201. };
  31202. /**
  31203. * @file
  31204. * @name UE
  31205. * @short UE
  31206. * @desc UEditor的顶部命名空间
  31207. */
  31208. /**
  31209. * @name getEditor
  31210. * @since 1.2.4+
  31211. * @grammar UE.getEditor(id,[opt]) => Editor实例
  31212. * @desc 提供一个全局的方法得到编辑器实例
  31213. *
  31214. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  31215. * * ''opt'' 编辑器的可选参数
  31216. * @example
  31217. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  31218. * this.setContent('hello')
  31219. * }});
  31220. * UE.getEditor('containerId'); //返回刚创建的实例
  31221. *
  31222. */
  31223. UE.getEditor = function(id, opt) {
  31224. var editor = instances[id];
  31225. if (!editor) {
  31226. editor = instances[id] = new UE.ui.Editor(opt);
  31227. try{
  31228. editor.render(id);
  31229. } catch (e) {
  31230. }
  31231. }
  31232. return editor;
  31233. };
  31234. UE.delEditor = function(id) {
  31235. var editor;
  31236. if ((editor = instances[id])) {
  31237. editor.key && editor.destroy();
  31238. delete instances[id];
  31239. }
  31240. };
  31241. UE.registerUI = function(uiName, fn, index, editorId) {
  31242. utils.each(uiName.split(/\s+/), function(name) {
  31243. baidu.editor.ui[name] = {
  31244. id: editorId,
  31245. execFn: fn,
  31246. index: index
  31247. };
  31248. });
  31249. };
  31250. })();
  31251. // adapter/message.js
  31252. UE.registerUI("message", function(editor) {
  31253. var editorui = baidu.editor.ui;
  31254. var Message = editorui.Message;
  31255. var holder;
  31256. var _messageItems = [];
  31257. var me = editor;
  31258. me.setOpt("enableMessageShow", true);
  31259. if (me.getOpt("enableMessageShow") === false) {
  31260. return;
  31261. }
  31262. me.addListener("ready", function() {
  31263. holder = document.getElementById(me.ui.id + "_message_holder");
  31264. updateHolderPos();
  31265. setTimeout(function() {
  31266. updateHolderPos();
  31267. }, 500);
  31268. });
  31269. me.addListener("showmessage", function(type, opt) {
  31270. opt = utils.isString(opt)
  31271. ? {
  31272. content: opt
  31273. }
  31274. : opt;
  31275. var message = new Message({
  31276. timeout: opt.timeout,
  31277. type: opt.type,
  31278. content: opt.content,
  31279. keepshow: opt.keepshow,
  31280. editor: me
  31281. }),
  31282. mid = opt.id || "msg_" + (+new Date()).toString(36);
  31283. message.render(holder);
  31284. _messageItems[mid] = message;
  31285. message.reset(opt);
  31286. updateHolderPos();
  31287. return mid;
  31288. });
  31289. me.addListener("updatemessage", function(type, id, opt) {
  31290. opt = utils.isString(opt)
  31291. ? {
  31292. content: opt
  31293. }
  31294. : opt;
  31295. var message = _messageItems[id];
  31296. message.render(holder);
  31297. message && message.reset(opt);
  31298. });
  31299. me.addListener("hidemessage", function(type, id) {
  31300. var message = _messageItems[id];
  31301. message && message.hide();
  31302. });
  31303. function updateHolderPos() {
  31304. if (!holder || !me.ui) return;
  31305. var toolbarbox = me.ui.getDom("toolbarbox");
  31306. if (toolbarbox) {
  31307. holder.style.top = toolbarbox.offsetHeight + 3 + "px";
  31308. }
  31309. holder.style.zIndex =
  31310. Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1;
  31311. }
  31312. });
  31313. // adapter/autosave.js
  31314. UE.registerUI("autosave", function(editor) {
  31315. var timer = null,
  31316. uid = null;
  31317. editor.on("afterautosave", function() {
  31318. clearTimeout(timer);
  31319. timer = setTimeout(function() {
  31320. if (uid) {
  31321. editor.trigger("hidemessage", uid);
  31322. }
  31323. uid = editor.trigger("showmessage", {
  31324. content: editor.getLang("autosave.success"),
  31325. timeout: 2000
  31326. });
  31327. }, 2000);
  31328. });
  31329. });
  31330. })();