]> git.mxchange.org Git - friendica.git/blob - library/tinymce/jscripts/tiny_mce/plugins/autolink/editor_plugin_src.js
update tinymce to 3.5b2 to fix issues with FF 11 and pasting into code blocks
[friendica.git] / library / tinymce / jscripts / tiny_mce / plugins / autolink / editor_plugin_src.js
1 /**\r
2  * editor_plugin_src.js\r
3  *\r
4  * Copyright 2011, 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 (function() {\r
12         tinymce.create('tinymce.plugins.AutolinkPlugin', {\r
13         /**\r
14         * Initializes the plugin, this will be executed after the plugin has been created.\r
15         * This call is done before the editor instance has finished it's initialization so use the onInit event\r
16         * of the editor instance to intercept that event.\r
17         *\r
18         * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.\r
19         * @param {string} url Absolute URL to where the plugin is located.\r
20         */\r
21 \r
22         init : function(ed, url) {\r
23                 var t = this;\r
24 \r
25                 // Internet Explorer has built-in automatic linking\r
26                 if (tinyMCE.isIE)\r
27                         return;\r
28 \r
29                 // Add a key down handler\r
30                 ed.onKeyDown.add(function(ed, e) {\r
31                         if (e.keyCode == 13)\r
32                                 return t.handleEnter(ed);\r
33                         });\r
34 \r
35                 ed.onKeyPress.add(function(ed, e) {\r
36                         if (e.which == 41)\r
37                                 return t.handleEclipse(ed);\r
38                 });\r
39 \r
40                 // Add a key up handler\r
41                 ed.onKeyUp.add(function(ed, e) {\r
42                         if (e.keyCode == 32)\r
43                                 return t.handleSpacebar(ed);\r
44                         });\r
45                },\r
46 \r
47                 handleEclipse : function(ed) {\r
48                         this.parseCurrentLine(ed, -1, '(', true);\r
49                 },\r
50 \r
51                 handleSpacebar : function(ed) {\r
52                          this.parseCurrentLine(ed, 0, '', true);\r
53                  },\r
54 \r
55                 handleEnter : function(ed) {\r
56                         this.parseCurrentLine(ed, -1, '', false);\r
57                 },\r
58 \r
59                 parseCurrentLine : function(ed, end_offset, delimiter, goback) {\r
60                         var r, end, start, endContainer, bookmark, text, matches, prev, len;\r
61 \r
62                         // We need at least five characters to form a URL,\r
63                         // hence, at minimum, five characters from the beginning of the line.\r
64                         r = ed.selection.getRng().cloneRange();\r
65                         if (r.startOffset < 5) {\r
66                                 // During testing, the caret is placed inbetween two text nodes. \r
67                                 // The previous text node contains the URL.\r
68                                 prev = r.endContainer.previousSibling;\r
69                                 if (prev == null) {\r
70                                         if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)\r
71                                                 return;\r
72 \r
73                                         prev = r.endContainer.firstChild.nextSibling;\r
74                                 }\r
75                                 len = prev.length;\r
76                                 r.setStart(prev, len);\r
77                                 r.setEnd(prev, len);\r
78 \r
79                                 if (r.endOffset < 5)\r
80                                         return;\r
81 \r
82                                 end = r.endOffset;\r
83                                 endContainer = prev;\r
84                         } else {\r
85                                 endContainer = r.endContainer;\r
86 \r
87                                 // Get a text node\r
88                                 if (endContainer.nodeType != 3 && endContainer.firstChild) {\r
89                                         while (endContainer.nodeType != 3 && endContainer.firstChild)\r
90                                                 endContainer = endContainer.firstChild;\r
91 \r
92                                         r.setStart(endContainer, 0);\r
93                                         r.setEnd(endContainer, endContainer.nodeValue.length);\r
94                                 }\r
95 \r
96                                 if (r.endOffset == 1)\r
97                                         end = 2;\r
98                                 else\r
99                                         end = r.endOffset - 1 - end_offset;\r
100                         }\r
101 \r
102                         start = end;\r
103 \r
104                         do\r
105                         {\r
106                                 // Move the selection one character backwards.\r
107                                 r.setStart(endContainer, end - 2);\r
108                                 r.setEnd(endContainer, end - 1);\r
109                                 end -= 1;\r
110 \r
111                                 // Loop until one of the following is found: a blank space, &nbsp;, delimeter, (end-2) >= 0\r
112                         } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);\r
113 \r
114                         if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {\r
115                                 r.setStart(endContainer, end);\r
116                                 r.setEnd(endContainer, start);\r
117                                 end += 1;\r
118                         } else if (r.startOffset == 0) {\r
119                                 r.setStart(endContainer, 0);\r
120                                 r.setEnd(endContainer, start);\r
121                         }\r
122                         else {\r
123                                 r.setStart(endContainer, end);\r
124                                 r.setEnd(endContainer, start);\r
125                         }\r
126 \r
127                         text = r.toString();\r
128                         matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|[A-Z0-9._%+-]+@)(.+)$/i);\r
129 \r
130                         if (matches) {\r
131                                 if (matches[1] == 'www.') {\r
132                                         matches[1] = 'http://www.';\r
133                                 } else if (/@$/.test(matches[1])) {\r
134                                         matches[1] = 'mailto:' + matches[1];\r
135                                 }\r
136 \r
137                                 bookmark = ed.selection.getBookmark();\r
138 \r
139                                 ed.selection.setRng(r);\r
140                                 tinyMCE.execCommand('createlink',false, matches[1] + matches[2]);\r
141                                 ed.selection.moveToBookmark(bookmark);\r
142 \r
143                                 // TODO: Determine if this is still needed.\r
144                                 if (tinyMCE.isWebKit) {\r
145                                         // move the caret to its original position\r
146                                         ed.selection.collapse(false);\r
147                                         var max = Math.min(endContainer.length, start + 1);\r
148                                         r.setStart(endContainer, max);\r
149                                         r.setEnd(endContainer, max);\r
150                                         ed.selection.setRng(r);\r
151                                 }\r
152                         }\r
153                 },\r
154 \r
155                 /**\r
156                 * Returns information about the plugin as a name/value array.\r
157                 * The current keys are longname, author, authorurl, infourl and version.\r
158                 *\r
159                 * @return {Object} Name/value array containing information about the plugin.\r
160                 */\r
161                 getInfo : function() {\r
162                         return {\r
163                                 longname : 'Autolink',\r
164                                 author : 'Moxiecode Systems AB',\r
165                                 authorurl : 'http://tinymce.moxiecode.com',\r
166                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',\r
167                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
168                         };\r
169                 }\r
170         });\r
171 \r
172         // Register plugin\r
173         tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);\r
174 })();\r