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);
33 if (e.shiftKey && e.keyCode == 48)
34 return t.handleEclipse(ed);
37 // Add a key up handler
38 ed.onKeyUp.add(function(ed, e) {
40 return t.handleSpacebar(ed);
44 handleEclipse : function(ed) {
45 this.parseCurrentLine(ed, -1, '(', true);
48 handleSpacebar : function(ed) {
49 this.parseCurrentLine(ed, 0, '', true);
52 handleEnter : function(ed) {
53 this.parseCurrentLine(ed, -1, '', false);
56 parseCurrentLine : function(ed, end_offset, delimiter, goback) {
57 var r, end, start, endContainer, bookmark, text, matches, prev, len;
59 // We need at least five characters to form a URL,
60 // hence, at minimum, five characters from the beginning of the line.
61 r = ed.selection.getRng().cloneRange();
62 if (r.startOffset < 5) {
63 // During testing, the caret is placed inbetween two text nodes.
64 // The previous text node contains the URL.
65 prev = r.endContainer.previousSibling;
67 if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)
70 prev = r.endContainer.firstChild.nextSibling;
73 r.setStart(prev, len);
82 endContainer = r.endContainer;
85 if (endContainer.nodeType != 3 && endContainer.firstChild) {
86 while (endContainer.nodeType != 3 && endContainer.firstChild)
87 endContainer = endContainer.firstChild;
89 r.setStart(endContainer, 0);
90 r.setEnd(endContainer, endContainer.nodeValue.length);
96 end = r.endOffset - 1 - end_offset;
103 // Move the selection one character backwards.
104 r.setStart(endContainer, end - 2);
105 r.setEnd(endContainer, end - 1);
108 // Loop until one of the following is found: a blank space, , delimeter, (end-2) >= 0
109 } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
111 if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
112 r.setStart(endContainer, end);
113 r.setEnd(endContainer, start);
115 } else if (r.startOffset == 0) {
116 r.setStart(endContainer, 0);
117 r.setEnd(endContainer, start);
120 r.setStart(endContainer, end);
121 r.setEnd(endContainer, start);
125 matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)(.+)$/i);
128 if (matches[1] == 'www.') {
129 matches[1] = 'http://www.';
132 bookmark = ed.selection.getBookmark();
134 ed.selection.setRng(r);
135 tinyMCE.execCommand('mceInsertLink',false, matches[1] + matches[2]);
136 ed.selection.moveToBookmark(bookmark);
138 // TODO: Determine if this is still needed.
139 if (tinyMCE.isWebKit) {
140 // move the caret to its original position
141 ed.selection.collapse(false);
142 var max = Math.min(endContainer.length, start + 1);
143 r.setStart(endContainer, max);
144 r.setEnd(endContainer, max);
145 ed.selection.setRng(r);
151 * Returns information about the plugin as a name/value array.
152 * The current keys are longname, author, authorurl, infourl and version.
154 * @return {Object} Name/value array containing information about the plugin.
156 getInfo : function() {
158 longname : 'Autolink',
159 author : 'Moxiecode Systems AB',
160 authorurl : 'http://tinymce.moxiecode.com',
161 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
162 version : tinymce.majorVersion + "." + tinymce.minorVersion
168 tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);