4 * Copyright 2011, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 tinymce.create('tinymce.plugins.AutolinkPlugin', {
14 * Initializes the plugin, this will be executed after the plugin has been created.
15 * This call is done before the editor instance has finished it's initialization so use the onInit event
16 * of the editor instance to intercept that event.
18 * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
19 * @param {string} url Absolute URL to where the plugin is located.
22 init : function(ed, url) {
25 // Internet Explorer has built-in automatic linking
29 // Add a key down handler
30 ed.onKeyDown.add(function(ed, e) {
32 return t.handleEnter(ed);
35 ed.onKeyPress.add(function(ed, e) {
37 return t.handleEclipse(ed);
40 // Add a key up handler
41 ed.onKeyUp.add(function(ed, e) {
43 return t.handleSpacebar(ed);
47 handleEclipse : function(ed) {
48 this.parseCurrentLine(ed, -1, '(', true);
51 handleSpacebar : function(ed) {
52 this.parseCurrentLine(ed, 0, '', true);
55 handleEnter : function(ed) {
56 this.parseCurrentLine(ed, -1, '', false);
59 parseCurrentLine : function(ed, end_offset, delimiter, goback) {
60 var r, end, start, endContainer, bookmark, text, matches, prev, len;
62 // We need at least five characters to form a URL,
63 // hence, at minimum, five characters from the beginning of the line.
64 r = ed.selection.getRng().cloneRange();
65 if (r.startOffset < 5) {
66 // During testing, the caret is placed inbetween two text nodes.
67 // The previous text node contains the URL.
68 prev = r.endContainer.previousSibling;
70 if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)
73 prev = r.endContainer.firstChild.nextSibling;
76 r.setStart(prev, len);
85 endContainer = r.endContainer;
88 if (endContainer.nodeType != 3 && endContainer.firstChild) {
89 while (endContainer.nodeType != 3 && endContainer.firstChild)
90 endContainer = endContainer.firstChild;
92 r.setStart(endContainer, 0);
93 r.setEnd(endContainer, endContainer.nodeValue.length);
99 end = r.endOffset - 1 - end_offset;
106 // Move the selection one character backwards.
107 r.setStart(endContainer, end - 2);
108 r.setEnd(endContainer, end - 1);
111 // Loop until one of the following is found: a blank space, , delimeter, (end-2) >= 0
112 } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
114 if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
115 r.setStart(endContainer, end);
116 r.setEnd(endContainer, start);
118 } else if (r.startOffset == 0) {
119 r.setStart(endContainer, 0);
120 r.setEnd(endContainer, start);
123 r.setStart(endContainer, end);
124 r.setEnd(endContainer, start);
128 matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)(.+)$/i);
131 if (matches[1] == 'www.') {
132 matches[1] = 'http://www.';
135 bookmark = ed.selection.getBookmark();
137 ed.selection.setRng(r);
138 tinyMCE.execCommand('mceInsertLink',false, matches[1] + matches[2]);
139 ed.selection.moveToBookmark(bookmark);
141 // TODO: Determine if this is still needed.
142 if (tinyMCE.isWebKit) {
143 // move the caret to its original position
144 ed.selection.collapse(false);
145 var max = Math.min(endContainer.length, start + 1);
146 r.setStart(endContainer, max);
147 r.setEnd(endContainer, max);
148 ed.selection.setRng(r);
154 * Returns information about the plugin as a name/value array.
155 * The current keys are longname, author, authorurl, infourl and version.
157 * @return {Object} Name/value array containing information about the plugin.
159 getInfo : function() {
161 longname : 'Autolink',
162 author : 'Moxiecode Systems AB',
163 authorurl : 'http://tinymce.moxiecode.com',
164 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
165 version : tinymce.majorVersion + "." + tinymce.minorVersion
171 tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);