-/**\r
- * editor_plugin_src.js\r
- *\r
- * Copyright 2011, Moxiecode Systems AB\r
- * Released under LGPL License.\r
- *\r
- * License: http://tinymce.moxiecode.com/license\r
- * Contributing: http://tinymce.moxiecode.com/contributing\r
- */\r
-\r
-(function() {\r
- tinymce.create('tinymce.plugins.AutolinkPlugin', {\r
- /**\r
- * Initializes the plugin, this will be executed after the plugin has been created.\r
- * This call is done before the editor instance has finished it's initialization so use the onInit event\r
- * of the editor instance to intercept that event.\r
- *\r
- * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.\r
- * @param {string} url Absolute URL to where the plugin is located.\r
- */\r
-\r
- init : function(ed, url) {\r
- var t = this;\r
-\r
- // Internet Explorer has built-in automatic linking\r
- if (tinyMCE.isIE)\r
- return;\r
-\r
- // Add a key down handler\r
- ed.onKeyDown.add(function(ed, e) {\r
- if (e.keyCode == 13)\r
- return t.handleEnter(ed);\r
- });\r
-\r
- ed.onKeyPress.add(function(ed, e) {\r
- if (e.which == 41)\r
- return t.handleEclipse(ed);\r
- });\r
-\r
- // Add a key up handler\r
- ed.onKeyUp.add(function(ed, e) {\r
- if (e.keyCode == 32)\r
- return t.handleSpacebar(ed);\r
- });\r
- },\r
-\r
- handleEclipse : function(ed) {\r
- this.parseCurrentLine(ed, -1, '(', true);\r
- },\r
-\r
- handleSpacebar : function(ed) {\r
- this.parseCurrentLine(ed, 0, '', true);\r
- },\r
-\r
- handleEnter : function(ed) {\r
- this.parseCurrentLine(ed, -1, '', false);\r
- },\r
-\r
- parseCurrentLine : function(ed, end_offset, delimiter, goback) {\r
- var r, end, start, endContainer, bookmark, text, matches, prev, len;\r
-\r
- // We need at least five characters to form a URL,\r
- // hence, at minimum, five characters from the beginning of the line.\r
- r = ed.selection.getRng().cloneRange();\r
- if (r.startOffset < 5) {\r
- // During testing, the caret is placed inbetween two text nodes. \r
- // The previous text node contains the URL.\r
- prev = r.endContainer.previousSibling;\r
- if (prev == null) {\r
- if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)\r
- return;\r
-\r
- prev = r.endContainer.firstChild.nextSibling;\r
- }\r
- len = prev.length;\r
- r.setStart(prev, len);\r
- r.setEnd(prev, len);\r
-\r
- if (r.endOffset < 5)\r
- return;\r
-\r
- end = r.endOffset;\r
- endContainer = prev;\r
- } else {\r
- endContainer = r.endContainer;\r
-\r
- // Get a text node\r
- if (endContainer.nodeType != 3 && endContainer.firstChild) {\r
- while (endContainer.nodeType != 3 && endContainer.firstChild)\r
- endContainer = endContainer.firstChild;\r
-\r
- r.setStart(endContainer, 0);\r
- r.setEnd(endContainer, endContainer.nodeValue.length);\r
- }\r
-\r
- if (r.endOffset == 1)\r
- end = 2;\r
- else\r
- end = r.endOffset - 1 - end_offset;\r
- }\r
-\r
- start = end;\r
-\r
- do\r
- {\r
- // Move the selection one character backwards.\r
- r.setStart(endContainer, end - 2);\r
- r.setEnd(endContainer, end - 1);\r
- end -= 1;\r
-\r
- // Loop until one of the following is found: a blank space, , delimeter, (end-2) >= 0\r
- } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);\r
-\r
- if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {\r
- r.setStart(endContainer, end);\r
- r.setEnd(endContainer, start);\r
- end += 1;\r
- } else if (r.startOffset == 0) {\r
- r.setStart(endContainer, 0);\r
- r.setEnd(endContainer, start);\r
- }\r
- else {\r
- r.setStart(endContainer, end);\r
- r.setEnd(endContainer, start);\r
- }\r
-\r
- text = r.toString();\r
- matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|[A-Z0-9._%+-]+@)(.+)$/i);\r
-\r
- if (matches) {\r
- if (matches[1] == 'www.') {\r
- matches[1] = 'http://www.';\r
- } else if (/@$/.test(matches[1])) {\r
- matches[1] = 'mailto:' + matches[1];\r
- }\r
-\r
- bookmark = ed.selection.getBookmark();\r
-\r
- ed.selection.setRng(r);\r
- tinyMCE.execCommand('createlink',false, matches[1] + matches[2]);\r
- ed.selection.moveToBookmark(bookmark);\r
-\r
- // TODO: Determine if this is still needed.\r
- if (tinyMCE.isWebKit) {\r
- // move the caret to its original position\r
- ed.selection.collapse(false);\r
- var max = Math.min(endContainer.length, start + 1);\r
- r.setStart(endContainer, max);\r
- r.setEnd(endContainer, max);\r
- ed.selection.setRng(r);\r
- }\r
- }\r
- },\r
-\r
- /**\r
- * Returns information about the plugin as a name/value array.\r
- * The current keys are longname, author, authorurl, infourl and version.\r
- *\r
- * @return {Object} Name/value array containing information about the plugin.\r
- */\r
- getInfo : function() {\r
- return {\r
- longname : 'Autolink',\r
- author : 'Moxiecode Systems AB',\r
- authorurl : 'http://tinymce.moxiecode.com',\r
- infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',\r
- version : tinymce.majorVersion + "." + tinymce.minorVersion\r
- };\r
- }\r
- });\r
-\r
- // Register plugin\r
- tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);\r
-})();\r
+/**
+ * editor_plugin_src.js
+ *
+ * Copyright 2011, Moxiecode Systems AB
+ * Released under LGPL License.
+ *
+ * License: http://tinymce.moxiecode.com/license
+ * Contributing: http://tinymce.moxiecode.com/contributing
+ */
+
+(function() {
+ tinymce.create('tinymce.plugins.AutolinkPlugin', {
+ /**
+ * Initializes the plugin, this will be executed after the plugin has been created.
+ * This call is done before the editor instance has finished it's initialization so use the onInit event
+ * of the editor instance to intercept that event.
+ *
+ * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
+ * @param {string} url Absolute URL to where the plugin is located.
+ */
+
+ init : function(ed, url) {
+ var t = this;
+
+ // Add a key down handler
+ ed.onKeyDown.addToTop(function(ed, e) {
+ if (e.keyCode == 13)
+ return t.handleEnter(ed);
+ });
+
+ // Internet Explorer has built-in automatic linking for most cases
+ if (tinyMCE.isIE)
+ return;
+
+ ed.onKeyPress.add(function(ed, e) {
+ if (e.which == 41)
+ return t.handleEclipse(ed);
+ });
+
+ // Add a key up handler
+ ed.onKeyUp.add(function(ed, e) {
+ if (e.keyCode == 32)
+ return t.handleSpacebar(ed);
+ });
+ },
+
+ handleEclipse : function(ed) {
+ this.parseCurrentLine(ed, -1, '(', true);
+ },
+
+ handleSpacebar : function(ed) {
+ this.parseCurrentLine(ed, 0, '', true);
+ },
+
+ handleEnter : function(ed) {
+ this.parseCurrentLine(ed, -1, '', false);
+ },
+
+ parseCurrentLine : function(ed, end_offset, delimiter, goback) {
+ var r, end, start, endContainer, bookmark, text, matches, prev, len;
+
+ // We need at least five characters to form a URL,
+ // hence, at minimum, five characters from the beginning of the line.
+ r = ed.selection.getRng(true).cloneRange();
+ if (r.startOffset < 5) {
+ // During testing, the caret is placed inbetween two text nodes.
+ // The previous text node contains the URL.
+ prev = r.endContainer.previousSibling;
+ if (prev == null) {
+ if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)
+ return;
+
+ prev = r.endContainer.firstChild.nextSibling;
+ }
+ len = prev.length;
+ r.setStart(prev, len);
+ r.setEnd(prev, len);
+
+ if (r.endOffset < 5)
+ return;
+
+ end = r.endOffset;
+ endContainer = prev;
+ } else {
+ endContainer = r.endContainer;
+
+ // Get a text node
+ if (endContainer.nodeType != 3 && endContainer.firstChild) {
+ while (endContainer.nodeType != 3 && endContainer.firstChild)
+ endContainer = endContainer.firstChild;
+
+ r.setStart(endContainer, 0);
+ r.setEnd(endContainer, endContainer.nodeValue.length);
+ }
+
+ if (r.endOffset == 1)
+ end = 2;
+ else
+ end = r.endOffset - 1 - end_offset;
+ }
+
+ start = end;
+
+ do
+ {
+ // Move the selection one character backwards.
+ r.setStart(endContainer, end - 2);
+ r.setEnd(endContainer, end - 1);
+ end -= 1;
+
+ // Loop until one of the following is found: a blank space, , delimeter, (end-2) >= 0
+ } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
+
+ if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
+ r.setStart(endContainer, end);
+ r.setEnd(endContainer, start);
+ end += 1;
+ } else if (r.startOffset == 0) {
+ r.setStart(endContainer, 0);
+ r.setEnd(endContainer, start);
+ }
+ else {
+ r.setStart(endContainer, end);
+ r.setEnd(endContainer, start);
+ }
+
+ // Exclude last . from word like "www.site.com."
+ var text = r.toString();
+ if (text.charAt(text.length - 1) == '.') {
+ r.setEnd(endContainer, start - 1);
+ }
+
+ text = r.toString();
+ matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|[A-Z0-9._%+-]+@)(.+)$/i);
+
+ if (matches) {
+ if (matches[1] == 'www.') {
+ matches[1] = 'http://www.';
+ } else if (/@$/.test(matches[1])) {
+ matches[1] = 'mailto:' + matches[1];
+ }
+
+ bookmark = ed.selection.getBookmark();
+
+ ed.selection.setRng(r);
+ tinyMCE.execCommand('createlink',false, matches[1] + matches[2]);
+ ed.selection.moveToBookmark(bookmark);
+
+ // TODO: Determine if this is still needed.
+ if (tinyMCE.isWebKit) {
+ // move the caret to its original position
+ ed.selection.collapse(false);
+ var max = Math.min(endContainer.length, start + 1);
+ r.setStart(endContainer, max);
+ r.setEnd(endContainer, max);
+ ed.selection.setRng(r);
+ }
+ }
+ },
+
+ /**
+ * Returns information about the plugin as a name/value array.
+ * The current keys are longname, author, authorurl, infourl and version.
+ *
+ * @return {Object} Name/value array containing information about the plugin.
+ */
+ getInfo : function() {
+ return {
+ longname : 'Autolink',
+ author : 'Moxiecode Systems AB',
+ authorurl : 'http://tinymce.moxiecode.com',
+ infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
+ version : tinymce.majorVersion + "." + tinymce.minorVersion
+ };
+ }
+ });
+
+ // Register plugin
+ tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);
+})();