]> git.mxchange.org Git - friendica.git/blob - library/tinymce/jscripts/tiny_mce/plugins/bbcode/editor_plugin_src.js
updated tinymce to 3.5.11
[friendica.git] / library / tinymce / jscripts / tiny_mce / plugins / bbcode / editor_plugin_src.js
1 /**\r
2  * editor_plugin_src.js\r
3  *\r
4  * Copyright 2009, Moxiecode Systems AB\r
5  * Released under LGPL License.\r
6  *\r
7  * License: http://tinymce.moxiecode.com/license\r
8  * Contributing: http://tinymce.moxiecode.com/contributing\r
9  */\r
10 \r
11 /* Macgirvin Aug-2010 changed from punbb to dfrn dialect */
12
13 (function() {\r
14         tinymce.create('tinymce.plugins.BBCodePlugin', {\r
15                 init : function(ed, url) {\r
16                         var t = this, dialect = ed.getParam('bbcode_dialect', 'dfrn').toLowerCase();
17 \r
18                         ed.onBeforeSetContent.add(function(ed, o) {\r
19                                 o.content = t['_' + dialect + '_bbcode2html'](o.content);\r
20                         });\r
21 \r
22                         ed.onPostProcess.add(function(ed, o) {\r
23                                 if (o.set)\r
24                                         o.content = t['_' + dialect + '_bbcode2html'](o.content);\r
25 \r
26                                 if (o.get)\r
27                                         o.content = t['_' + dialect + '_html2bbcode'](o.content);\r
28                         });\r
29                 },\r
30 \r
31                 getInfo : function() {\r
32                         return {\r
33                                 longname : 'BBCode Plugin',\r
34                                 author : 'Moxiecode Systems AB',\r
35                                 authorurl : 'http://tinymce.moxiecode.com',\r
36                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',\r
37                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
38                         };\r
39                 },\r
40 \r
41                 // Private methods\r
42 \r
43                 // HTML -> BBCode in DFRN dialect
44                 _dfrn_html2bbcode : function(s) {
45                         s = tinymce.trim(s);\r
46 \r
47                         function rep(re, str) {\r
48                                 s = s.replace(re, str);\r
49                         };\r
50 \r
51
52                         function get(re) {
53                                 return s.match(re);
54                         }
55
56
57                         /* oembed */
58                         function _h2b_cb(match) {
59                                 /*
60                                 function s_h2b(data) {
61                                                 match = data;
62                                 }
63                                 $.ajax({
64                                         type:"POST",
65                                         url: 'oembed/h2b',
66                                         data: {text: match},
67                                         async: false,
68                                         success: s_h2b,
69                                         dataType: 'html'
70                                 });
71                                 */
72                                 
73                                 var f, g, tof = [], tor = [];
74                                 var find_spanc = /<span [^>]*class *= *[\"'](?:[^\"']* )*oembed(?: [^\"']*)*[\"'][^>]*>(.*?(?:<span[^>]*>(.*?)<\/span *>)*.*?)<\/span *>/ig;
75                                 while (f = find_spanc.exec(match)) {
76                                         var find_a = /<a([^>]* rel=[\"']oembed[\"'][^>]*)>.*?<\/a *>/ig;
77                                         if (g = find_a.exec(f[1])) {
78                                                 var find_href = /href=[\"']([^\"']*)[\"']/ig;
79                                                 var m2 = find_href.exec(g[1]);
80                                                 if (m2[1]) {
81                                                         tof.push(f[0]);
82                                                         tor.push("[EMBED]" + m2[1] + "[/EMBED]");
83                                                 }
84                                         }
85                                 }
86                                 for (var i = 0; i < tof.length; i++) match = match.replace(tof[i], tor[i]);
87                                 
88                                 return match;
89                         }
90
91                         if (s.indexOf('class="oembed')>=0){
92                                 //alert("request oembed html2bbcode");
93                                 s = _h2b_cb(s);
94                         }
95                         
96                         /* /oembed */
97
98
99                         // Preserve HTML tags inside code blocks
100                         var codes = get(/<code>(.*?)<\/code>/gi);
101                         rep(/<code>(.*?)<\/code>/gi,"[$!$!CODEBLOCK!$!$]");
102
103                         // example: <strong> to [b]\r
104                         rep(/<a class=\"bookmark\" href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[bookmark=$1]$2[/bookmark]");
105                         rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");\r
106                         rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");\r
107                         rep(/<span style=\"color:(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
108                         rep(/<font>(.*?)<\/font>/gi,"$1");\r
109
110                         // Use [^>]* instead of .* to prevent a match against two separate <img> tags
111                         rep(/<img[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$1x$2]$3[/img]");
112                         rep(/<img[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$1]$3[/img]");
113                         rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$3x$2]$1[/img]");
114                         rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?width=\"([^>]*?)\"[^>]*?height=\"([^>]*?)\"[^>]*?\/>/gi,"[img=$2x$3]$1[/img]");
115                         rep(/<img[^>]*?src=\"([^>]*?)\"[^>]*?\/>/gi,"[img]$1[/img]");
116
117                         rep(/<ul class=\"listbullet\" style=\"list-style-type\: circle\;\">(.*?)<\/ul>/gi,"[list]$1[/list]");
118                         rep(/<ul class=\"listnone\" style=\"list-style-type\: none\;\">(.*?)<\/ul>/gi,"[list=]$1[/list]");
119                         rep(/<ul class=\"listdecimal\" style=\"list-style-type\: decimal\;\">(.*?)<\/ul>/gi,"[list=1]$1[/list]");
120                         rep(/<ul class=\"listlowerroman\" style=\"list-style-type\: lower-roman\;\">(.*?)<\/ul>/gi,"[list=i]$1[/list]");
121                         rep(/<ul class=\"listupperroman\" style=\"list-style-type\: upper-roman\;\">(.*?)<\/ul>/gi,"[list=I]$1[/list]");
122                         rep(/<ul class=\"listloweralpha\" style=\"list-style-type\: lower-alpha\;\">(.*?)<\/ul>/gi,"[list=a]$1[/list]");
123                         rep(/<ul class=\"listupperalpha\" style=\"list-style-type\: upper-alpha\;\">(.*?)<\/ul>/gi,"[list=A]$1[/list]");
124                         rep(/<li>(.*?)<\/li>/gi,'[li]$1[/li]');
125
126                         //rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]");
127                         rep(/<\/(strong|b)>/gi,"[/b]");\r
128                         rep(/<(strong|b)>/gi,"[b]");\r
129                         rep(/<\/(em|i)>/gi,"[/i]");\r
130                         rep(/<(em|i)>/gi,"[i]");\r
131                         rep(/<\/u>/gi,"[/u]");\r
132                         rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");\r
133                         rep(/<u>/gi,"[u]");\r
134                         rep(/<blockquote[^>]*>/gi,"[quote]");\r
135                         rep(/<\/blockquote>/gi,"[/quote]");\r
136                         rep(/<hr \/>/gi,"[hr]");
137                         rep(/<br (.*?)\/>/gi,"\n");
138                         rep(/<br \/>/gi,"\n");\r
139                         rep(/<br>/gi,"\n");\r
140                         rep(/<p>/gi,"");\r
141                         rep(/<\/p>/gi,"\n");\r
142                         rep(/&nbsp;|\u00a0/gi," ");\r
143                         rep(/&quot;/gi,"\"");\r
144                         rep(/&lt;/gi,"<");\r
145                         rep(/&gt;/gi,">");\r
146                         rep(/&amp;/gi,"&");\r
147 \r
148                         // Hack to fix an annoying bug of TinyMCE where block formats don't
149                         // work when forced_root_block = ''. So set forced_root_block = 'div'
150                         // and then strip out the divs manually
151                         rep(/<div>/gi,"");
152                         rep(/<\/div>/gi,"");
153
154                         if(codes != null) {                     
155                                 for(var i=0; i<codes.length; i++) {
156                                         codes[i] = codes[i].replace("<code>","");
157                                         codes[i] = codes[i].replace("</code>","");
158                                         rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"[code]"+codes[i]+"[/code]");
159                                 }
160                         }
161
162                         return s; \r
163                 },\r
164 \r
165                 // BBCode -> HTML from DFRN dialect
166                 _dfrn_bbcode2html : function(s) {
167                         s = tinymce.trim(s);\r
168 \r
169                         function rep(re, str) {\r
170                                 s = s.replace(re, str);\r
171                         };\r
172 \r
173
174
175                         function get(re) {
176                                 return s.match(re);
177                         }
178
179
180
181                         // Preserve HTML tags inside code blocks
182                         var codes = get(/\[code\](.*?)\[\/code\]/gi);
183                         rep(/\[code\](.*?)\[\/code\]/gi,"[$!$!CODEBLOCK!$!$]");
184
185                         // example: [b] to <strong>\r
186                         rep(/\n/gi,"<br />");\r
187                         rep(/\[b\]/gi,"<strong>");\r
188                         rep(/\[\/b\]/gi,"</strong>");\r
189                         rep(/\[i\]/gi,"<em>");\r
190                         rep(/\[\/i\]/gi,"</em>");\r
191                         rep(/\[u\]/gi,"<u>");\r
192                         rep(/\[\/u\]/gi,"</u>");\r
193                         rep(/\[hr\]/gi,"<hr />");
194                         rep(/\[bookmark=([^\]]+)\](.*?)\[\/bookmark\]/gi,"<a class=\"bookmark\" href=\"$1\">$2</a>");
195                         rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");\r
196                         rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");\r
197                         rep(/\[img=(.*?)x(.*?)\](.*?)\[\/img\]/gi,"<img width=\"$1\" height=\"$2\" src=\"$3\" />");
198                         rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");\r
199 \r
200                         rep(/\[list\](.*?)\[\/list\]/gi, '<ul class="listbullet" style="list-style-type: circle;">$1</ul>');
201                         rep(/\[list=\](.*?)\[\/list\]/gi, '<ul class="listnone" style="list-style-type: none;">$1</ul>');
202                         rep(/\[list=1\](.*?)\[\/list\]/gi, '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>');
203                         rep(/\[list=i\](.*?)\[\/list\]/gi,'<ul class="listlowerroman" style="list-style-type: lower-roman;">$1</ul>');
204                         rep(/\[list=I\](.*?)\[\/list\]/gi, '<ul class="listupperroman" style="list-style-type: upper-roman;">$1</ul>');
205                         rep(/\[list=a\](.*?)\[\/list\]/gi, '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$1</ul>');
206                         rep(/\[list=A\](.*?)\[\/list\]/gi, '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$1</ul>');
207                         rep(/\[li\](.*?)\[\/li\]/gi, '<li>$1</li>');
208                         rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<span style=\"color: $1;\">$2</span>");
209                         rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>");
210                         //rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");
211                         rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");
212
213                         /* oembed */
214                         function _b2h_cb(match, url) {
215                                 url = bin2hex(url);
216                                 function s_b2h(data) {
217                                                 match = data;
218                                 }
219                                 $.ajax({
220                                         url: 'oembed/b2h?url=' + url,
221                                         async: false,
222                                         success: s_b2h,
223                                         dataType: 'html'
224                                 });
225                                 return match;
226                         }
227
228                         s = s.replace(/\[embed\](.*?)\[\/embed\]/gi, _b2h_cb);
229                         
230                         /* /oembed */
231
232                         if(codes != null) {                     
233                                 for(var i=0; i<codes.length; i++) {
234                                         codes[i] = codes[i].replace("[code]","");
235                                         codes[i] = codes[i].replace("[/code]","");
236                                         rep(/\[\$!\$!CODEBLOCK!\$!\$\]/i,"<code>"+codes[i]+"</code>");
237                                 }
238                         }
239
240                         return s; \r
241                 }\r
242         });\r
243 \r
244         // Register plugin\r
245         tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);\r
246 })();