Editor.js 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287
  1. /**
  2. * Editor.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function(tinymce) {
  11. // Shorten these names
  12. var DOM = tinymce.DOM, Event = tinymce.dom.Event, extend = tinymce.extend,
  13. Dispatcher = tinymce.util.Dispatcher, each = tinymce.each, isGecko = tinymce.isGecko,
  14. isIE = tinymce.isIE, isWebKit = tinymce.isWebKit, is = tinymce.is,
  15. ThemeManager = tinymce.ThemeManager, PluginManager = tinymce.PluginManager,
  16. inArray = tinymce.inArray, grep = tinymce.grep, explode = tinymce.explode;
  17. /**
  18. * This class contains the core logic for a TinyMCE editor.
  19. *
  20. * @class tinymce.Editor
  21. * @example
  22. * // Add a class to all paragraphs in the editor.
  23. * tinyMCE.activeEditor.dom.addClass(tinyMCE.activeEditor.dom.select('p'), 'someclass');
  24. *
  25. * // Gets the current editors selection as text
  26. * tinyMCE.activeEditor.selection.getContent({format : 'text'});
  27. *
  28. * // Creates a new editor instance
  29. * var ed = new tinymce.Editor('textareaid', {
  30. * some_setting : 1
  31. * });
  32. *
  33. * // Select each item the user clicks on
  34. * ed.onClick.add(function(ed, e) {
  35. * ed.selection.select(e.target);
  36. * });
  37. *
  38. * ed.render();
  39. */
  40. tinymce.create('tinymce.Editor', {
  41. /**
  42. * Constructs a editor instance by id.
  43. *
  44. * @constructor
  45. * @method Editor
  46. * @param {String} id Unique id for the editor.
  47. * @param {Object} s Optional settings string for the editor.
  48. * @author Moxiecode
  49. */
  50. Editor : function(id, s) {
  51. var t = this;
  52. /**
  53. * Editor instance id, normally the same as the div/textarea that was replaced.
  54. *
  55. * @property id
  56. * @type String
  57. */
  58. t.id = t.editorId = id;
  59. t.execCommands = {};
  60. t.queryStateCommands = {};
  61. t.queryValueCommands = {};
  62. /**
  63. * State to force the editor to return false on a isDirty call.
  64. *
  65. * @property isNotDirty
  66. * @type Boolean
  67. * @example
  68. * function ajaxSave() {
  69. * var ed = tinyMCE.get('elm1');
  70. *
  71. * // Save contents using some XHR call
  72. * alert(ed.getContent());
  73. *
  74. * ed.isNotDirty = 1; // Force not dirty state
  75. * }
  76. */
  77. t.isNotDirty = false;
  78. /**
  79. * Name/Value object containting plugin instances.
  80. *
  81. * @property plugins
  82. * @type Object
  83. * @example
  84. * // Execute a method inside a plugin directly
  85. * tinyMCE.activeEditor.plugins.someplugin.someMethod();
  86. */
  87. t.plugins = {};
  88. // Add events to the editor
  89. each([
  90. /**
  91. * Fires before the initialization of the editor.
  92. *
  93. * @event onPreInit
  94. * @param {tinymce.Editor} sender Editor instance.
  95. * @see #onInit
  96. * @example
  97. * // Adds an observer to the onPreInit event using tinyMCE.init
  98. * tinyMCE.init({
  99. * ...
  100. * setup : function(ed) {
  101. * ed.onPreInit.add(function(ed) {
  102. * console.debug('PreInit: ' + ed.id);
  103. * });
  104. * }
  105. * });
  106. */
  107. 'onPreInit',
  108. /**
  109. * Fires before the initialization of the editor.
  110. *
  111. * @event onBeforeRenderUI
  112. * @param {tinymce.Editor} sender Editor instance.
  113. * @example
  114. * // Adds an observer to the onBeforeRenderUI event using tinyMCE.init
  115. * tinyMCE.init({
  116. * ...
  117. * setup : function(ed) {
  118. * ed.onBeforeRenderUI.add(function(ed, cm) {
  119. * console.debug('Before render: ' + ed.id);
  120. * });
  121. * }
  122. * });
  123. */
  124. 'onBeforeRenderUI',
  125. /**
  126. * Fires after the rendering has completed.
  127. *
  128. * @event onPostRender
  129. * @param {tinymce.Editor} sender Editor instance.
  130. * @example
  131. * // Adds an observer to the onPostRender event using tinyMCE.init
  132. * tinyMCE.init({
  133. * ...
  134. * setup : function(ed) {
  135. * ed.onPostRender.add(function(ed, cm) {
  136. * console.debug('After render: ' + ed.id);
  137. * });
  138. * }
  139. * });
  140. */
  141. 'onPostRender',
  142. /**
  143. * Fires after the initialization of the editor is done.
  144. *
  145. * @event onInit
  146. * @param {tinymce.Editor} sender Editor instance.
  147. * @see #onPreInit
  148. * @example
  149. * // Adds an observer to the onInit event using tinyMCE.init
  150. * tinyMCE.init({
  151. * ...
  152. * setup : function(ed) {
  153. * ed.onInit.add(function(ed) {
  154. * console.debug('Editor is done: ' + ed.id);
  155. * });
  156. * }
  157. * });
  158. */
  159. 'onInit',
  160. /**
  161. * Fires when the editor instance is removed from page.
  162. *
  163. * @event onRemove
  164. * @param {tinymce.Editor} sender Editor instance.
  165. * @example
  166. * // Adds an observer to the onRemove event using tinyMCE.init
  167. * tinyMCE.init({
  168. * ...
  169. * setup : function(ed) {
  170. * ed.onRemove.add(function(ed) {
  171. * console.debug('Editor was removed: ' + ed.id);
  172. * });
  173. * }
  174. * });
  175. */
  176. 'onRemove',
  177. /**
  178. * Fires when the editor is activated.
  179. *
  180. * @event onActivate
  181. * @param {tinymce.Editor} sender Editor instance.
  182. * @example
  183. * // Adds an observer to the onActivate event using tinyMCE.init
  184. * tinyMCE.init({
  185. * ...
  186. * setup : function(ed) {
  187. * ed.onActivate.add(function(ed) {
  188. * console.debug('Editor was activated: ' + ed.id);
  189. * });
  190. * }
  191. * });
  192. */
  193. 'onActivate',
  194. /**
  195. * Fires when the editor is deactivated.
  196. *
  197. * @event onDeactivate
  198. * @param {tinymce.Editor} sender Editor instance.
  199. * @example
  200. * // Adds an observer to the onDeactivate event using tinyMCE.init
  201. * tinyMCE.init({
  202. * ...
  203. * setup : function(ed) {
  204. * ed.onDeactivate.add(function(ed) {
  205. * console.debug('Editor was deactivated: ' + ed.id);
  206. * });
  207. * }
  208. * });
  209. */
  210. 'onDeactivate',
  211. /**
  212. * Fires when something in the body of the editor is clicked.
  213. *
  214. * @event onClick
  215. * @param {tinymce.Editor} sender Editor instance.
  216. * @param {Event} evt W3C DOM Event instance.
  217. * @example
  218. * // Adds an observer to the onClick event using tinyMCE.init
  219. * tinyMCE.init({
  220. * ...
  221. * setup : function(ed) {
  222. * ed.onClick.add(function(ed, e) {
  223. * console.debug('Editor was clicked: ' + e.target.nodeName);
  224. * });
  225. * }
  226. * });
  227. */
  228. 'onClick',
  229. /**
  230. * Fires when a registered event is intercepted.
  231. *
  232. * @event onEvent
  233. * @param {tinymce.Editor} sender Editor instance.
  234. * @param {Event} evt W3C DOM Event instance.
  235. * @example
  236. * // Adds an observer to the onEvent event using tinyMCE.init
  237. * tinyMCE.init({
  238. * ...
  239. * setup : function(ed) {
  240. * ed.onEvent.add(function(ed, e) {
  241. * console.debug('Editor event occured: ' + e.target.nodeName);
  242. * });
  243. * }
  244. * });
  245. */
  246. 'onEvent',
  247. /**
  248. * Fires when a mouseup event is intercepted inside the editor.
  249. *
  250. * @event onMouseUp
  251. * @param {tinymce.Editor} sender Editor instance.
  252. * @param {Event} evt W3C DOM Event instance.
  253. * @example
  254. * // Adds an observer to the onMouseUp event using tinyMCE.init
  255. * tinyMCE.init({
  256. * ...
  257. * setup : function(ed) {
  258. * ed.onMouseUp.add(function(ed, e) {
  259. * console.debug('Mouse up event: ' + e.target.nodeName);
  260. * });
  261. * }
  262. * });
  263. */
  264. 'onMouseUp',
  265. /**
  266. * Fires when a mousedown event is intercepted inside the editor.
  267. *
  268. * @event onMouseDown
  269. * @param {tinymce.Editor} sender Editor instance.
  270. * @param {Event} evt W3C DOM Event instance.
  271. * @example
  272. * // Adds an observer to the onMouseDown event using tinyMCE.init
  273. * tinyMCE.init({
  274. * ...
  275. * setup : function(ed) {
  276. * ed.onMouseDown.add(function(ed, e) {
  277. * console.debug('Mouse down event: ' + e.target.nodeName);
  278. * });
  279. * }
  280. * });
  281. */
  282. 'onMouseDown',
  283. /**
  284. * Fires when a dblclick event is intercepted inside the editor.
  285. *
  286. * @event onDblClick
  287. * @param {tinymce.Editor} sender Editor instance.
  288. * @param {Event} evt W3C DOM Event instance.
  289. * @example
  290. * // Adds an observer to the onDblClick event using tinyMCE.init
  291. * tinyMCE.init({
  292. * ...
  293. * setup : function(ed) {
  294. * ed.onDblClick.add(function(ed, e) {
  295. * console.debug('Double click event: ' + e.target.nodeName);
  296. * });
  297. * }
  298. * });
  299. */
  300. 'onDblClick',
  301. /**
  302. * Fires when a keydown event is intercepted inside the editor.
  303. *
  304. * @event onKeyDown
  305. * @param {tinymce.Editor} sender Editor instance.
  306. * @param {Event} evt W3C DOM Event instance.
  307. * @example
  308. * // Adds an observer to the onKeyDown event using tinyMCE.init
  309. * tinyMCE.init({
  310. * ...
  311. * setup : function(ed) {
  312. * ed.onKeyDown.add(function(ed, e) {
  313. * console.debug('Key down event: ' + e.keyCode);
  314. * });
  315. * }
  316. * });
  317. */
  318. 'onKeyDown',
  319. /**
  320. * Fires when a keydown event is intercepted inside the editor.
  321. *
  322. * @event onKeyUp
  323. * @param {tinymce.Editor} sender Editor instance.
  324. * @param {Event} evt W3C DOM Event instance.
  325. * @example
  326. * // Adds an observer to the onKeyUp event using tinyMCE.init
  327. * tinyMCE.init({
  328. * ...
  329. * setup : function(ed) {
  330. * ed.onKeyUp.add(function(ed, e) {
  331. * console.debug('Key up event: ' + e.keyCode);
  332. * });
  333. * }
  334. * });
  335. */
  336. 'onKeyUp',
  337. /**
  338. * Fires when a keypress event is intercepted inside the editor.
  339. *
  340. * @event onKeyPress
  341. * @param {tinymce.Editor} sender Editor instance.
  342. * @param {Event} evt W3C DOM Event instance.
  343. * @example
  344. * // Adds an observer to the onKeyPress event using tinyMCE.init
  345. * tinyMCE.init({
  346. * ...
  347. * setup : function(ed) {
  348. * ed.onKeyPress.add(function(ed, e) {
  349. * console.debug('Key press event: ' + e.keyCode);
  350. * });
  351. * }
  352. * });
  353. */
  354. 'onKeyPress',
  355. /**
  356. * Fires when a contextmenu event is intercepted inside the editor.
  357. *
  358. * @event onContextMenu
  359. * @param {tinymce.Editor} sender Editor instance.
  360. * @param {Event} evt W3C DOM Event instance.
  361. * @example
  362. * // Adds an observer to the onContextMenu event using tinyMCE.init
  363. * tinyMCE.init({
  364. * ...
  365. * setup : function(ed) {
  366. * ed.onContextMenu.add(function(ed, e) {
  367. * console.debug('Context menu event:' + e.target);
  368. * });
  369. * }
  370. * });
  371. */
  372. 'onContextMenu',
  373. /**
  374. * Fires when a form submit event is intercepted.
  375. *
  376. * @event onSubmit
  377. * @param {tinymce.Editor} sender Editor instance.
  378. * @param {Event} evt W3C DOM Event instance.
  379. * @example
  380. * // Adds an observer to the onSubmit event using tinyMCE.init
  381. * tinyMCE.init({
  382. * ...
  383. * setup : function(ed) {
  384. * ed.onSubmit.add(function(ed, e) {
  385. * console.debug('Form submit:' + e.target);
  386. * });
  387. * }
  388. * });
  389. */
  390. 'onSubmit',
  391. /**
  392. * Fires when a form reset event is intercepted.
  393. *
  394. * @event onReset
  395. * @param {tinymce.Editor} sender Editor instance.
  396. * @param {Event} evt W3C DOM Event instance.
  397. * @example
  398. * // Adds an observer to the onReset event using tinyMCE.init
  399. * tinyMCE.init({
  400. * ...
  401. * setup : function(ed) {
  402. * ed.onReset.add(function(ed, e) {
  403. * console.debug('Form reset:' + e.target);
  404. * });
  405. * }
  406. * });
  407. */
  408. 'onReset',
  409. /**
  410. * Fires when a paste event is intercepted inside the editor.
  411. *
  412. * @event onPaste
  413. * @param {tinymce.Editor} sender Editor instance.
  414. * @param {Event} evt W3C DOM Event instance.
  415. * @example
  416. * // Adds an observer to the onPaste event using tinyMCE.init
  417. * tinyMCE.init({
  418. * ...
  419. * setup : function(ed) {
  420. * ed.onPaste.add(function(ed, e) {
  421. * console.debug('Pasted plain text');
  422. * });
  423. * }
  424. * });
  425. */
  426. 'onPaste',
  427. /**
  428. * Fires when the Serializer does a preProcess on the contents.
  429. *
  430. * @event onPreProcess
  431. * @param {tinymce.Editor} sender Editor instance.
  432. * @param {Object} obj PreProcess object.
  433. * @option {Node} node DOM node for the item being serialized.
  434. * @option {String} format The specified output format normally "html".
  435. * @option {Boolean} get Is true if the process is on a getContent operation.
  436. * @option {Boolean} set Is true if the process is on a setContent operation.
  437. * @option {Boolean} cleanup Is true if the process is on a cleanup operation.
  438. * @example
  439. * // Adds an observer to the onPreProcess event using tinyMCE.init
  440. * tinyMCE.init({
  441. * ...
  442. * setup : function(ed) {
  443. * ed.onPreProcess.add(function(ed, o) {
  444. * // Add a class to each paragraph in the editor
  445. * ed.dom.addClass(ed.dom.select('p', o.node), 'myclass');
  446. * });
  447. * }
  448. * });
  449. */
  450. 'onPreProcess',
  451. /**
  452. * Fires when the Serializer does a postProcess on the contents.
  453. *
  454. * @event onPostProcess
  455. * @param {tinymce.Editor} sender Editor instance.
  456. * @param {Object} obj PreProcess object.
  457. * @example
  458. * // Adds an observer to the onPostProcess event using tinyMCE.init
  459. * tinyMCE.init({
  460. * ...
  461. * setup : function(ed) {
  462. * ed.onPostProcess.add(function(ed, o) {
  463. * // Remove all paragraphs and replace with BR
  464. * o.content = o.content.replace(/<p[^>]+>|<p>/g, '');
  465. * o.content = o.content.replace(/<\/p>/g, '<br />');
  466. * });
  467. * }
  468. * });
  469. */
  470. 'onPostProcess',
  471. /**
  472. * Fires before new contents is added to the editor. Using for example setContent.
  473. *
  474. * @event onBeforeSetContent
  475. * @param {tinymce.Editor} sender Editor instance.
  476. * @example
  477. * // Adds an observer to the onBeforeSetContent event using tinyMCE.init
  478. * tinyMCE.init({
  479. * ...
  480. * setup : function(ed) {
  481. * ed.onBeforeSetContent.add(function(ed, o) {
  482. * // Replaces all a characters with b characters
  483. * o.content = o.content.replace(/a/g, 'b');
  484. * });
  485. * }
  486. * });
  487. */
  488. 'onBeforeSetContent',
  489. /**
  490. * Fires before contents is extracted from the editor using for example getContent.
  491. *
  492. * @event onBeforeGetContent
  493. * @param {tinymce.Editor} sender Editor instance.
  494. * @param {Event} evt W3C DOM Event instance.
  495. * @example
  496. * // Adds an observer to the onBeforeGetContent event using tinyMCE.init
  497. * tinyMCE.init({
  498. * ...
  499. * setup : function(ed) {
  500. * ed.onBeforeGetContent.add(function(ed, o) {
  501. * console.debug('Before get content.');
  502. * });
  503. * }
  504. * });
  505. */
  506. 'onBeforeGetContent',
  507. /**
  508. * Fires after the contents has been added to the editor using for example onSetContent.
  509. *
  510. * @event onSetContent
  511. * @param {tinymce.Editor} sender Editor instance.
  512. * @example
  513. * // Adds an observer to the onSetContent event using tinyMCE.init
  514. * tinyMCE.init({
  515. * ...
  516. * setup : function(ed) {
  517. * ed.onSetContent.add(function(ed, o) {
  518. * // Replaces all a characters with b characters
  519. * o.content = o.content.replace(/a/g, 'b');
  520. * });
  521. * }
  522. * });
  523. */
  524. 'onSetContent',
  525. /**
  526. * Fires after the contents has been extracted from the editor using for example getContent.
  527. *
  528. * @event onGetContent
  529. * @param {tinymce.Editor} sender Editor instance.
  530. * @example
  531. * // Adds an observer to the onGetContent event using tinyMCE.init
  532. * tinyMCE.init({
  533. * ...
  534. * setup : function(ed) {
  535. * ed.onGetContent.add(function(ed, o) {
  536. * // Replace all a characters with b
  537. * o.content = o.content.replace(/a/g, 'b');
  538. * });
  539. * }
  540. * });
  541. */
  542. 'onGetContent',
  543. /**
  544. * Fires when the editor gets loaded with contents for example when the load method is executed.
  545. *
  546. * @event onLoadContent
  547. * @param {tinymce.Editor} sender Editor instance.
  548. * @example
  549. * // Adds an observer to the onLoadContent event using tinyMCE.init
  550. * tinyMCE.init({
  551. * ...
  552. * setup : function(ed) {
  553. * ed.onLoadContent.add(function(ed, o) {
  554. * // Output the element name
  555. * console.debug(o.element.nodeName);
  556. * });
  557. * }
  558. * });
  559. */
  560. 'onLoadContent',
  561. /**
  562. * Fires when the editor contents gets saved for example when the save method is executed.
  563. *
  564. * @event onSaveContent
  565. * @param {tinymce.Editor} sender Editor instance.
  566. * @example
  567. * // Adds an observer to the onSaveContent event using tinyMCE.init
  568. * tinyMCE.init({
  569. * ...
  570. * setup : function(ed) {
  571. * ed.onSaveContent.add(function(ed, o) {
  572. * // Output the element name
  573. * console.debug(o.element.nodeName);
  574. * });
  575. * }
  576. * });
  577. */
  578. 'onSaveContent',
  579. /**
  580. * Fires when the user changes node location using the mouse or keyboard.
  581. *
  582. * @event onNodeChange
  583. * @param {tinymce.Editor} sender Editor instance.
  584. * @example
  585. * // Adds an observer to the onNodeChange event using tinyMCE.init
  586. * tinyMCE.init({
  587. * ...
  588. * setup : function(ed) {
  589. * ed.onNodeChange.add(function(ed, cm, e) {
  590. * // Activates the link button when the caret is placed in a anchor element
  591. * if (e.nodeName == 'A')
  592. * cm.setActive('link', true);
  593. * });
  594. * }
  595. * });
  596. */
  597. 'onNodeChange',
  598. /**
  599. * Fires when a new undo level is added to the editor.
  600. *
  601. * @event onChange
  602. * @param {tinymce.Editor} sender Editor instance.
  603. * @example
  604. * // Adds an observer to the onChange event using tinyMCE.init
  605. * tinyMCE.init({
  606. * ...
  607. * setup : function(ed) {
  608. * ed.onChange.add(function(ed, l) {
  609. * console.debug('Editor contents was modified. Contents: ' + l.content);
  610. * });
  611. * }
  612. * });
  613. */
  614. 'onChange',
  615. /**
  616. * Fires before a command gets executed for example "Bold".
  617. *
  618. * @event onBeforeExecCommand
  619. * @param {tinymce.Editor} sender Editor instance.
  620. * @example
  621. * // Adds an observer to the onBeforeExecCommand event using tinyMCE.init
  622. * tinyMCE.init({
  623. * ...
  624. * setup : function(ed) {
  625. * ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
  626. * console.debug('Command is to be executed: ' + cmd);
  627. * });
  628. * }
  629. * });
  630. */
  631. 'onBeforeExecCommand',
  632. /**
  633. * Fires after a command is executed for example "Bold".
  634. *
  635. * @event onExecCommand
  636. * @param {tinymce.Editor} sender Editor instance.
  637. * @example
  638. * // Adds an observer to the onExecCommand event using tinyMCE.init
  639. * tinyMCE.init({
  640. * ...
  641. * setup : function(ed) {
  642. * ed.onExecCommand.add(function(ed, cmd, ui, val) {
  643. * console.debug('Command was executed: ' + cmd);
  644. * });
  645. * }
  646. * });
  647. */
  648. 'onExecCommand',
  649. /**
  650. * Fires when the contents is undo:ed.
  651. *
  652. * @event onUndo
  653. * @param {tinymce.Editor} sender Editor instance.
  654. * @param {Object} level Undo level object.
  655. * @ example
  656. * // Adds an observer to the onUndo event using tinyMCE.init
  657. * tinyMCE.init({
  658. * ...
  659. * setup : function(ed) {
  660. * ed.onUndo.add(function(ed, level) {
  661. * console.debug('Undo was performed: ' + level.content);
  662. * });
  663. * }
  664. * });
  665. */
  666. 'onUndo',
  667. /**
  668. * Fires when the contents is redo:ed.
  669. *
  670. * @event onRedo
  671. * @param {tinymce.Editor} sender Editor instance.
  672. * @param {Object} level Undo level object.
  673. * @example
  674. * // Adds an observer to the onRedo event using tinyMCE.init
  675. * tinyMCE.init({
  676. * ...
  677. * setup : function(ed) {
  678. * ed.onRedo.add(function(ed, level) {
  679. * console.debug('Redo was performed: ' +level.content);
  680. * });
  681. * }
  682. * });
  683. */
  684. 'onRedo',
  685. /**
  686. * Fires when visual aids is enabled/disabled.
  687. *
  688. * @event onVisualAid
  689. * @param {tinymce.Editor} sender Editor instance.
  690. * @example
  691. * // Adds an observer to the onVisualAid event using tinyMCE.init
  692. * tinyMCE.init({
  693. * ...
  694. * setup : function(ed) {
  695. * ed.onVisualAid.add(function(ed, e, s) {
  696. * console.debug('onVisualAid event: ' + ed.id + ", State: " + s);
  697. * });
  698. * }
  699. * });
  700. */
  701. 'onVisualAid',
  702. /**
  703. * Fires when the progress throbber is shown above the editor.
  704. *
  705. * @event onSetProgressState
  706. * @param {tinymce.Editor} sender Editor instance.
  707. * @example
  708. * // Adds an observer to the onSetProgressState event using tinyMCE.init
  709. * tinyMCE.init({
  710. * ...
  711. * setup : function(ed) {
  712. * ed.onSetProgressState.add(function(ed, b) {
  713. * if (b)
  714. * console.debug('SHOW!');
  715. * else
  716. * console.debug('HIDE!');
  717. * });
  718. * }
  719. * });
  720. */
  721. 'onSetProgressState'
  722. ], function(e) {
  723. t[e] = new Dispatcher(t);
  724. });
  725. /**
  726. * Name/value collection with editor settings.
  727. *
  728. * @property settings
  729. * @type Object
  730. * @example
  731. * // Get the value of the theme setting
  732. * tinyMCE.activeEditor.windowManager.alert("You are using the " + tinyMCE.activeEditor.settings.theme + " theme");
  733. */
  734. t.settings = s = extend({
  735. id : id,
  736. language : 'en',
  737. docs_language : 'en',
  738. theme : 'simple',
  739. skin : 'default',
  740. delta_width : 0,
  741. delta_height : 0,
  742. popup_css : '',
  743. plugins : '',
  744. document_base_url : tinymce.documentBaseURL,
  745. add_form_submit_trigger : 1,
  746. submit_patch : 1,
  747. add_unload_trigger : 1,
  748. convert_urls : 1,
  749. relative_urls : 1,
  750. remove_script_host : 1,
  751. table_inline_editing : 0,
  752. object_resizing : 1,
  753. cleanup : 1,
  754. accessibility_focus : 1,
  755. custom_shortcuts : 1,
  756. custom_undo_redo_keyboard_shortcuts : 1,
  757. custom_undo_redo_restore_selection : 1,
  758. custom_undo_redo : 1,
  759. doctype : tinymce.isIE6 ? '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' : '<!DOCTYPE>', // Use old doctype on IE 6 to avoid horizontal scroll
  760. visual_table_class : 'mceItemTable',
  761. visual : 1,
  762. font_size_style_values : 'xx-small,x-small,small,medium,large,x-large,xx-large',
  763. font_size_legacy_values : 'xx-small,small,medium,large,x-large,xx-large,300%', // See: http://www.w3.org/TR/CSS2/fonts.html#propdef-font-size
  764. apply_source_formatting : 1,
  765. directionality : 'ltr',
  766. forced_root_block : 'p',
  767. hidden_input : 1,
  768. padd_empty_editor : 1,
  769. render_ui : 1,
  770. init_theme : 1,
  771. force_p_newlines : 1,
  772. indentation : '30px',
  773. keep_styles : 1,
  774. fix_table_elements : 1,
  775. inline_styles : 1,
  776. convert_fonts_to_spans : true,
  777. indent : 'simple',
  778. indent_before : 'p,h1,h2,h3,h4,h5,h6,blockquote,div,title,style,pre,script,td,ul,li,area,table,thead,tfoot,tbody,tr',
  779. indent_after : 'p,h1,h2,h3,h4,h5,h6,blockquote,div,title,style,pre,script,td,ul,li,area,table,thead,tfoot,tbody,tr',
  780. validate : true,
  781. entity_encoding : 'named',
  782. url_converter : t.convertURL,
  783. url_converter_scope : t,
  784. ie7_compat : true
  785. }, s);
  786. /**
  787. * URI object to document configured for the TinyMCE instance.
  788. *
  789. * @property documentBaseURI
  790. * @type tinymce.util.URI
  791. * @example
  792. * // Get relative URL from the location of document_base_url
  793. * tinyMCE.activeEditor.documentBaseURI.toRelative('/somedir/somefile.htm');
  794. *
  795. * // Get absolute URL from the location of document_base_url
  796. * tinyMCE.activeEditor.documentBaseURI.toAbsolute('somefile.htm');
  797. */
  798. t.documentBaseURI = new tinymce.util.URI(s.document_base_url || tinymce.documentBaseURL, {
  799. base_uri : tinyMCE.baseURI
  800. });
  801. /**
  802. * URI object to current document that holds the TinyMCE editor instance.
  803. *
  804. * @property baseURI
  805. * @type tinymce.util.URI
  806. * @example
  807. * // Get relative URL from the location of the API
  808. * tinyMCE.activeEditor.baseURI.toRelative('/somedir/somefile.htm');
  809. *
  810. * // Get absolute URL from the location of the API
  811. * tinyMCE.activeEditor.baseURI.toAbsolute('somefile.htm');
  812. */
  813. t.baseURI = tinymce.baseURI;
  814. /**
  815. * Array with CSS files to load into the iframe.
  816. *
  817. * @property contentCSS
  818. * @type Array
  819. */
  820. t.contentCSS = [];
  821. // Call setup
  822. t.execCallback('setup', t);
  823. },
  824. /**
  825. * Renderes the editor/adds it to the page.
  826. *
  827. * @method render
  828. */
  829. render : function(nst) {
  830. var t = this, s = t.settings, id = t.id, sl = tinymce.ScriptLoader;
  831. // Page is not loaded yet, wait for it
  832. if (!Event.domLoaded) {
  833. Event.add(document, 'init', function() {
  834. t.render();
  835. });
  836. return;
  837. }
  838. tinyMCE.settings = s;
  839. // Element not found, then skip initialization
  840. if (!t.getElement())
  841. return;
  842. // Is a iPad/iPhone and not on iOS5, then skip initialization. We need to sniff
  843. // here since the browser says it has contentEditable support but there is no visible
  844. // caret We will remove this check ones Apple implements full contentEditable support
  845. if (tinymce.isIDevice && !tinymce.isIOS5)
  846. return;
  847. // Add hidden input for non input elements inside form elements
  848. if (!/TEXTAREA|INPUT/i.test(t.getElement().nodeName) && s.hidden_input && DOM.getParent(id, 'form'))
  849. DOM.insertAfter(DOM.create('input', {type : 'hidden', name : id}), id);
  850. /**
  851. * Window manager reference, use this to open new windows and dialogs.
  852. *
  853. * @property windowManager
  854. * @type tinymce.WindowManager
  855. * @example
  856. * // Shows an alert message
  857. * tinyMCE.activeEditor.windowManager.alert('Hello world!');
  858. *
  859. * // Opens a new dialog with the file.htm file and the size 320x240
  860. * // It also adds a custom parameter this can be retrieved by using tinyMCEPopup.getWindowArg inside the dialog.
  861. * tinyMCE.activeEditor.windowManager.open({
  862. * url : 'file.htm',
  863. * width : 320,
  864. * height : 240
  865. * }, {
  866. * custom_param : 1
  867. * });
  868. */
  869. if (tinymce.WindowManager)
  870. t.windowManager = new tinymce.WindowManager(t);
  871. if (s.encoding == 'xml') {
  872. t.onGetContent.add(function(ed, o) {
  873. if (o.save)
  874. o.content = DOM.encode(o.content);
  875. });
  876. }
  877. if (s.add_form_submit_trigger) {
  878. t.onSubmit.addToTop(function() {
  879. if (t.initialized) {
  880. t.save();
  881. t.isNotDirty = 1;
  882. }
  883. });
  884. }
  885. if (s.add_unload_trigger) {
  886. t._beforeUnload = tinyMCE.onBeforeUnload.add(function() {
  887. if (t.initialized && !t.destroyed && !t.isHidden())
  888. t.save({format : 'raw', no_events : true});
  889. });
  890. }
  891. tinymce.addUnload(t.destroy, t);
  892. if (s.submit_patch) {
  893. t.onBeforeRenderUI.add(function() {
  894. var n = t.getElement().form;
  895. if (!n)
  896. return;
  897. // Already patched
  898. if (n._mceOldSubmit)
  899. return;
  900. // Check page uses id="submit" or name="submit" for it's submit button
  901. if (!n.submit.nodeType && !n.submit.length) {
  902. t.formElement = n;
  903. n._mceOldSubmit = n.submit;
  904. n.submit = function() {
  905. // Save all instances
  906. tinymce.triggerSave();
  907. t.isNotDirty = 1;
  908. return t.formElement._mceOldSubmit(t.formElement);
  909. };
  910. }
  911. n = null;
  912. });
  913. }
  914. // Load scripts
  915. function loadScripts() {
  916. if (s.language && s.language_load !== false)
  917. sl.add(tinymce.baseURL + '/langs/' + s.language + '.js');
  918. if (s.theme && s.theme.charAt(0) != '-' && !ThemeManager.urls[s.theme])
  919. ThemeManager.load(s.theme, 'themes/' + s.theme + '/editor_template' + tinymce.suffix + '.js');
  920. each(explode(s.plugins), function(p) {
  921. if (p &&!PluginManager.urls[p]) {
  922. if (p.charAt(0) == '-') {
  923. p = p.substr(1, p.length);
  924. var dependencies = PluginManager.dependencies(p);
  925. each(dependencies, function(dep) {
  926. var defaultSettings = {prefix:'plugins/', resource: dep, suffix:'/editor_plugin' + tinymce.suffix + '.js'};
  927. var dep = PluginManager.createUrl(defaultSettings, dep);
  928. PluginManager.load(dep.resource, dep);
  929. });
  930. } else {
  931. // Skip safari plugin, since it is removed as of 3.3b1
  932. if (p == 'safari') {
  933. return;
  934. }
  935. PluginManager.load(p, {prefix:'plugins/', resource: p, suffix:'/editor_plugin' + tinymce.suffix + '.js'});
  936. }
  937. }
  938. });
  939. // Init when que is loaded
  940. sl.loadQueue(function() {
  941. if (!t.removed)
  942. t.init();
  943. });
  944. };
  945. loadScripts();
  946. },
  947. /**
  948. * Initializes the editor this will be called automatically when
  949. * all plugins/themes and language packs are loaded by the rendered method.
  950. * This method will setup the iframe and create the theme and plugin instances.
  951. *
  952. * @method init
  953. */
  954. init : function() {
  955. var n, t = this, s = t.settings, w, h, e = t.getElement(), o, ti, u, bi, bc, re, i, initializedPlugins = [];
  956. tinymce.add(t);
  957. s.aria_label = s.aria_label || DOM.getAttrib(e, 'aria-label', t.getLang('aria.rich_text_area'));
  958. /**
  959. * Reference to the theme instance that was used to generate the UI.
  960. *
  961. * @property theme
  962. * @type tinymce.Theme
  963. * @example
  964. * // Executes a method on the theme directly
  965. * tinyMCE.activeEditor.theme.someMethod();
  966. */
  967. if (s.theme) {
  968. s.theme = s.theme.replace(/-/, '');
  969. o = ThemeManager.get(s.theme);
  970. t.theme = new o();
  971. if (t.theme.init && s.init_theme)
  972. t.theme.init(t, ThemeManager.urls[s.theme] || tinymce.documentBaseURL.replace(/\/$/, ''));
  973. }
  974. function initPlugin(p) {
  975. var c = PluginManager.get(p), u = PluginManager.urls[p] || tinymce.documentBaseURL.replace(/\/$/, ''), po;
  976. if (c && tinymce.inArray(initializedPlugins,p) === -1) {
  977. each(PluginManager.dependencies(p), function(dep){
  978. initPlugin(dep);
  979. });
  980. po = new c(t, u);
  981. t.plugins[p] = po;
  982. if (po.init) {
  983. po.init(t, u);
  984. initializedPlugins.push(p);
  985. }
  986. }
  987. }
  988. // Create all plugins
  989. each(explode(s.plugins.replace(/\-/g, '')), initPlugin);
  990. // Setup popup CSS path(s)
  991. if (s.popup_css !== false) {
  992. if (s.popup_css)
  993. s.popup_css = t.documentBaseURI.toAbsolute(s.popup_css);
  994. else
  995. s.popup_css = t.baseURI.toAbsolute("themes/" + s.theme + "/skins/" + s.skin + "/dialog.css");
  996. }
  997. if (s.popup_css_add)
  998. s.popup_css += ',' + t.documentBaseURI.toAbsolute(s.popup_css_add);
  999. /**
  1000. * Control manager instance for the editor. Will enables you to create new UI elements and change their states etc.
  1001. *
  1002. * @property controlManager
  1003. * @type tinymce.ControlManager
  1004. * @example
  1005. * // Disables the bold button
  1006. * tinyMCE.activeEditor.controlManager.setDisabled('bold', true);
  1007. */
  1008. t.controlManager = new tinymce.ControlManager(t);
  1009. if (s.custom_undo_redo) {
  1010. t.onBeforeExecCommand.add(function(ed, cmd, ui, val, a) {
  1011. if (cmd != 'Undo' && cmd != 'Redo' && cmd != 'mceRepaint' && (!a || !a.skip_undo))
  1012. t.undoManager.beforeChange();
  1013. });
  1014. t.onExecCommand.add(function(ed, cmd, ui, val, a) {
  1015. if (cmd != 'Undo' && cmd != 'Redo' && cmd != 'mceRepaint' && (!a || !a.skip_undo))
  1016. t.undoManager.add();
  1017. });
  1018. }
  1019. t.onExecCommand.add(function(ed, c) {
  1020. // Don't refresh the select lists until caret move
  1021. if (!/^(FontName|FontSize)$/.test(c))
  1022. t.nodeChanged();
  1023. });
  1024. // Remove ghost selections on images and tables in Gecko
  1025. if (isGecko) {
  1026. function repaint(a, o) {
  1027. if (!o || !o.initial)
  1028. t.execCommand('mceRepaint');
  1029. };
  1030. t.onUndo.add(repaint);
  1031. t.onRedo.add(repaint);
  1032. t.onSetContent.add(repaint);
  1033. }
  1034. // Enables users to override the control factory
  1035. t.onBeforeRenderUI.dispatch(t, t.controlManager);
  1036. // Measure box
  1037. if (s.render_ui) {
  1038. w = s.width || e.style.width || e.offsetWidth;
  1039. h = s.height || e.style.height || e.offsetHeight;
  1040. t.orgDisplay = e.style.display;
  1041. re = /^[0-9\.]+(|px)$/i;
  1042. if (re.test('' + w))
  1043. w = Math.max(parseInt(w) + (o.deltaWidth || 0), 100);
  1044. if (re.test('' + h))
  1045. h = Math.max(parseInt(h) + (o.deltaHeight || 0), 100);
  1046. // Render UI
  1047. o = t.theme.renderUI({
  1048. targetNode : e,
  1049. width : w,
  1050. height : h,
  1051. deltaWidth : s.delta_width,
  1052. deltaHeight : s.delta_height
  1053. });
  1054. t.editorContainer = o.editorContainer;
  1055. }
  1056. // #ifdef contentEditable
  1057. // Content editable mode ends here
  1058. if (s.content_editable) {
  1059. e = n = o = null; // Fix IE leak
  1060. return t.setupContentEditable();
  1061. }
  1062. // #endif
  1063. // User specified a document.domain value
  1064. if (document.domain && location.hostname != document.domain)
  1065. tinymce.relaxedDomain = document.domain;
  1066. // Resize editor
  1067. DOM.setStyles(o.sizeContainer || o.editorContainer, {
  1068. width : w,
  1069. height : h
  1070. });
  1071. // Load specified content CSS last
  1072. if (s.content_css) {
  1073. tinymce.each(explode(s.content_css), function(u) {
  1074. t.contentCSS.push(t.documentBaseURI.toAbsolute(u));
  1075. });
  1076. }
  1077. h = (o.iframeHeight || h) + (typeof(h) == 'number' ? (o.deltaHeight || 0) : '');
  1078. if (h < 100)
  1079. h = 100;
  1080. t.iframeHTML = s.doctype + '<html><head xmlns="http://www.w3.org/1999/xhtml">';
  1081. // We only need to override paths if we have to
  1082. // IE has a bug where it remove site absolute urls to relative ones if this is specified
  1083. if (s.document_base_url != tinymce.documentBaseURL)
  1084. t.iframeHTML += '<base href="' + t.documentBaseURI.getURI() + '" />';
  1085. // IE8 doesn't support carets behind images setting ie7_compat would force IE8+ to run in IE7 compat mode.
  1086. if (s.ie7_compat)
  1087. t.iframeHTML += '<meta http-equiv="X-UA-Compatible" content="IE=7" />';
  1088. else
  1089. t.iframeHTML += '<meta http-equiv="X-UA-Compatible" content="IE=edge" />';
  1090. t.iframeHTML += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
  1091. // Load the CSS by injecting them into the HTML this will reduce "flicker"
  1092. for (i = 0; i < t.contentCSS.length; i++) {
  1093. t.iframeHTML += '<link type="text/css" rel="stylesheet" href="' + t.contentCSS[i] + '" />';
  1094. }
  1095. bi = s.body_id || 'tinymce';
  1096. if (bi.indexOf('=') != -1) {
  1097. bi = t.getParam('body_id', '', 'hash');
  1098. bi = bi[t.id] || bi;
  1099. }
  1100. bc = s.body_class || '';
  1101. if (bc.indexOf('=') != -1) {
  1102. bc = t.getParam('body_class', '', 'hash');
  1103. bc = bc[t.id] || '';
  1104. }
  1105. t.iframeHTML += '</head><body id="' + bi + '" class="mceContentBody ' + bc + '"><br></body></html>';
  1106. // Domain relaxing enabled, then set document domain
  1107. if (tinymce.relaxedDomain && (isIE || (tinymce.isOpera && parseFloat(opera.version()) < 11))) {
  1108. // We need to write the contents here in IE since multiple writes messes up refresh button and back button
  1109. u = 'javascript:(function(){document.open();document.domain="' + document.domain + '";var ed = window.parent.tinyMCE.get("' + t.id + '");document.write(ed.iframeHTML);document.close();ed.setupIframe();})()';
  1110. }
  1111. // Create iframe
  1112. // TODO: ACC add the appropriate description on this.
  1113. n = DOM.add(o.iframeContainer, 'iframe', {
  1114. id : t.id + "_ifr",
  1115. src : u || 'javascript:""', // Workaround for HTTPS warning in IE6/7
  1116. frameBorder : '0',
  1117. allowTransparency : "true",
  1118. title : s.aria_label,
  1119. style : {
  1120. width : '100%',
  1121. height : h,
  1122. display : 'block' // Important for Gecko to render the iframe correctly
  1123. }
  1124. });
  1125. t.contentAreaContainer = o.iframeContainer;
  1126. DOM.get(o.editorContainer).style.display = t.orgDisplay;
  1127. DOM.get(t.id).style.display = 'none';
  1128. DOM.setAttrib(t.id, 'aria-hidden', true);
  1129. if (!tinymce.relaxedDomain || !u)
  1130. t.setupIframe();
  1131. e = n = o = null; // Cleanup
  1132. },
  1133. /**
  1134. * This method get called by the init method ones the iframe is loaded.
  1135. * It will fill the iframe with contents, setups DOM and selection objects for the iframe.
  1136. * This method should not be called directly.
  1137. *
  1138. * @method setupIframe
  1139. */
  1140. setupIframe : function() {
  1141. var t = this, s = t.settings, e = DOM.get(t.id), d = t.getDoc(), h, b;
  1142. // Setup iframe body
  1143. if (!isIE || !tinymce.relaxedDomain) {
  1144. d.open();
  1145. d.write(t.iframeHTML);
  1146. d.close();
  1147. if (tinymce.relaxedDomain)
  1148. d.domain = tinymce.relaxedDomain;
  1149. }
  1150. // It will not steal focus while setting contentEditable
  1151. b = t.getBody();
  1152. b.disabled = true;
  1153. if (!s.readonly)
  1154. b.contentEditable = true;
  1155. b.disabled = false;
  1156. /**
  1157. * Schema instance, enables you to validate elements and it's children.
  1158. *
  1159. * @property schema
  1160. * @type tinymce.html.Schema
  1161. */
  1162. t.schema = new tinymce.html.Schema(s);
  1163. /**
  1164. * DOM instance for the editor.
  1165. *
  1166. * @property dom
  1167. * @type tinymce.dom.DOMUtils
  1168. * @example
  1169. * // Adds a class to all paragraphs within the editor
  1170. * tinyMCE.activeEditor.dom.addClass(tinyMCE.activeEditor.dom.select('p'), 'someclass');
  1171. */
  1172. t.dom = new tinymce.dom.DOMUtils(t.getDoc(), {
  1173. keep_values : true,
  1174. url_converter : t.convertURL,
  1175. url_converter_scope : t,
  1176. hex_colors : s.force_hex_style_colors,
  1177. class_filter : s.class_filter,
  1178. update_styles : 1,
  1179. fix_ie_paragraphs : 1,
  1180. schema : t.schema
  1181. });
  1182. /**
  1183. * HTML parser will be used when contents is inserted into the editor.
  1184. *
  1185. * @property parser
  1186. * @type tinymce.html.DomParser
  1187. */
  1188. t.parser = new tinymce.html.DomParser(s, t.schema);
  1189. // Force anchor names closed, unless the setting "allow_html_in_named_anchor" is explicitly included.
  1190. if (!t.settings.allow_html_in_named_anchor) {
  1191. t.parser.addAttributeFilter('name', function(nodes, name) {
  1192. var i = nodes.length, sibling, prevSibling, parent, node;
  1193. while (i--) {
  1194. node = nodes[i];
  1195. if (node.name === 'a' && node.firstChild) {
  1196. parent = node.parent;
  1197. // Move children after current node
  1198. sibling = node.lastChild;
  1199. do {
  1200. prevSibling = sibling.prev;
  1201. parent.insert(sibling, node);
  1202. sibling = prevSibling;
  1203. } while (sibling);
  1204. }
  1205. }
  1206. });
  1207. }
  1208. // Convert src and href into data-mce-src, data-mce-href and data-mce-style
  1209. t.parser.addAttributeFilter('src,href,style', function(nodes, name) {
  1210. var i = nodes.length, node, dom = t.dom, value, internalName;
  1211. while (i--) {
  1212. node = nodes[i];
  1213. value = node.attr(name);
  1214. internalName = 'data-mce-' + name;
  1215. // Add internal attribute if we need to we don't on a refresh of the document
  1216. if (!node.attributes.map[internalName]) {
  1217. if (name === "style")
  1218. node.attr(internalName, dom.serializeStyle(dom.parseStyle(value), node.name));
  1219. else
  1220. node.attr(internalName, t.convertURL(value, name, node.name));
  1221. }
  1222. }
  1223. });
  1224. // Keep scripts from executing
  1225. t.parser.addNodeFilter('script', function(nodes, name) {
  1226. var i = nodes.length, node;
  1227. while (i--) {
  1228. node = nodes[i];
  1229. node.attr('type', 'mce-' + (node.attr('type') || 'text/javascript'));
  1230. }
  1231. });
  1232. t.parser.addNodeFilter('#cdata', function(nodes, name) {
  1233. var i = nodes.length, node;
  1234. while (i--) {
  1235. node = nodes[i];
  1236. node.type = 8;
  1237. node.name = '#comment';
  1238. node.value = '[CDATA[' + node.value + ']]';
  1239. }
  1240. });
  1241. t.parser.addNodeFilter('p,h1,h2,h3,h4,h5,h6,div', function(nodes, name) {
  1242. var i = nodes.length, node, nonEmptyElements = t.schema.getNonEmptyElements();
  1243. while (i--) {
  1244. node = nodes[i];
  1245. if (node.isEmpty(nonEmptyElements))
  1246. node.empty().append(new tinymce.html.Node('br', 1)).shortEnded = true;
  1247. }
  1248. });
  1249. /**
  1250. * DOM serializer for the editor. Will be used when contents is extracted from the editor.
  1251. *
  1252. * @property serializer
  1253. * @type tinymce.dom.Serializer
  1254. * @example
  1255. * // Serializes the first paragraph in the editor into a string
  1256. * tinyMCE.activeEditor.serializer.serialize(tinyMCE.activeEditor.dom.select('p')[0]);
  1257. */
  1258. t.serializer = new tinymce.dom.Serializer(s, t.dom, t.schema);
  1259. /**
  1260. * Selection instance for the editor.
  1261. *
  1262. * @property selection
  1263. * @type tinymce.dom.Selection
  1264. * @example
  1265. * // Sets some contents to the current selection in the editor
  1266. * tinyMCE.activeEditor.selection.setContent('Some contents');
  1267. *
  1268. * // Gets the current selection
  1269. * alert(tinyMCE.activeEditor.selection.getContent());
  1270. *
  1271. * // Selects the first paragraph found
  1272. * tinyMCE.activeEditor.selection.select(tinyMCE.activeEditor.dom.select('p')[0]);
  1273. */
  1274. t.selection = new tinymce.dom.Selection(t.dom, t.getWin(), t.serializer);
  1275. /**
  1276. * Formatter instance.
  1277. *
  1278. * @property formatter
  1279. * @type tinymce.Formatter
  1280. */
  1281. t.formatter = new tinymce.Formatter(this);
  1282. // Register default formats
  1283. t.formatter.register({
  1284. alignleft : [
  1285. {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'left'}},
  1286. {selector : 'img,table', collapsed : false, styles : {'float' : 'left'}}
  1287. ],
  1288. aligncenter : [
  1289. {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'center'}},
  1290. {selector : 'img', collapsed : false, styles : {display : 'block', marginLeft : 'auto', marginRight : 'auto'}},
  1291. {selector : 'table', collapsed : false, styles : {marginLeft : 'auto', marginRight : 'auto'}}
  1292. ],
  1293. alignright : [
  1294. {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'right'}},
  1295. {selector : 'img,table', collapsed : false, styles : {'float' : 'right'}}
  1296. ],
  1297. alignfull : [
  1298. {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'justify'}}
  1299. ],
  1300. bold : [
  1301. {inline : 'strong', remove : 'all'},
  1302. {inline : 'span', styles : {fontWeight : 'bold'}},
  1303. {inline : 'b', remove : 'all'}
  1304. ],
  1305. italic : [
  1306. {inline : 'em', remove : 'all'},
  1307. {inline : 'span', styles : {fontStyle : 'italic'}},
  1308. {inline : 'i', remove : 'all'}
  1309. ],
  1310. underline : [
  1311. {inline : 'span', styles : {textDecoration : 'underline'}, exact : true},
  1312. {inline : 'u', remove : 'all'}
  1313. ],
  1314. strikethrough : [
  1315. {inline : 'span', styles : {textDecoration : 'line-through'}, exact : true},
  1316. {inline : 'strike', remove : 'all'}
  1317. ],
  1318. forecolor : {inline : 'span', styles : {color : '%value'}, wrap_links : false},
  1319. hilitecolor : {inline : 'span', styles : {backgroundColor : '%value'}, wrap_links : false},
  1320. fontname : {inline : 'span', styles : {fontFamily : '%value'}},
  1321. fontsize : {inline : 'span', styles : {fontSize : '%value'}},
  1322. fontsize_class : {inline : 'span', attributes : {'class' : '%value'}},
  1323. blockquote : {block : 'blockquote', wrapper : 1, remove : 'all'},
  1324. subscript : {inline : 'sub'},
  1325. superscript : {inline : 'sup'},
  1326. link : {inline : 'a', selector : 'a', remove : 'all', split : true, deep : true,
  1327. onmatch : function(node) {
  1328. return true;
  1329. },
  1330. onformat : function(elm, fmt, vars) {
  1331. each(vars, function(value, key) {
  1332. t.dom.setAttrib(elm, key, value);
  1333. });
  1334. }
  1335. },
  1336. removeformat : [
  1337. {selector : 'b,strong,em,i,font,u,strike', remove : 'all', split : true, expand : false, block_expand : true, deep : true},
  1338. {selector : 'span', attributes : ['style', 'class'], remove : 'empty', split : true, expand : false, deep : true},
  1339. {selector : '*', attributes : ['style', 'class'], split : false, expand : false, deep : true}
  1340. ]
  1341. });
  1342. // Register default block formats
  1343. each('p h1 h2 h3 h4 h5 h6 div address pre div code dt dd samp'.split(/\s/), function(name) {
  1344. t.formatter.register(name, {block : name, remove : 'all'});
  1345. });
  1346. // Register user defined formats
  1347. t.formatter.register(t.settings.formats);
  1348. /**
  1349. * Undo manager instance, responsible for handling undo levels.
  1350. *
  1351. * @property undoManager
  1352. * @type tinymce.UndoManager
  1353. * @example
  1354. * // Undoes the last modification to the editor
  1355. * tinyMCE.activeEditor.undoManager.undo();
  1356. */
  1357. t.undoManager = new tinymce.UndoManager(t);
  1358. // Pass through
  1359. t.undoManager.onAdd.add(function(um, l) {
  1360. if (um.hasUndo())
  1361. return t.onChange.dispatch(t, l, um);
  1362. });
  1363. t.undoManager.onUndo.add(function(um, l) {
  1364. return t.onUndo.dispatch(t, l, um);
  1365. });
  1366. t.undoManager.onRedo.add(function(um, l) {
  1367. return t.onRedo.dispatch(t, l, um);
  1368. });
  1369. t.forceBlocks = new tinymce.ForceBlocks(t, {
  1370. forced_root_block : s.forced_root_block
  1371. });
  1372. t.editorCommands = new tinymce.EditorCommands(t);
  1373. // Pass through
  1374. t.serializer.onPreProcess.add(function(se, o) {
  1375. return t.onPreProcess.dispatch(t, o, se);
  1376. });
  1377. t.serializer.onPostProcess.add(function(se, o) {
  1378. return t.onPostProcess.dispatch(t, o, se);
  1379. });
  1380. t.onPreInit.dispatch(t);
  1381. if (!s.gecko_spellcheck)
  1382. t.getBody().spellcheck = 0;
  1383. if (!s.readonly)
  1384. t._addEvents();
  1385. t.controlManager.onPostRender.dispatch(t, t.controlManager);
  1386. t.onPostRender.dispatch(t);
  1387. t.quirks = new tinymce.util.Quirks(this);
  1388. if (s.directionality)
  1389. t.getBody().dir = s.directionality;
  1390. if (s.nowrap)
  1391. t.getBody().style.whiteSpace = "nowrap";
  1392. if (s.handle_node_change_callback) {
  1393. t.onNodeChange.add(function(ed, cm, n) {
  1394. t.execCallback('handle_node_change_callback', t.id, n, -1, -1, true, t.selection.isCollapsed());
  1395. });
  1396. }
  1397. if (s.save_callback) {
  1398. t.onSaveContent.add(function(ed, o) {
  1399. var h = t.execCallback('save_callback', t.id, o.content, t.getBody());
  1400. if (h)
  1401. o.content = h;
  1402. });
  1403. }
  1404. if (s.onchange_callback) {
  1405. t.onChange.add(function(ed, l) {
  1406. t.execCallback('onchange_callback', t, l);
  1407. });
  1408. }
  1409. if (s.protect) {
  1410. t.onBeforeSetContent.add(function(ed, o) {
  1411. if (s.protect) {
  1412. each(s.protect, function(pattern) {
  1413. o.content = o.content.replace(pattern, function(str) {
  1414. return '<!--mce:protected ' + escape(str) + '-->';
  1415. });
  1416. });
  1417. }
  1418. });
  1419. }
  1420. if (s.convert_newlines_to_brs) {
  1421. t.onBeforeSetContent.add(function(ed, o) {
  1422. if (o.initial)
  1423. o.content = o.content.replace(/\r?\n/g, '<br />');
  1424. });
  1425. }
  1426. if (s.preformatted) {
  1427. t.onPostProcess.add(function(ed, o) {
  1428. o.content = o.content.replace(/^\s*<pre.*?>/, '');
  1429. o.content = o.content.replace(/<\/pre>\s*$/, '');
  1430. if (o.set)
  1431. o.content = '<pre class="mceItemHidden">' + o.content + '</pre>';
  1432. });
  1433. }
  1434. if (s.verify_css_classes) {
  1435. t.serializer.attribValueFilter = function(n, v) {
  1436. var s, cl;
  1437. if (n == 'class') {
  1438. // Build regexp for classes
  1439. if (!t.classesRE) {
  1440. cl = t.dom.getClasses();
  1441. if (cl.length > 0) {
  1442. s = '';
  1443. each (cl, function(o) {
  1444. s += (s ? '|' : '') + o['class'];
  1445. });
  1446. t.classesRE = new RegExp('(' + s + ')', 'gi');
  1447. }
  1448. }
  1449. return !t.classesRE || /(\bmceItem\w+\b|\bmceTemp\w+\b)/g.test(v) || t.classesRE.test(v) ? v : '';
  1450. }
  1451. return v;
  1452. };
  1453. }
  1454. if (s.cleanup_callback) {
  1455. t.onBeforeSetContent.add(function(ed, o) {
  1456. o.content = t.execCallback('cleanup_callback', 'insert_to_editor', o.content, o);
  1457. });
  1458. t.onPreProcess.add(function(ed, o) {
  1459. if (o.set)
  1460. t.execCallback('cleanup_callback', 'insert_to_editor_dom', o.node, o);
  1461. if (o.get)
  1462. t.execCallback('cleanup_callback', 'get_from_editor_dom', o.node, o);
  1463. });
  1464. t.onPostProcess.add(function(ed, o) {
  1465. if (o.set)
  1466. o.content = t.execCallback('cleanup_callback', 'insert_to_editor', o.content, o);
  1467. if (o.get)
  1468. o.content = t.execCallback('cleanup_callback', 'get_from_editor', o.content, o);
  1469. });
  1470. }
  1471. if (s.save_callback) {
  1472. t.onGetContent.add(function(ed, o) {
  1473. if (o.save)
  1474. o.content = t.execCallback('save_callback', t.id, o.content, t.getBody());
  1475. });
  1476. }
  1477. if (s.handle_event_callback) {
  1478. t.onEvent.add(function(ed, e, o) {
  1479. if (t.execCallback('handle_event_callback', e, ed, o) === false)
  1480. Event.cancel(e);
  1481. });
  1482. }
  1483. // Add visual aids when new contents is added
  1484. t.onSetContent.add(function() {
  1485. t.addVisual(t.getBody());
  1486. });
  1487. // Remove empty contents
  1488. if (s.padd_empty_editor) {
  1489. t.onPostProcess.add(function(ed, o) {
  1490. o.content = o.content.replace(/^(<p[^>]*>(&nbsp;|&#160;|\s|\u00a0|)<\/p>[\r\n]*|<br \/>[\r\n]*)$/, '');
  1491. });
  1492. }
  1493. if (isGecko) {
  1494. // Fix gecko link bug, when a link is placed at the end of block elements there is
  1495. // no way to move the caret behind the link. This fix adds a bogus br element after the link
  1496. function fixLinks(ed, o) {
  1497. each(ed.dom.select('a'), function(n) {
  1498. var pn = n.parentNode;
  1499. if (ed.dom.isBlock(pn) && pn.lastChild === n)
  1500. ed.dom.add(pn, 'br', {'data-mce-bogus' : 1});
  1501. });
  1502. };
  1503. t.onExecCommand.add(function(ed, cmd) {
  1504. if (cmd === 'CreateLink')
  1505. fixLinks(ed);
  1506. });
  1507. t.onSetContent.add(t.selection.onSetContent.add(fixLinks));
  1508. }
  1509. t.load({initial : true, format : 'html'});
  1510. t.startContent = t.getContent({format : 'raw'});
  1511. t.undoManager.add();
  1512. t.initialized = true;
  1513. t.onInit.dispatch(t);
  1514. t.execCallback('setupcontent_callback', t.id, t.getBody(), t.getDoc());
  1515. t.execCallback('init_instance_callback', t);
  1516. t.focus(true);
  1517. t.nodeChanged({initial : 1});
  1518. // Load specified content CSS last
  1519. each(t.contentCSS, function(u) {
  1520. t.dom.loadCSS(u);
  1521. });
  1522. // Handle auto focus
  1523. if (s.auto_focus) {
  1524. setTimeout(function () {
  1525. var ed = tinymce.get(s.auto_focus);
  1526. ed.selection.select(ed.getBody(), 1);
  1527. ed.selection.collapse(1);
  1528. ed.getBody().focus();
  1529. ed.getWin().focus();
  1530. }, 100);
  1531. }
  1532. e = null;
  1533. },
  1534. // #ifdef contentEditable
  1535. /**
  1536. * Sets up the contentEditable mode.
  1537. *
  1538. * @method setupContentEditable
  1539. */
  1540. setupContentEditable : function() {
  1541. var t = this, s = t.settings, e = t.getElement();
  1542. t.contentDocument = s.content_document || document;
  1543. t.contentWindow = s.content_window || window;
  1544. t.bodyElement = e;
  1545. // Prevent leak in IE
  1546. s.content_document = s.content_window = null;
  1547. DOM.hide(e);
  1548. e.contentEditable = t.getParam('content_editable_state', true);
  1549. DOM.show(e);
  1550. if (!s.gecko_spellcheck)
  1551. t.getDoc().body.spellcheck = 0;
  1552. // Setup objects
  1553. t.dom = new tinymce.dom.DOMUtils(t.getDoc(), {
  1554. keep_values : true,
  1555. url_converter : t.convertURL,
  1556. url_converter_scope : t,
  1557. hex_colors : s.force_hex_style_colors,
  1558. class_filter : s.class_filter,
  1559. root_element : t.id,
  1560. fix_ie_paragraphs : 1,
  1561. update_styles : 1
  1562. });
  1563. t.serializer = new tinymce.dom.Serializer(s, t.dom, schema);
  1564. t.selection = new tinymce.dom.Selection(t.dom, t.getWin(), t.serializer);
  1565. t.forceBlocks = new tinymce.ForceBlocks(t, {
  1566. forced_root_block : s.forced_root_block
  1567. });
  1568. t.editorCommands = new tinymce.EditorCommands(t);
  1569. // Pass through
  1570. t.serializer.onPreProcess.add(function(se, o) {
  1571. return t.onPreProcess.dispatch(t, o, se);
  1572. });
  1573. t.serializer.onPostProcess.add(function(se, o) {
  1574. return t.onPostProcess.dispatch(t, o, se);
  1575. });
  1576. t.onPreInit.dispatch(t);
  1577. t._addEvents();
  1578. t.controlManager.onPostRender.dispatch(t, t.controlManager);
  1579. t.onPostRender.dispatch(t);
  1580. t.onSetContent.add(function() {
  1581. t.addVisual(t.getBody());
  1582. });
  1583. //t.load({initial : true, format : (s.cleanup_on_startup ? 'html' : 'raw')});
  1584. t.startContent = t.getContent({format : 'raw'});
  1585. t.undoManager.add({initial : true});
  1586. t.initialized = true;
  1587. t.onInit.dispatch(t);
  1588. t.focus(true);
  1589. t.nodeChanged({initial : 1});
  1590. // Load specified content CSS last
  1591. if (s.content_css) {
  1592. each(explode(s.content_css), function(u) {
  1593. t.dom.loadCSS(t.documentBaseURI.toAbsolute(u));
  1594. });
  1595. }
  1596. if (isIE) {
  1597. // Store away selection
  1598. t.dom.bind(t.getElement(), 'beforedeactivate', function() {
  1599. t.lastSelectionBookmark = t.selection.getBookmark(1);
  1600. });
  1601. t.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
  1602. if (!DOM.getParent(ed.selection.getStart(), function(n) {return n == ed.getBody();}))
  1603. o.terminate = 1;
  1604. if (!DOM.getParent(ed.selection.getEnd(), function(n) {return n == ed.getBody();}))
  1605. o.terminate = 1;
  1606. });
  1607. }
  1608. e = null; // Cleanup
  1609. },
  1610. // #endif
  1611. /**
  1612. * Focuses/activates the editor. This will set this editor as the activeEditor in the tinymce collection
  1613. * it will also place DOM focus inside the editor.
  1614. *
  1615. * @method focus
  1616. * @param {Boolean} sf Skip DOM focus. Just set is as the active editor.
  1617. */
  1618. focus : function(sf) {
  1619. var oed, t = this, selection = t.selection, ce = t.settings.content_editable, ieRng, controlElm, doc = t.getDoc();
  1620. if (!sf) {
  1621. // Get selected control element
  1622. ieRng = selection.getRng();
  1623. if (ieRng.item) {
  1624. controlElm = ieRng.item(0);
  1625. }
  1626. t._refreshContentEditable();
  1627. selection.normalize();
  1628. // Is not content editable
  1629. if (!ce)
  1630. t.getWin().focus();
  1631. // Focus the body as well since it's contentEditable
  1632. if (tinymce.isGecko) {
  1633. t.getBody().focus();
  1634. }
  1635. // Restore selected control element
  1636. // This is needed when for example an image is selected within a
  1637. // layer a call to focus will then remove the control selection
  1638. if (controlElm && controlElm.ownerDocument == doc) {
  1639. ieRng = doc.body.createControlRange();
  1640. ieRng.addElement(controlElm);
  1641. ieRng.select();
  1642. }
  1643. // #ifdef contentEditable
  1644. // Content editable mode ends here
  1645. if (ce) {
  1646. if (tinymce.isWebKit)
  1647. t.getWin().focus();
  1648. else {
  1649. if (tinymce.isIE)
  1650. t.getElement().setActive();
  1651. else
  1652. t.getElement().focus();
  1653. }
  1654. }
  1655. // #endif
  1656. }
  1657. if (tinymce.activeEditor != t) {
  1658. if ((oed = tinymce.activeEditor) != null)
  1659. oed.onDeactivate.dispatch(oed, t);
  1660. t.onActivate.dispatch(t, oed);
  1661. }
  1662. tinymce._setActive(t);
  1663. },
  1664. /**
  1665. * Executes a legacy callback. This method is useful to call old 2.x option callbacks.
  1666. * There new event model is a better way to add callback so this method might be removed in the future.
  1667. *
  1668. * @method execCallback
  1669. * @param {String} n Name of the callback to execute.
  1670. * @return {Object} Return value passed from callback function.
  1671. */
  1672. execCallback : function(n) {
  1673. var t = this, f = t.settings[n], s;
  1674. if (!f)
  1675. return;
  1676. // Look through lookup
  1677. if (t.callbackLookup && (s = t.callbackLookup[n])) {
  1678. f = s.func;
  1679. s = s.scope;
  1680. }
  1681. if (is(f, 'string')) {
  1682. s = f.replace(/\.\w+$/, '');
  1683. s = s ? tinymce.resolve(s) : 0;
  1684. f = tinymce.resolve(f);
  1685. t.callbackLookup = t.callbackLookup || {};
  1686. t.callbackLookup[n] = {func : f, scope : s};
  1687. }
  1688. return f.apply(s || t, Array.prototype.slice.call(arguments, 1));
  1689. },
  1690. /**
  1691. * Translates the specified string by replacing variables with language pack items it will also check if there is
  1692. * a key mathcin the input.
  1693. *
  1694. * @method translate
  1695. * @param {String} s String to translate by the language pack data.
  1696. * @return {String} Translated string.
  1697. */
  1698. translate : function(s) {
  1699. var c = this.settings.language || 'en', i18n = tinymce.i18n;
  1700. if (!s)
  1701. return '';
  1702. return i18n[c + '.' + s] || s.replace(/{\#([^}]+)\}/g, function(a, b) {
  1703. return i18n[c + '.' + b] || '{#' + b + '}';
  1704. });
  1705. },
  1706. /**
  1707. * Returns a language pack item by name/key.
  1708. *
  1709. * @method getLang
  1710. * @param {String} n Name/key to get from the language pack.
  1711. * @param {String} dv Optional default value to retrive.
  1712. */
  1713. getLang : function(n, dv) {
  1714. return tinymce.i18n[(this.settings.language || 'en') + '.' + n] || (is(dv) ? dv : '{#' + n + '}');
  1715. },
  1716. /**
  1717. * Returns a configuration parameter by name.
  1718. *
  1719. * @method getParam
  1720. * @param {String} n Configruation parameter to retrive.
  1721. * @param {String} dv Optional default value to return.
  1722. * @param {String} ty Optional type parameter.
  1723. * @return {String} Configuration parameter value or default value.
  1724. * @example
  1725. * // Returns a specific config value from the currently active editor
  1726. * var someval = tinyMCE.activeEditor.getParam('myvalue');
  1727. *
  1728. * // Returns a specific config value from a specific editor instance by id
  1729. * var someval2 = tinyMCE.get('my_editor').getParam('myvalue');
  1730. */
  1731. getParam : function(n, dv, ty) {
  1732. var tr = tinymce.trim, v = is(this.settings[n]) ? this.settings[n] : dv, o;
  1733. if (ty === 'hash') {
  1734. o = {};
  1735. if (is(v, 'string')) {
  1736. each(v.indexOf('=') > 0 ? v.split(/[;,](?![^=;,]*(?:[;,]|$))/) : v.split(','), function(v) {
  1737. v = v.split('=');
  1738. if (v.length > 1)
  1739. o[tr(v[0])] = tr(v[1]);
  1740. else
  1741. o[tr(v[0])] = tr(v);
  1742. });
  1743. } else
  1744. o = v;
  1745. return o;
  1746. }
  1747. return v;
  1748. },
  1749. /**
  1750. * Distpaches out a onNodeChange event to all observers. This method should be called when you
  1751. * need to update the UI states or element path etc.
  1752. *
  1753. * @method nodeChanged
  1754. * @param {Object} o Optional object to pass along for the node changed event.
  1755. */
  1756. nodeChanged : function(o) {
  1757. var t = this, s = t.selection, n = s.getStart() || t.getBody();
  1758. // Fix for bug #1896577 it seems that this can not be fired while the editor is loading
  1759. if (t.initialized) {
  1760. o = o || {};
  1761. n = isIE && n.ownerDocument != t.getDoc() ? t.getBody() : n; // Fix for IE initial state
  1762. // Get parents and add them to object
  1763. o.parents = [];
  1764. t.dom.getParent(n, function(node) {
  1765. if (node.nodeName == 'BODY')
  1766. return true;
  1767. o.parents.push(node);
  1768. });
  1769. t.onNodeChange.dispatch(
  1770. t,
  1771. o ? o.controlManager || t.controlManager : t.controlManager,
  1772. n,
  1773. s.isCollapsed(),
  1774. o
  1775. );
  1776. }
  1777. },
  1778. /**
  1779. * Adds a button that later gets created by the ControlManager. This is a shorter and easier method
  1780. * of adding buttons without the need to deal with the ControlManager directly. But it's also less
  1781. * powerfull if you need more control use the ControlManagers factory methods instead.
  1782. *
  1783. * @method addButton
  1784. * @param {String} n Button name to add.
  1785. * @param {Object} s Settings object with title, cmd etc.
  1786. * @example
  1787. * // Adds a custom button to the editor and when a user clicks the button it will open
  1788. * // an alert box with the selected contents as plain text.
  1789. * tinyMCE.init({
  1790. * ...
  1791. *
  1792. * theme_advanced_buttons1 : 'example,..'
  1793. *
  1794. * setup : function(ed) {
  1795. * // Register example button
  1796. * ed.addButton('example', {
  1797. * title : 'example.desc',
  1798. * image : '../jscripts/tiny_mce/plugins/example/img/example.gif',
  1799. * onclick : function() {
  1800. * ed.windowManager.alert('Hello world!! Selection: ' + ed.selection.getContent({format : 'text'}));
  1801. * }
  1802. * });
  1803. * }
  1804. * });
  1805. */
  1806. addButton : function(n, s) {
  1807. var t = this;
  1808. t.buttons = t.buttons || {};
  1809. t.buttons[n] = s;
  1810. },
  1811. /**
  1812. * Adds a custom command to the editor, you can also override existing commands with this method.
  1813. * The command that you add can be executed with execCommand.
  1814. *
  1815. * @method addCommand
  1816. * @param {String} name Command name to add/override.
  1817. * @param {addCommandCallback} callback Function to execute when the command occurs.
  1818. * @param {Object} scope Optional scope to execute the function in.
  1819. * @example
  1820. * // Adds a custom command that later can be executed using execCommand
  1821. * tinyMCE.init({
  1822. * ...
  1823. *
  1824. * setup : function(ed) {
  1825. * // Register example command
  1826. * ed.addCommand('mycommand', function(ui, v) {
  1827. * ed.windowManager.alert('Hello world!! Selection: ' + ed.selection.getContent({format : 'text'}));
  1828. * });
  1829. * }
  1830. * });
  1831. */
  1832. addCommand : function(name, callback, scope) {
  1833. /**
  1834. * Callback function that gets called when a command is executed.
  1835. *
  1836. * @callback addCommandCallback
  1837. * @param {Boolean} ui Display UI state true/false.
  1838. * @param {Object} value Optional value for command.
  1839. * @return {Boolean} True/false state if the command was handled or not.
  1840. */
  1841. this.execCommands[name] = {func : callback, scope : scope || this};
  1842. },
  1843. /**
  1844. * Adds a custom query state command to the editor, you can also override existing commands with this method.
  1845. * The command that you add can be executed with queryCommandState function.
  1846. *
  1847. * @method addQueryStateHandler
  1848. * @param {String} name Command name to add/override.
  1849. * @param {addQueryStateHandlerCallback} callback Function to execute when the command state retrival occurs.
  1850. * @param {Object} scope Optional scope to execute the function in.
  1851. */
  1852. addQueryStateHandler : function(name, callback, scope) {
  1853. /**
  1854. * Callback function that gets called when a queryCommandState is executed.
  1855. *
  1856. * @callback addQueryStateHandlerCallback
  1857. * @return {Boolean} True/false state if the command is enabled or not like is it bold.
  1858. */
  1859. this.queryStateCommands[name] = {func : callback, scope : scope || this};
  1860. },
  1861. /**
  1862. * Adds a custom query value command to the editor, you can also override existing commands with this method.
  1863. * The command that you add can be executed with queryCommandValue function.
  1864. *
  1865. * @method addQueryValueHandler
  1866. * @param {String} name Command name to add/override.
  1867. * @param {addQueryValueHandlerCallback} callback Function to execute when the command value retrival occurs.
  1868. * @param {Object} scope Optional scope to execute the function in.
  1869. */
  1870. addQueryValueHandler : function(name, callback, scope) {
  1871. /**
  1872. * Callback function that gets called when a queryCommandValue is executed.
  1873. *
  1874. * @callback addQueryValueHandlerCallback
  1875. * @return {Object} Value of the command or undefined.
  1876. */
  1877. this.queryValueCommands[name] = {func : callback, scope : scope || this};
  1878. },
  1879. /**
  1880. * Adds a keyboard shortcut for some command or function.
  1881. *
  1882. * @method addShortcut
  1883. * @param {String} pa Shortcut pattern. Like for example: ctrl+alt+o.
  1884. * @param {String} desc Text description for the command.
  1885. * @param {String/Function} cmd_func Command name string or function to execute when the key is pressed.
  1886. * @param {Object} sc Optional scope to execute the function in.
  1887. * @return {Boolean} true/false state if the shortcut was added or not.
  1888. */
  1889. addShortcut : function(pa, desc, cmd_func, sc) {
  1890. var t = this, c;
  1891. if (!t.settings.custom_shortcuts)
  1892. return false;
  1893. t.shortcuts = t.shortcuts || {};
  1894. if (is(cmd_func, 'string')) {
  1895. c = cmd_func;
  1896. cmd_func = function() {
  1897. t.execCommand(c, false, null);
  1898. };
  1899. }
  1900. if (is(cmd_func, 'object')) {
  1901. c = cmd_func;
  1902. cmd_func = function() {
  1903. t.execCommand(c[0], c[1], c[2]);
  1904. };
  1905. }
  1906. each(explode(pa), function(pa) {
  1907. var o = {
  1908. func : cmd_func,
  1909. scope : sc || this,
  1910. desc : desc,
  1911. alt : false,
  1912. ctrl : false,
  1913. shift : false
  1914. };
  1915. each(explode(pa, '+'), function(v) {
  1916. switch (v) {
  1917. case 'alt':
  1918. case 'ctrl':
  1919. case 'shift':
  1920. o[v] = true;
  1921. break;
  1922. default:
  1923. o.charCode = v.charCodeAt(0);
  1924. o.keyCode = v.toUpperCase().charCodeAt(0);
  1925. }
  1926. });
  1927. t.shortcuts[(o.ctrl ? 'ctrl' : '') + ',' + (o.alt ? 'alt' : '') + ',' + (o.shift ? 'shift' : '') + ',' + o.keyCode] = o;
  1928. });
  1929. return true;
  1930. },
  1931. /**
  1932. * Executes a command on the current instance. These commands can be TinyMCE internal commands prefixed with "mce" or
  1933. * they can be build in browser commands such as "Bold". A compleate list of browser commands is available on MSDN or Mozilla.org.
  1934. * This function will dispatch the execCommand function on each plugin, theme or the execcommand_callback option if none of these
  1935. * return true it will handle the command as a internal browser command.
  1936. *
  1937. * @method execCommand
  1938. * @param {String} cmd Command name to execute, for example mceLink or Bold.
  1939. * @param {Boolean} ui True/false state if a UI (dialog) should be presented or not.
  1940. * @param {mixed} val Optional command value, this can be anything.
  1941. * @param {Object} a Optional arguments object.
  1942. * @return {Boolean} True/false if the command was executed or not.
  1943. */
  1944. execCommand : function(cmd, ui, val, a) {
  1945. var t = this, s = 0, o, st;
  1946. if (!/^(mceAddUndoLevel|mceEndUndoLevel|mceBeginUndoLevel|mceRepaint|SelectAll)$/.test(cmd) && (!a || !a.skip_focus))
  1947. t.focus();
  1948. o = {};
  1949. t.onBeforeExecCommand.dispatch(t, cmd, ui, val, o);
  1950. if (o.terminate)
  1951. return false;
  1952. // Command callback
  1953. if (t.execCallback('execcommand_callback', t.id, t.selection.getNode(), cmd, ui, val)) {
  1954. t.onExecCommand.dispatch(t, cmd, ui, val, a);
  1955. return true;
  1956. }
  1957. // Registred commands
  1958. if (o = t.execCommands[cmd]) {
  1959. st = o.func.call(o.scope, ui, val);
  1960. // Fall through on true
  1961. if (st !== true) {
  1962. t.onExecCommand.dispatch(t, cmd, ui, val, a);
  1963. return st;
  1964. }
  1965. }
  1966. // Plugin commands
  1967. each(t.plugins, function(p) {
  1968. if (p.execCommand && p.execCommand(cmd, ui, val)) {
  1969. t.onExecCommand.dispatch(t, cmd, ui, val, a);
  1970. s = 1;
  1971. return false;
  1972. }
  1973. });
  1974. if (s)
  1975. return true;
  1976. // Theme commands
  1977. if (t.theme && t.theme.execCommand && t.theme.execCommand(cmd, ui, val)) {
  1978. t.onExecCommand.dispatch(t, cmd, ui, val, a);
  1979. return true;
  1980. }
  1981. // Editor commands
  1982. if (t.editorCommands.execCommand(cmd, ui, val)) {
  1983. t.onExecCommand.dispatch(t, cmd, ui, val, a);
  1984. return true;
  1985. }
  1986. // Browser commands
  1987. t.getDoc().execCommand(cmd, ui, val);
  1988. t.onExecCommand.dispatch(t, cmd, ui, val, a);
  1989. },
  1990. /**
  1991. * Returns a command specific state, for example if bold is enabled or not.
  1992. *
  1993. * @method queryCommandState
  1994. * @param {string} cmd Command to query state from.
  1995. * @return {Boolean} Command specific state, for example if bold is enabled or not.
  1996. */
  1997. queryCommandState : function(cmd) {
  1998. var t = this, o, s;
  1999. // Is hidden then return undefined
  2000. if (t._isHidden())
  2001. return;
  2002. // Registred commands
  2003. if (o = t.queryStateCommands[cmd]) {
  2004. s = o.func.call(o.scope);
  2005. // Fall though on true
  2006. if (s !== true)
  2007. return s;
  2008. }
  2009. // Registred commands
  2010. o = t.editorCommands.queryCommandState(cmd);
  2011. if (o !== -1)
  2012. return o;
  2013. // Browser commands
  2014. try {
  2015. return this.getDoc().queryCommandState(cmd);
  2016. } catch (ex) {
  2017. // Fails sometimes see bug: 1896577
  2018. }
  2019. },
  2020. /**
  2021. * Returns a command specific value, for example the current font size.
  2022. *
  2023. * @method queryCommandValue
  2024. * @param {string} c Command to query value from.
  2025. * @return {Object} Command specific value, for example the current font size.
  2026. */
  2027. queryCommandValue : function(c) {
  2028. var t = this, o, s;
  2029. // Is hidden then return undefined
  2030. if (t._isHidden())
  2031. return;
  2032. // Registred commands
  2033. if (o = t.queryValueCommands[c]) {
  2034. s = o.func.call(o.scope);
  2035. // Fall though on true
  2036. if (s !== true)
  2037. return s;
  2038. }
  2039. // Registred commands
  2040. o = t.editorCommands.queryCommandValue(c);
  2041. if (is(o))
  2042. return o;
  2043. // Browser commands
  2044. try {
  2045. return this.getDoc().queryCommandValue(c);
  2046. } catch (ex) {
  2047. // Fails sometimes see bug: 1896577
  2048. }
  2049. },
  2050. /**
  2051. * Shows the editor and hides any textarea/div that the editor is supposed to replace.
  2052. *
  2053. * @method show
  2054. */
  2055. show : function() {
  2056. var t = this;
  2057. DOM.show(t.getContainer());
  2058. DOM.hide(t.id);
  2059. t.load();
  2060. },
  2061. /**
  2062. * Hides the editor and shows any textarea/div that the editor is supposed to replace.
  2063. *
  2064. * @method hide
  2065. */
  2066. hide : function() {
  2067. var t = this, d = t.getDoc();
  2068. // Fixed bug where IE has a blinking cursor left from the editor
  2069. if (isIE && d)
  2070. d.execCommand('SelectAll');
  2071. // We must save before we hide so Safari doesn't crash
  2072. t.save();
  2073. DOM.hide(t.getContainer());
  2074. DOM.setStyle(t.id, 'display', t.orgDisplay);
  2075. },
  2076. /**
  2077. * Returns true/false if the editor is hidden or not.
  2078. *
  2079. * @method isHidden
  2080. * @return {Boolean} True/false if the editor is hidden or not.
  2081. */
  2082. isHidden : function() {
  2083. return !DOM.isHidden(this.id);
  2084. },
  2085. /**
  2086. * Sets the progress state, this will display a throbber/progess for the editor.
  2087. * This is ideal for asycronous operations like an AJAX save call.
  2088. *
  2089. * @method setProgressState
  2090. * @param {Boolean} b Boolean state if the progress should be shown or hidden.
  2091. * @param {Number} ti Optional time to wait before the progress gets shown.
  2092. * @param {Object} o Optional object to pass to the progress observers.
  2093. * @return {Boolean} Same as the input state.
  2094. * @example
  2095. * // Show progress for the active editor
  2096. * tinyMCE.activeEditor.setProgressState(true);
  2097. *
  2098. * // Hide progress for the active editor
  2099. * tinyMCE.activeEditor.setProgressState(false);
  2100. *
  2101. * // Show progress after 3 seconds
  2102. * tinyMCE.activeEditor.setProgressState(true, 3000);
  2103. */
  2104. setProgressState : function(b, ti, o) {
  2105. this.onSetProgressState.dispatch(this, b, ti, o);
  2106. return b;
  2107. },
  2108. /**
  2109. * Loads contents from the textarea or div element that got converted into an editor instance.
  2110. * This method will move the contents from that textarea or div into the editor by using setContent
  2111. * so all events etc that method has will get dispatched as well.
  2112. *
  2113. * @method load
  2114. * @param {Object} o Optional content object, this gets passed around through the whole load process.
  2115. * @return {String} HTML string that got set into the editor.
  2116. */
  2117. load : function(o) {
  2118. var t = this, e = t.getElement(), h;
  2119. if (e) {
  2120. o = o || {};
  2121. o.load = true;
  2122. // Double encode existing entities in the value
  2123. h = t.setContent(is(e.value) ? e.value : e.innerHTML, o);
  2124. o.element = e;
  2125. if (!o.no_events)
  2126. t.onLoadContent.dispatch(t, o);
  2127. o.element = e = null;
  2128. return h;
  2129. }
  2130. },
  2131. /**
  2132. * Saves the contents from a editor out to the textarea or div element that got converted into an editor instance.
  2133. * This method will move the HTML contents from the editor into that textarea or div by getContent
  2134. * so all events etc that method has will get dispatched as well.
  2135. *
  2136. * @method save
  2137. * @param {Object} o Optional content object, this gets passed around through the whole save process.
  2138. * @return {String} HTML string that got set into the textarea/div.
  2139. */
  2140. save : function(o) {
  2141. var t = this, e = t.getElement(), h, f;
  2142. if (!e || !t.initialized)
  2143. return;
  2144. o = o || {};
  2145. o.save = true;
  2146. // Add undo level will trigger onchange event
  2147. if (!o.no_events) {
  2148. t.undoManager.typing = false;
  2149. t.undoManager.add();
  2150. }
  2151. o.element = e;
  2152. h = o.content = t.getContent(o);
  2153. if (!o.no_events)
  2154. t.onSaveContent.dispatch(t, o);
  2155. h = o.content;
  2156. if (!/TEXTAREA|INPUT/i.test(e.nodeName)) {
  2157. e.innerHTML = h;
  2158. // Update hidden form element
  2159. if (f = DOM.getParent(t.id, 'form')) {
  2160. each(f.elements, function(e) {
  2161. if (e.name == t.id) {
  2162. e.value = h;
  2163. return false;
  2164. }
  2165. });
  2166. }
  2167. } else
  2168. e.value = h;
  2169. o.element = e = null;
  2170. return h;
  2171. },
  2172. /**
  2173. * Sets the specified content to the editor instance, this will cleanup the content before it gets set using
  2174. * the different cleanup rules options.
  2175. *
  2176. * @method setContent
  2177. * @param {String} content Content to set to editor, normally HTML contents but can be other formats as well.
  2178. * @param {Object} args Optional content object, this gets passed around through the whole set process.
  2179. * @return {String} HTML string that got set into the editor.
  2180. * @example
  2181. * // Sets the HTML contents of the activeEditor editor
  2182. * tinyMCE.activeEditor.setContent('<span>some</span> html');
  2183. *
  2184. * // Sets the raw contents of the activeEditor editor
  2185. * tinyMCE.activeEditor.setContent('<span>some</span> html', {format : 'raw'});
  2186. *
  2187. * // Sets the content of a specific editor (my_editor in this example)
  2188. * tinyMCE.get('my_editor').setContent(data);
  2189. *
  2190. * // Sets the bbcode contents of the activeEditor editor if the bbcode plugin was added
  2191. * tinyMCE.activeEditor.setContent('[b]some[/b] html', {format : 'bbcode'});
  2192. */
  2193. setContent : function(content, args) {
  2194. var self = this, rootNode, body = self.getBody(), forcedRootBlockName;
  2195. // Setup args object
  2196. args = args || {};
  2197. args.format = args.format || 'html';
  2198. args.set = true;
  2199. args.content = content;
  2200. // Do preprocessing
  2201. if (!args.no_events)
  2202. self.onBeforeSetContent.dispatch(self, args);
  2203. content = args.content;
  2204. // Padd empty content in Gecko and Safari. Commands will otherwise fail on the content
  2205. // It will also be impossible to place the caret in the editor unless there is a BR element present
  2206. if (!tinymce.isIE && (content.length === 0 || /^\s+$/.test(content))) {
  2207. forcedRootBlockName = self.settings.forced_root_block;
  2208. if (forcedRootBlockName)
  2209. content = '<' + forcedRootBlockName + '><br data-mce-bogus="1"></' + forcedRootBlockName + '>';
  2210. else
  2211. content = '<br data-mce-bogus="1">';
  2212. body.innerHTML = content;
  2213. self.selection.select(body, true);
  2214. self.selection.collapse(true);
  2215. return;
  2216. }
  2217. // Parse and serialize the html
  2218. if (args.format !== 'raw') {
  2219. content = new tinymce.html.Serializer({}, self.schema).serialize(
  2220. self.parser.parse(content)
  2221. );
  2222. }
  2223. // Set the new cleaned contents to the editor
  2224. args.content = tinymce.trim(content);
  2225. self.dom.setHTML(body, args.content);
  2226. // Do post processing
  2227. if (!args.no_events)
  2228. self.onSetContent.dispatch(self, args);
  2229. self.selection.normalize();
  2230. return args.content;
  2231. },
  2232. /**
  2233. * Gets the content from the editor instance, this will cleanup the content before it gets returned using
  2234. * the different cleanup rules options.
  2235. *
  2236. * @method getContent
  2237. * @param {Object} args Optional content object, this gets passed around through the whole get process.
  2238. * @return {String} Cleaned content string, normally HTML contents.
  2239. * @example
  2240. * // Get the HTML contents of the currently active editor
  2241. * console.debug(tinyMCE.activeEditor.getContent());
  2242. *
  2243. * // Get the raw contents of the currently active editor
  2244. * tinyMCE.activeEditor.getContent({format : 'raw'});
  2245. *
  2246. * // Get content of a specific editor:
  2247. * tinyMCE.get('content id').getContent()
  2248. */
  2249. getContent : function(args) {
  2250. var self = this, content;
  2251. // Setup args object
  2252. args = args || {};
  2253. args.format = args.format || 'html';
  2254. args.get = true;
  2255. // Do preprocessing
  2256. if (!args.no_events)
  2257. self.onBeforeGetContent.dispatch(self, args);
  2258. // Get raw contents or by default the cleaned contents
  2259. if (args.format == 'raw')
  2260. content = self.getBody().innerHTML;
  2261. else
  2262. content = self.serializer.serialize(self.getBody(), args);
  2263. args.content = tinymce.trim(content);
  2264. // Do post processing
  2265. if (!args.no_events)
  2266. self.onGetContent.dispatch(self, args);
  2267. return args.content;
  2268. },
  2269. /**
  2270. * Returns true/false if the editor is dirty or not. It will get dirty if the user has made modifications to the contents.
  2271. *
  2272. * @method isDirty
  2273. * @return {Boolean} True/false if the editor is dirty or not. It will get dirty if the user has made modifications to the contents.
  2274. * @example
  2275. * if (tinyMCE.activeEditor.isDirty())
  2276. * alert("You must save your contents.");
  2277. */
  2278. isDirty : function() {
  2279. var self = this;
  2280. return tinymce.trim(self.startContent) != tinymce.trim(self.getContent({format : 'raw', no_events : 1})) && !self.isNotDirty;
  2281. },
  2282. /**
  2283. * Returns the editors container element. The container element wrappes in
  2284. * all the elements added to the page for the editor. Such as UI, iframe etc.
  2285. *
  2286. * @method getContainer
  2287. * @return {Element} HTML DOM element for the editor container.
  2288. */
  2289. getContainer : function() {
  2290. var t = this;
  2291. if (!t.container)
  2292. t.container = DOM.get(t.editorContainer || t.id + '_parent');
  2293. return t.container;
  2294. },
  2295. /**
  2296. * Returns the editors content area container element. The this element is the one who
  2297. * holds the iframe or the editable element.
  2298. *
  2299. * @method getContentAreaContainer
  2300. * @return {Element} HTML DOM element for the editor area container.
  2301. */
  2302. getContentAreaContainer : function() {
  2303. return this.contentAreaContainer;
  2304. },
  2305. /**
  2306. * Returns the target element/textarea that got replaced with a TinyMCE editor instance.
  2307. *
  2308. * @method getElement
  2309. * @return {Element} HTML DOM element for the replaced element.
  2310. */
  2311. getElement : function() {
  2312. return DOM.get(this.settings.content_element || this.id);
  2313. },
  2314. /**
  2315. * Returns the iframes window object.
  2316. *
  2317. * @method getWin
  2318. * @return {Window} Iframe DOM window object.
  2319. */
  2320. getWin : function() {
  2321. var t = this, e;
  2322. if (!t.contentWindow) {
  2323. e = DOM.get(t.id + "_ifr");
  2324. if (e)
  2325. t.contentWindow = e.contentWindow;
  2326. }
  2327. return t.contentWindow;
  2328. },
  2329. /**
  2330. * Returns the iframes document object.
  2331. *
  2332. * @method getDoc
  2333. * @return {Document} Iframe DOM document object.
  2334. */
  2335. getDoc : function() {
  2336. var t = this, w;
  2337. if (!t.contentDocument) {
  2338. w = t.getWin();
  2339. if (w)
  2340. t.contentDocument = w.document;
  2341. }
  2342. return t.contentDocument;
  2343. },
  2344. /**
  2345. * Returns the iframes body element.
  2346. *
  2347. * @method getBody
  2348. * @return {Element} Iframe body element.
  2349. */
  2350. getBody : function() {
  2351. return this.bodyElement || this.getDoc().body;
  2352. },
  2353. /**
  2354. * URL converter function this gets executed each time a user adds an img, a or
  2355. * any other element that has a URL in it. This will be called both by the DOM and HTML
  2356. * manipulation functions.
  2357. *
  2358. * @method convertURL
  2359. * @param {string} u URL to convert.
  2360. * @param {string} n Attribute name src, href etc.
  2361. * @param {string/HTMLElement} Tag name or HTML DOM element depending on HTML or DOM insert.
  2362. * @return {string} Converted URL string.
  2363. */
  2364. convertURL : function(u, n, e) {
  2365. var t = this, s = t.settings;
  2366. // Use callback instead
  2367. if (s.urlconverter_callback)
  2368. return t.execCallback('urlconverter_callback', u, e, true, n);
  2369. // Don't convert link href since thats the CSS files that gets loaded into the editor also skip local file URLs
  2370. if (!s.convert_urls || (e && e.nodeName == 'LINK') || u.indexOf('file:') === 0)
  2371. return u;
  2372. // Convert to relative
  2373. if (s.relative_urls)
  2374. return t.documentBaseURI.toRelative(u);
  2375. // Convert to absolute
  2376. u = t.documentBaseURI.toAbsolute(u, s.remove_script_host);
  2377. return u;
  2378. },
  2379. /**
  2380. * Adds visual aid for tables, anchors etc so they can be more easily edited inside the editor.
  2381. *
  2382. * @method addVisual
  2383. * @param {Element} e Optional root element to loop though to find tables etc that needs the visual aid.
  2384. */
  2385. addVisual : function(e) {
  2386. var t = this, s = t.settings;
  2387. e = e || t.getBody();
  2388. if (!is(t.hasVisual))
  2389. t.hasVisual = s.visual;
  2390. each(t.dom.select('table,a', e), function(e) {
  2391. var v;
  2392. switch (e.nodeName) {
  2393. case 'TABLE':
  2394. v = t.dom.getAttrib(e, 'border');
  2395. if (!v || v == '0') {
  2396. if (t.hasVisual)
  2397. t.dom.addClass(e, s.visual_table_class);
  2398. else
  2399. t.dom.removeClass(e, s.visual_table_class);
  2400. }
  2401. return;
  2402. case 'A':
  2403. v = t.dom.getAttrib(e, 'name');
  2404. if (v) {
  2405. if (t.hasVisual)
  2406. t.dom.addClass(e, 'mceItemAnchor');
  2407. else
  2408. t.dom.removeClass(e, 'mceItemAnchor');
  2409. }
  2410. return;
  2411. }
  2412. });
  2413. t.onVisualAid.dispatch(t, e, t.hasVisual);
  2414. },
  2415. /**
  2416. * Removes the editor from the dom and tinymce collection.
  2417. *
  2418. * @method remove
  2419. */
  2420. remove : function() {
  2421. var t = this, e = t.getContainer();
  2422. t.removed = 1; // Cancels post remove event execution
  2423. t.hide();
  2424. t.execCallback('remove_instance_callback', t);
  2425. t.onRemove.dispatch(t);
  2426. // Clear all execCommand listeners this is required to avoid errors if the editor was removed inside another command
  2427. t.onExecCommand.listeners = [];
  2428. tinymce.remove(t);
  2429. DOM.remove(e);
  2430. },
  2431. /**
  2432. * Destroys the editor instance by removing all events, element references or other resources
  2433. * that could leak memory. This method will be called automatically when the page is unloaded
  2434. * but you can also call it directly if you know what you are doing.
  2435. *
  2436. * @method destroy
  2437. * @param {Boolean} s Optional state if the destroy is an automatic destroy or user called one.
  2438. */
  2439. destroy : function(s) {
  2440. var t = this;
  2441. // One time is enough
  2442. if (t.destroyed)
  2443. return;
  2444. if (!s) {
  2445. tinymce.removeUnload(t.destroy);
  2446. tinyMCE.onBeforeUnload.remove(t._beforeUnload);
  2447. // Manual destroy
  2448. if (t.theme && t.theme.destroy)
  2449. t.theme.destroy();
  2450. // Destroy controls, selection and dom
  2451. t.controlManager.destroy();
  2452. t.selection.destroy();
  2453. t.dom.destroy();
  2454. // Remove all events
  2455. // Don't clear the window or document if content editable
  2456. // is enabled since other instances might still be present
  2457. if (!t.settings.content_editable) {
  2458. Event.clear(t.getWin());
  2459. Event.clear(t.getDoc());
  2460. }
  2461. Event.clear(t.getBody());
  2462. Event.clear(t.formElement);
  2463. }
  2464. if (t.formElement) {
  2465. t.formElement.submit = t.formElement._mceOldSubmit;
  2466. t.formElement._mceOldSubmit = null;
  2467. }
  2468. t.contentAreaContainer = t.formElement = t.container = t.settings.content_element = t.bodyElement = t.contentDocument = t.contentWindow = null;
  2469. if (t.selection)
  2470. t.selection = t.selection.win = t.selection.dom = t.selection.dom.doc = null;
  2471. t.destroyed = 1;
  2472. },
  2473. // Internal functions
  2474. _addEvents : function() {
  2475. // 'focus', 'blur', 'dblclick', 'beforedeactivate', submit, reset
  2476. var t = this, i, s = t.settings, dom = t.dom, lo = {
  2477. mouseup : 'onMouseUp',
  2478. mousedown : 'onMouseDown',
  2479. click : 'onClick',
  2480. keyup : 'onKeyUp',
  2481. keydown : 'onKeyDown',
  2482. keypress : 'onKeyPress',
  2483. submit : 'onSubmit',
  2484. reset : 'onReset',
  2485. contextmenu : 'onContextMenu',
  2486. dblclick : 'onDblClick',
  2487. paste : 'onPaste' // Doesn't work in all browsers yet
  2488. };
  2489. function eventHandler(e, o) {
  2490. var ty = e.type;
  2491. // Don't fire events when it's removed
  2492. if (t.removed)
  2493. return;
  2494. // Generic event handler
  2495. if (t.onEvent.dispatch(t, e, o) !== false) {
  2496. // Specific event handler
  2497. t[lo[e.fakeType || e.type]].dispatch(t, e, o);
  2498. }
  2499. };
  2500. // Add DOM events
  2501. each(lo, function(v, k) {
  2502. switch (k) {
  2503. case 'contextmenu':
  2504. dom.bind(t.getDoc(), k, eventHandler);
  2505. break;
  2506. case 'paste':
  2507. dom.bind(t.getBody(), k, function(e) {
  2508. eventHandler(e);
  2509. });
  2510. break;
  2511. case 'submit':
  2512. case 'reset':
  2513. dom.bind(t.getElement().form || DOM.getParent(t.id, 'form'), k, eventHandler);
  2514. break;
  2515. default:
  2516. dom.bind(s.content_editable ? t.getBody() : t.getDoc(), k, eventHandler);
  2517. }
  2518. });
  2519. dom.bind(s.content_editable ? t.getBody() : (isGecko ? t.getDoc() : t.getWin()), 'focus', function(e) {
  2520. t.focus(true);
  2521. });
  2522. // #ifdef contentEditable
  2523. if (s.content_editable && tinymce.isOpera) {
  2524. // Opera doesn't support focus event for contentEditable elements so we need to fake it
  2525. function doFocus(e) {
  2526. t.focus(true);
  2527. };
  2528. dom.bind(t.getBody(), 'click', doFocus);
  2529. dom.bind(t.getBody(), 'keydown', doFocus);
  2530. }
  2531. // #endif
  2532. // Fixes bug where a specified document_base_uri could result in broken images
  2533. // This will also fix drag drop of images in Gecko
  2534. if (tinymce.isGecko) {
  2535. dom.bind(t.getDoc(), 'DOMNodeInserted', function(e) {
  2536. var v;
  2537. e = e.target;
  2538. if (e.nodeType === 1 && e.nodeName === 'IMG' && (v = e.getAttribute('data-mce-src')))
  2539. e.src = t.documentBaseURI.toAbsolute(v);
  2540. });
  2541. }
  2542. // Set various midas options in Gecko
  2543. if (isGecko) {
  2544. function setOpts() {
  2545. var t = this, d = t.getDoc(), s = t.settings;
  2546. if (isGecko && !s.readonly) {
  2547. t._refreshContentEditable();
  2548. try {
  2549. // Try new Gecko method
  2550. d.execCommand("styleWithCSS", 0, false);
  2551. } catch (ex) {
  2552. // Use old method
  2553. if (!t._isHidden())
  2554. try {d.execCommand("useCSS", 0, true);} catch (ex) {}
  2555. }
  2556. if (!s.table_inline_editing)
  2557. try {d.execCommand('enableInlineTableEditing', false, false);} catch (ex) {}
  2558. if (!s.object_resizing)
  2559. try {d.execCommand('enableObjectResizing', false, false);} catch (ex) {}
  2560. }
  2561. };
  2562. t.onBeforeExecCommand.add(setOpts);
  2563. t.onMouseDown.add(setOpts);
  2564. }
  2565. // Add node change handlers
  2566. t.onMouseUp.add(t.nodeChanged);
  2567. //t.onClick.add(t.nodeChanged);
  2568. t.onKeyUp.add(function(ed, e) {
  2569. var c = e.keyCode;
  2570. if ((c >= 33 && c <= 36) || (c >= 37 && c <= 40) || c == 13 || c == 45 || c == 46 || c == 8 || (tinymce.isMac && (c == 91 || c == 93)) || e.ctrlKey)
  2571. t.nodeChanged();
  2572. });
  2573. // Add block quote deletion handler
  2574. t.onKeyDown.add(function(ed, e) {
  2575. // Was the BACKSPACE key pressed?
  2576. if (e.keyCode != 8)
  2577. return;
  2578. var n = ed.selection.getRng().startContainer;
  2579. var offset = ed.selection.getRng().startOffset;
  2580. while (n && n.nodeType && n.nodeType != 1 && n.parentNode)
  2581. n = n.parentNode;
  2582. // Is the cursor at the beginning of a blockquote?
  2583. if (n && n.parentNode && n.parentNode.tagName === 'BLOCKQUOTE' && n.parentNode.firstChild == n && offset == 0) {
  2584. // Remove the blockquote
  2585. ed.formatter.toggle('blockquote', null, n.parentNode);
  2586. // Move the caret to the beginning of n
  2587. var rng = ed.selection.getRng();
  2588. rng.setStart(n, 0);
  2589. rng.setEnd(n, 0);
  2590. ed.selection.setRng(rng);
  2591. ed.selection.collapse(false);
  2592. }
  2593. });
  2594. // Add reset handler
  2595. t.onReset.add(function() {
  2596. t.setContent(t.startContent, {format : 'raw'});
  2597. });
  2598. // Add shortcuts
  2599. if (s.custom_shortcuts) {
  2600. if (s.custom_undo_redo_keyboard_shortcuts) {
  2601. t.addShortcut('ctrl+z', t.getLang('undo_desc'), 'Undo');
  2602. t.addShortcut('ctrl+y', t.getLang('redo_desc'), 'Redo');
  2603. }
  2604. // Add default shortcuts for gecko
  2605. t.addShortcut('ctrl+b', t.getLang('bold_desc'), 'Bold');
  2606. t.addShortcut('ctrl+i', t.getLang('italic_desc'), 'Italic');
  2607. t.addShortcut('ctrl+u', t.getLang('underline_desc'), 'Underline');
  2608. // BlockFormat shortcuts keys
  2609. for (i=1; i<=6; i++)
  2610. t.addShortcut('ctrl+' + i, '', ['FormatBlock', false, 'h' + i]);
  2611. t.addShortcut('ctrl+7', '', ['FormatBlock', false, 'p']);
  2612. t.addShortcut('ctrl+8', '', ['FormatBlock', false, 'div']);
  2613. t.addShortcut('ctrl+9', '', ['FormatBlock', false, 'address']);
  2614. function find(e) {
  2615. var v = null;
  2616. if (!e.altKey && !e.ctrlKey && !e.metaKey)
  2617. return v;
  2618. each(t.shortcuts, function(o) {
  2619. if (tinymce.isMac && o.ctrl != e.metaKey)
  2620. return;
  2621. else if (!tinymce.isMac && o.ctrl != e.ctrlKey)
  2622. return;
  2623. if (o.alt != e.altKey)
  2624. return;
  2625. if (o.shift != e.shiftKey)
  2626. return;
  2627. if (e.keyCode == o.keyCode || (e.charCode && e.charCode == o.charCode)) {
  2628. v = o;
  2629. return false;
  2630. }
  2631. });
  2632. return v;
  2633. };
  2634. t.onKeyUp.add(function(ed, e) {
  2635. var o = find(e);
  2636. if (o)
  2637. return Event.cancel(e);
  2638. });
  2639. t.onKeyPress.add(function(ed, e) {
  2640. var o = find(e);
  2641. if (o)
  2642. return Event.cancel(e);
  2643. });
  2644. t.onKeyDown.add(function(ed, e) {
  2645. var o = find(e);
  2646. if (o) {
  2647. o.func.call(o.scope);
  2648. return Event.cancel(e);
  2649. }
  2650. });
  2651. }
  2652. if (tinymce.isIE) {
  2653. // Fix so resize will only update the width and height attributes not the styles of an image
  2654. // It will also block mceItemNoResize items
  2655. dom.bind(t.getDoc(), 'controlselect', function(e) {
  2656. var re = t.resizeInfo, cb;
  2657. e = e.target;
  2658. // Don't do this action for non image elements
  2659. if (e.nodeName !== 'IMG')
  2660. return;
  2661. if (re)
  2662. dom.unbind(re.node, re.ev, re.cb);
  2663. if (!dom.hasClass(e, 'mceItemNoResize')) {
  2664. ev = 'resizeend';
  2665. cb = dom.bind(e, ev, function(e) {
  2666. var v;
  2667. e = e.target;
  2668. if (v = dom.getStyle(e, 'width')) {
  2669. dom.setAttrib(e, 'width', v.replace(/[^0-9%]+/g, ''));
  2670. dom.setStyle(e, 'width', '');
  2671. }
  2672. if (v = dom.getStyle(e, 'height')) {
  2673. dom.setAttrib(e, 'height', v.replace(/[^0-9%]+/g, ''));
  2674. dom.setStyle(e, 'height', '');
  2675. }
  2676. });
  2677. } else {
  2678. ev = 'resizestart';
  2679. cb = dom.bind(e, 'resizestart', Event.cancel, Event);
  2680. }
  2681. re = t.resizeInfo = {
  2682. node : e,
  2683. ev : ev,
  2684. cb : cb
  2685. };
  2686. });
  2687. }
  2688. if (tinymce.isOpera) {
  2689. t.onClick.add(function(ed, e) {
  2690. Event.prevent(e);
  2691. });
  2692. }
  2693. // Add custom undo/redo handlers
  2694. if (s.custom_undo_redo) {
  2695. function addUndo() {
  2696. t.undoManager.typing = false;
  2697. t.undoManager.add();
  2698. };
  2699. dom.bind(t.getDoc(), 'focusout', function(e) {
  2700. if (!t.removed && t.undoManager.typing)
  2701. addUndo();
  2702. });
  2703. // Add undo level when contents is drag/dropped within the editor
  2704. t.dom.bind(t.dom.getRoot(), 'dragend', function(e) {
  2705. addUndo();
  2706. });
  2707. t.onKeyUp.add(function(ed, e) {
  2708. var keyCode = e.keyCode;
  2709. if ((keyCode >= 33 && keyCode <= 36) || (keyCode >= 37 && keyCode <= 40) || keyCode == 13 || keyCode == 45 || e.ctrlKey)
  2710. addUndo();
  2711. });
  2712. t.onKeyDown.add(function(ed, e) {
  2713. var keyCode = e.keyCode, sel;
  2714. if (keyCode == 8) {
  2715. sel = t.getDoc().selection;
  2716. // Fix IE control + backspace browser bug
  2717. if (sel && sel.createRange && sel.createRange().item) {
  2718. t.undoManager.beforeChange();
  2719. ed.dom.remove(sel.createRange().item(0));
  2720. addUndo();
  2721. return Event.cancel(e);
  2722. }
  2723. }
  2724. // Is caracter positon keys left,right,up,down,home,end,pgdown,pgup,enter
  2725. if ((keyCode >= 33 && keyCode <= 36) || (keyCode >= 37 && keyCode <= 40) || keyCode == 13 || keyCode == 45) {
  2726. // Add position before enter key is pressed, used by IE since it still uses the default browser behavior
  2727. // Todo: Remove this once we normalize enter behavior on IE
  2728. if (tinymce.isIE && keyCode == 13)
  2729. t.undoManager.beforeChange();
  2730. if (t.undoManager.typing)
  2731. addUndo();
  2732. return;
  2733. }
  2734. // If key isn't shift,ctrl,alt,capslock,metakey
  2735. if ((keyCode < 16 || keyCode > 20) && keyCode != 224 && keyCode != 91 && !t.undoManager.typing) {
  2736. t.undoManager.beforeChange();
  2737. t.undoManager.typing = true;
  2738. t.undoManager.add();
  2739. }
  2740. });
  2741. t.onMouseDown.add(function() {
  2742. if (t.undoManager.typing)
  2743. addUndo();
  2744. });
  2745. }
  2746. // Bug fix for FireFox keeping styles from end of selection instead of start.
  2747. if (tinymce.isGecko) {
  2748. function getAttributeApplyFunction() {
  2749. var template = t.dom.getAttribs(t.selection.getStart().cloneNode(false));
  2750. return function() {
  2751. var target = t.selection.getStart();
  2752. if (target !== t.getBody()) {
  2753. t.dom.setAttrib(target, "style", null);
  2754. each(template, function(attr) {
  2755. target.setAttributeNode(attr.cloneNode(true));
  2756. });
  2757. }
  2758. };
  2759. }
  2760. function isSelectionAcrossElements() {
  2761. var s = t.selection;
  2762. return !s.isCollapsed() && s.getStart() != s.getEnd();
  2763. }
  2764. t.onKeyPress.add(function(ed, e) {
  2765. var applyAttributes;
  2766. if ((e.keyCode == 8 || e.keyCode == 46) && isSelectionAcrossElements()) {
  2767. applyAttributes = getAttributeApplyFunction();
  2768. t.getDoc().execCommand('delete', false, null);
  2769. applyAttributes();
  2770. return Event.cancel(e);
  2771. }
  2772. });
  2773. t.dom.bind(t.getDoc(), 'cut', function(e) {
  2774. var applyAttributes;
  2775. if (isSelectionAcrossElements()) {
  2776. applyAttributes = getAttributeApplyFunction();
  2777. t.onKeyUp.addToTop(Event.cancel, Event);
  2778. setTimeout(function() {
  2779. applyAttributes();
  2780. t.onKeyUp.remove(Event.cancel, Event);
  2781. }, 0);
  2782. }
  2783. });
  2784. }
  2785. },
  2786. _refreshContentEditable : function() {
  2787. var self = this, body, parent;
  2788. // Check if the editor was hidden and the re-initalize contentEditable mode by removing and adding the body again
  2789. if (self._isHidden()) {
  2790. body = self.getBody();
  2791. parent = body.parentNode;
  2792. parent.removeChild(body);
  2793. parent.appendChild(body);
  2794. body.focus();
  2795. }
  2796. },
  2797. _isHidden : function() {
  2798. var s;
  2799. if (!isGecko)
  2800. return 0;
  2801. // Weird, wheres that cursor selection?
  2802. s = this.selection.getSel();
  2803. return (!s || !s.rangeCount || s.rangeCount == 0);
  2804. }
  2805. });
  2806. })(tinymce);