FCKeditor 2.6.6
[freeside.git] / httemplate / elements / fckeditor / editor / plugins / bbcode / fckplugin.js
1 /*\r
2  * FCKeditor - The text editor for Internet - http://www.fckeditor.net\r
3  * Copyright (C) 2003-2010 Frederico Caldeira Knabben\r
4  *\r
5  * == BEGIN LICENSE ==\r
6  *\r
7  * Licensed under the terms of any of the following licenses at your\r
8  * choice:\r
9  *\r
10  *  - GNU General Public License Version 2 or later (the "GPL")\r
11  *    http://www.gnu.org/licenses/gpl.html\r
12  *\r
13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")\r
14  *    http://www.gnu.org/licenses/lgpl.html\r
15  *\r
16  *  - Mozilla Public License Version 1.1 or later (the "MPL")\r
17  *    http://www.mozilla.org/MPL/MPL-1.1.html\r
18  *\r
19  * == END LICENSE ==\r
20  *\r
21  * This is a sample implementation for a custom Data Processor for basic BBCode.\r
22  */\r
23 \r
24 FCK.DataProcessor =\r
25 {\r
26         /*\r
27          * Returns a string representing the HTML format of "data". The returned\r
28          * value will be loaded in the editor.\r
29          * The HTML must be from <html> to </html>, eventually including\r
30          * the DOCTYPE.\r
31          *     @param {String} data The data to be converted in the\r
32          *            DataProcessor specific format.\r
33          */\r
34         ConvertToHtml : function( data )\r
35         {\r
36                 // Convert < and > to their HTML entities.\r
37         data = data.replace( /</g, '&lt;' ) ;\r
38         data = data.replace( />/g, '&gt;' ) ;\r
39 \r
40         // Convert line breaks to <br>.\r
41         data = data.replace( /(?:\r\n|\n|\r)/g, '<br>' ) ;\r
42 \r
43         // [url]\r
44         data = data.replace( /\[url\](.+?)\[\/url]/gi, '<a href="$1">$1</a>' ) ;\r
45         data = data.replace( /\[url\=([^\]]+)](.+?)\[\/url]/gi, '<a href="$1">$2</a>' ) ;\r
46 \r
47         // [b]\r
48         data = data.replace( /\[b\](.+?)\[\/b]/gi, '<b>$1</b>' ) ;\r
49 \r
50         // [i]\r
51         data = data.replace( /\[i\](.+?)\[\/i]/gi, '<i>$1</i>' ) ;\r
52 \r
53         // [u]\r
54         data = data.replace( /\[u\](.+?)\[\/u]/gi, '<u>$1</u>' ) ;\r
55 \r
56                 return '<html><head><title></title></head><body>' + data + '</body></html>' ;\r
57         },\r
58 \r
59         /*\r
60          * Converts a DOM (sub-)tree to a string in the data format.\r
61          *     @param {Object} rootNode The node that contains the DOM tree to be\r
62          *            converted to the data format.\r
63          *     @param {Boolean} excludeRoot Indicates that the root node must not\r
64          *            be included in the conversion, only its children.\r
65          *     @param {Boolean} format Indicates that the data must be formatted\r
66          *            for human reading. Not all Data Processors may provide it.\r
67          */\r
68         ConvertToDataFormat : function( rootNode, excludeRoot, ignoreIfEmptyParagraph, format )\r
69         {\r
70                 var data = rootNode.innerHTML ;\r
71 \r
72                 // Convert <br> to line breaks.\r
73                 data = data.replace( /<br(?=[ \/>]).*?>/gi, '\r\n') ;\r
74 \r
75                 // [url]\r
76                 data = data.replace( /<a .*?href=(["'])(.+?)\1.*?>(.+?)<\/a>/gi, '[url=$2]$3[/url]') ;\r
77 \r
78                 // [b]\r
79                 data = data.replace( /<(?:b|strong)>/gi, '[b]') ;\r
80                 data = data.replace( /<\/(?:b|strong)>/gi, '[/b]') ;\r
81 \r
82                 // [i]\r
83                 data = data.replace( /<(?:i|em)>/gi, '[i]') ;\r
84                 data = data.replace( /<\/(?:i|em)>/gi, '[/i]') ;\r
85 \r
86                 // [u]\r
87                 data = data.replace( /<u>/gi, '[u]') ;\r
88                 data = data.replace( /<\/u>/gi, '[/u]') ;\r
89 \r
90                 // Remove remaining tags.\r
91                 data = data.replace( /<[^>]+>/g, '') ;\r
92 \r
93                 return data ;\r
94         },\r
95 \r
96         /*\r
97          * Makes any necessary changes to a piece of HTML for insertion in the\r
98          * editor selection position.\r
99          *     @param {String} html The HTML to be fixed.\r
100          */\r
101         FixHtml : function( html )\r
102         {\r
103                 return html ;\r
104         }\r
105 } ;\r
106 \r
107 // This Data Processor doesn't support <p>, so let's use <br>.\r
108 FCKConfig.EnterMode = 'br' ;\r
109 \r
110 // To avoid pasting invalid markup (which is discarded in any case), let's\r
111 // force pasting to plain text.\r
112 FCKConfig.ForcePasteAsPlainText = true ;\r
113 \r
114 // Rename the "Source" buttom to "BBCode".\r
115 FCKToolbarItems.RegisterItem( 'Source', new FCKToolbarButton( 'Source', 'BBCode', null, FCK_TOOLBARITEM_ICONTEXT, true, true, 1 ) ) ;\r
116 \r
117 // Let's enforce the toolbar to the limits of this Data Processor. A custom\r
118 // toolbar set may be defined in the configuration file with more or less entries.\r
119 FCKConfig.ToolbarSets["Default"] = [\r
120         ['Source'],\r
121         ['Bold','Italic','Underline','-','Link'],\r
122         ['About']\r
123 ] ;\r