Permalink
Browse files

initial commit.

  • Loading branch information...
garyharan committed Nov 13, 2009
0 parents commit 3eda83804e739b4ba62813b9304a70bf0eb911a0
Showing with 5,603 additions and 0 deletions.
  1. +92 −0 jquery.caretPosition.js
  2. +64 −0 jquery_caretPosition_test.html
  3. +4,376 −0 lib/jquery.js
  4. +1,017 −0 lib/jsunittest.js
  5. +54 −0 lib/unittest.css
@@ -0,0 +1,92 @@
+(function($){
+ $.fn.insertAtCaret = function(text, opts) {
+ var element = $(this).get(0);
+
+ if (document.selection) {
+ element.focus();
+ var orig = element.value.replace(/\r\n/g, "\n");
+ var range = document.selection.createRange();
+
+ if (range.parentElement() != element) {
+ return false;
+ }
+
+ range.text = text;
+
+ var actual = tmp = element.value.replace(/\r\n/g, "\n");
+
+ for (var diff = 0; diff < orig.length; diff++) {
+ if (orig.charAt(diff) != actual.charAt(diff)) break;
+ }
+
+ for (var index = 0, start = 0; tmp.match(text) && (tmp = tmp.replace(text, "")) && index <= diff; index = start + text.length ) {
+ start = actual.indexOf(text, index);
+ }
+ } else if (element.selectionStart) {
+ var start = element.selectionStart;
+ var end = element.selectionEnd;
+
+ element.value = element.value.substr(0, start) + text + element.value.substr(end, element.value.length);
+ }
+
+ if (start != null) {
+ setCaretTo(element, start + text.length);
+ } else {
+ element.value += text;
+ }
+
+ return this;
+ }
+
+ $.fn.setCaretPosition = function(start, end) {
+ var element = $(this).get(0);
+ element.focus();
+ setCaretTo(element, start, end);
+ return this;
+ }
+
+
+ $.fn.getCaretPosition = function() {
+ var element = $(this).get(0);
+ $(element).focus();
+ return getCaretPosition(element);
+ }
+
+ // privates
+ function setCaretTo(element, start, end) {
+ if(element.createTextRange) {
+ var range = element.createTextRange();
+ range.moveStart('character', start);
+ range.moveEnd('character', (end || start));
+ range.select();
+ } else if(element.selectionStart) {
+ element.focus();
+ element.setSelectionRange(start, (end || start));
+ }
+ }
+
+ function getCaretPosition(element) {
+ if (typeof element.selectionStart == 'number'){
+ return element.selectionStart;
+ } else if (document.selection) {
+ var range = document.selection.createRange();
+ var rangeLength = range.text.length;
+ range.moveStart('character', -element.value.length);
+ return range.text.length - rangeLength;
+ }
+ }
+
+ // DANGER... this will does not necessarily return text selected within the element you call.
+ $.fn.getSelectedText = function() {
+ var element = $(this).get(0);
+
+ // workaround for firefox because window.getSelection does not work inside inputs
+ if (typeof element.selectionStart == 'number') {
+ return $(element).val().substr(element.selectionStart, element.selectionEnd - element.selectionStart);
+ } else if (document.getSelection) {
+ return document.getSelection();
+ } else if (window.getSelection) {
+ return window.getSelection();
+ }
+ }
+})(jQuery);
@@ -0,0 +1,64 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <title>JavaScript unit test file</title>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <script src="lib/jsunittest.js" type="text/javascript"></script>
+ <script src="lib/jquery.js" type="text/javascript"></script>
+
+ <!-- TODO: REPLACE "example_library_to_test" throughout file with name of file to test -->
+ <script src="jquery.caretPosition.js" type="text/javascript"></script>
+
+ <link rel="stylesheet" href="lib/unittest.css" type="text/css" />
+</head>
+<body>
+
+<div id="content">
+
+ <div id="header">
+ <h1>JavaScript unit test file</h1>
+ <p>
+ This file tests <strong>jquery.caretPosition.js</strong>.
+ </p>
+ </div>
+
+ <!-- Log output (one per Runner, via {testLog: "testlog"} option)-->
+ <div id="testlog"></div>
+
+ <!-- Put sample/test html here -->
+ <div id="sample">
+ </div>
+</div>
+
+<script type="text/javascript">
+// <![CDATA[
+ new Test.Unit.Runner({
+ setup: function(){
+ if ($('#test_input').length == 0){
+ $('<input/>').attr('id', 'test_input').appendTo(document.body).focus().css({'position': 'absolute', 'top': '-1000px'});
+ }
+ },
+
+ testGetAndSetCaretPosition: function(){ with(this) {
+ assertEqual(5, $('#test_input').val('emergency broadcast system').setCaretPosition(5).getCaretPosition());
+ assertEqual(5, $('#test_input').val('emergency broadcast system').setCaretPosition(5, 8).getCaretPosition());
+ }},
+
+ testInsertAtCaret: function() { with(this) {
+ assertEqual('hello', $('#test_input').val('helo').setCaretPosition(2).insertAtCaret('l').val());
+ }},
+
+ testChaining: function() { with(this) {
+ assertEqual('this is a test of the emergency broadcast system',
+ $('#test_input').val('this of the emergency broadcast system').setCaretPosition(5).insertAtCaret('is a test ').setCaretPosition(10, 14).val());
+ }},
+
+ testGetSelectedText: function() { with(this){
+ assertEqual('test', $('#test_input').val('this is a test of that').setCaretPosition(10, 14).getSelectedText());
+ }}
+ });
+// ]]>
+</script>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 3eda838

Please sign in to comment.