1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 */
22
23// Register the related commands.
24
25FCKCommands.RegisterCommand( 'Plugin_Tool',
26   new FCKDialogCommand( "Plugin Tool", "Plugin Tool",
27   FCKConfig.PluginsPath + 'plugintool/plugin_tool.html',  475, 400 ) ) ;
28
29
30var oPluginTool		= new FCKToolbarButton( 'Plugin_Tool', 'Plugin Tool' ) ;
31oPluginTool.IconPath	= FCKConfig.PluginsPath + 'plugintool/plugin.gif' ;
32
33FCKToolbarItems.RegisterItem( 'Plugin_Tool', oPluginTool ) ;	// 'Format_Table' is the name used in the Toolbar config.
34
35// The object used for all Abbr operations.
36var FCKAbbr = new Object() ;
37
38// Insert a new Abbr
39FCKAbbr.Insert = function(val, isSafari, stet) {
40
41    val = val.replace(/^\s+/,'');
42    val = val.replace(/\s+$/,'');
43
44    if(val) {
45          val = '"' + val + '"';
46    }
47
48	var hrefStartHtml	= (val == '') ? '' : '<plugin title=' + val + '>';
49	var hrefEndHtml		= (val == '') ? '' : '</plugin>&nbsp;';
50
51    var reset = false;
52
53	mySelection = ( FCKBrowserInfo.IsIE) ? FCKSelection.GetSelectedHTML(isSafari) : removeBR(FCKSelection.GetSelectedHTML(isSafari));
54
55    mySelection = mySelection.replace(/^\s+/,"");
56    mySelection = mySelection.replace(/\s+$/,"");
57
58    if(mySelection.match(/<\/FCK:plugin>/)) {
59            reset = true;
60    }
61    mySelection = mySelection.replace(/<\/FCK:plugin>/gi,"");
62    mySelection = mySelection.replace(/<FCK:plugin.*?>/gi,"");
63
64   if(!stet) {
65        mySelection = mySelection.replace(/<\/P>/gi," <br><br> ");
66        mySelection = mySelection.replace(/^<P.*?>/gi," <br><br> ");
67        mySelection = mySelection.replace(/&nbsp;/gi, "");  // for IE in <indent>
68
69        mySelection = mySelection.replace(/<indent.*?indent>/gi, "");
70
71        mySelection = mySelection.replace(/<P>/gi," &lt;p&gt; ");
72        mySelection = mySelection.replace(/<P.*?>/gi," ");
73
74        mySelection = mySelection.replace(/<br>/g," <br> ");
75        mySelection = mySelection.replace(/<br\s+\/>/g," <br> ");
76  }
77
78    if(FCKBrowserInfo.IsIE && reset) {
79        hrefHtml = mySelection;
80   }
81	else {
82      hrefHtml = hrefStartHtml+mySelection+hrefEndHtml;
83	}
84
85
86
87//////////////////////////////////////////////////////
88// 	choose one of these two lines; in fckeditor 2.5 both lines can be skipped!!!
89//
90 //	hrefHtml = FCK.ProtectTags(hrefStartHtml) ;								// needed because in fckeditor 2.4 protected tags only works with SetHTML
91	hrefHtml = ProtectTags( hrefHtml ) ;									// needed because IE doesn't support <abbr> and it breaks it.
92/////////////////////////////////////////////////////
93
94	FCK.InsertHtml(hrefHtml);
95}
96
97FCKAbbr.InsertEdited = function(val) {
98
99	//mySelection = ( FCKBrowserInfo.IsIE) ? FCKSelection.GetSelectedHTML() : removeBR(FCKSelection.GetSelectedHTML());
100
101    val = val.replace(/</g,"&lt;");
102	hrefHtml = val;
103
104
105	FCK.InsertHtml(hrefHtml);
106}
107
108FCKSelection.GetSelectedHTML = function(isSafari) {
109
110							// see http://www.quirksmode.org/js/selected.html for other browsers
111	if( FCKBrowserInfo.IsIE) {												// IE
112		var oRange = FCK.EditorDocument.selection.createRange() ;
113		//if an object like a table is deleted, the call to GetType before getting again a range returns Control
114		switch ( this.GetType() ) {
115			case 'Control' :
116			return oRange.item(0).outerHTML;
117
118			case 'None' :
119			return '' ;
120
121			default :
122			return oRange.htmlText ;
123		}
124	}
125	else if ( FCKBrowserInfo.IsGecko || isSafari ) {									// Mozilla, Safari
126
127									// Mozilla, Safari
128		var oSelection = FCK.EditorWindow.getSelection();
129		//Gecko doesn't provide a function to get the innerHTML of a selection,
130		//so we must clone the selection to a temporary element and check that innerHTML
131		var e = FCK.EditorDocument.createElement( 'DIV' );
132		for ( var i = 0 ; i < oSelection.rangeCount ; i++ ) {
133			e.appendChild( oSelection.getRangeAt(i).cloneContents() );
134		}
135		return e.innerHTML;
136	}
137}
138
139function removeBR(input) {							/* Used with Gecko */
140	var output = "";
141	for (var i = 0; i < input.length; i++) {
142		if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
143			i++;
144			output += " ";
145		}
146		else {
147			output += input.charAt(i);
148   		}
149	}
150	return output;
151}
152
153// put it into the contextmenu (optional)
154FCK.ContextMenu.RegisterListener( {
155	AddItems : function( menu, tag, tagName ) {
156		// when the option is displayed, show a separator then the command
157		menu.AddSeparator() ;
158		// the command needs the registered command name, the title for the context menu, and the icon path
159		menu.AddItem( 'Plugin_Tool', 'Plugin Tool', oPluginTool.IconPath) ;
160	}
161}
162);
163
164function ProtectTags ( html ) {
165									// copied from _source/internals/fck.js in fckeditor 2.4
166	// IE doesn't support <abbr> and it breaks it. Let's protect it.
167	if ( FCKBrowserInfo.IsIE ) {
168		var sTags = 'plugin' ;
169
170		var oRegex = new RegExp( '<(' + sTags + ')([ \>])', 'gi' ) ;
171		html = html.replace( oRegex, '<FCK:$1$2' ) ;
172
173		oRegex = new RegExp( '<\/(' + sTags + ')>', 'gi' ) ;
174		html = html.replace( oRegex, '<\/FCK:$1>' ) ;
175
176	}
177	return html ;
178}
179
180
181