public
Description: a jQuery plugin to get and set the caret/selection
Homepage: http://labs.0xab.cd/jquery/fieldselection/0.2.3-test/
Clone URL: git://github.com/localhost/jquery-fieldselection.git
jquery-fieldselection / test.html
100644 217 lines (172 sloc) 5.81 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>jQuery - fieldSelection</title>
<meta name="author" content="Alex Brem" />
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery-fieldselection.js"></script>
<script type="text/javascript"><!--//--><![CDATA[//><!--
 
$(document).ready(function(){
 
// install the event handler for #debug #output
$('input[@type="text"], textarea').keyup(update).mousedown(update).mousemove(update).mouseup(update);
 
// assign the field1 paste event
$('.replace').click(function(e){
$('#field1').replaceSelection($(this).title());
e.preventDefault();
});
 
});
 
/*
this function handles #debug #output
*/
 
function update(e) {
 
// here we fetch our text range object
var range = $(this).getSelection();
 
// just dump the values
$('#output').html(
"hexdump:\n" + hexdump(this.value, range.start, (range.end != range.start) ? range.end - 1 : range.end) + "\n\n" +
"id: " + this.id + "\n" +
"start: " + range.start + "\n" +
"length: " + range.length + "\n" +
        "end: " + range.end + "\n" +
        ((typeof range['row'] != 'undefined') ? "caret row: " + range.row + "\n" : '') +
        ((typeof range['col'] != 'undefined') ? "caret col: " + range.col + "\n" : '') +
        "selected text:\n<span class=\"txt\">" + (($('#ws').get(0).checked) ? range.text.whitespace() : range.text) + "</span>\n\n"
);
 
}
 
/*
this code block is not needed for jQuery fieldSelection
this is just to visualize white space in #debug #output
*/
 
String.prototype.whitespace = (function() {
 
if (!RegExp.escape) {
RegExp.escape = (function() { // RegExp.escape by Simon Wilson & Mark Wubben
var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ];
var sRE = new RegExp( '(\\' + specials.join('|\\') + ')', 'g' );
return function(text) { return text.replace(sRE, '\\$1') }
})();
 
}
 
var ws = { "\r\n": "¶", "\n": "¶", "\r": "¶", "\t": "&raquo;", " ": "&middot;" };
 
return ($.browser.msie) ? function() {
// &#8203; to break up our whitespaces doesn't work in IE (it prints a block)
// but nevertheless he doesn't need it, because he has "word-wrap:break-word;"
// and therefore he gets his own function
var s = this;
$.each(ws, function(i){ s = s.replace(new RegExp(RegExp.escape(i), 'g'), this) });
return s;
} : function () {
var s = this;
$.each(ws, function(i){ s = s.replace(new RegExp(RegExp.escape(i), 'g'), this + "\u200b") });
return s;
}
 
})();
 
function hexdump(txt, hi_f, hi_t) {
var hex = '', tmp;
 
if (txt) {
 
for (var i = 0, j = txt.length; i <= j; i++) {
 
tmp = txt.charCodeAt(i).toString(16);
 
if (i == hi_f)
hex += '<span class="hi">';
 
if (i < txt.length)
hex += ( (tmp.length == 2) ? tmp : '0' + tmp );
else
hex += "&nbsp;&nbsp;";
 
if (i == hi_t)
hex += '</span>';
 
if ((i+1) % 16 == 0)
hex += "\n";
else
hex += ' ';
 
}
 
}
 
return hex;
 
}
 
//--><!]]>
 
</script>
<style type="text/css">
body {
background:#cc9;
color:#653;
font:1em Georgia, "Courier New", serif;
}
 
h1 { color:#430 }
 
h1 .nuf {
background-color:HighlightText;
color:Highlight;
}
 
h1 .fun {
background-color:Highlight;
color:HighlightText;
}
 
a:link { color:#993 }
a:visited { color:#660 }
 
#content { position:relative }
 
#debug {
position:absolute;
top:5%;
left:50%;
width:45%;
min-width:200px;
min-height:50px;
padding:4px;
background:#ee9;
border:1px solid #996;
}
 
#output .txt {
background:#f99;
display:inline;
color:black;
}
 
   #output .hi {
   background:#cc9;
   color:#630;
   }
 
pre {
white-space:pre-wrap;
white-space:-moz-pre-wrap;
white-space:-pre-wrap;
white-space:-o-pre-wrap;
word-wrap:break-word;
}
 
label {
color:#663;
font-size:80%;
}
 
input[type='text'], textarea {
display:block;
width:300px;
margin-bottom:8px;
}
 
/* bastard. */
* html input, * html textarea { display:block; }
 
</style>
</head>
<body>
<h1>jQuery plugin: <span class="nuf">field</span><span class="fun">Selection</span></h1>
<p>
version <strong>0.1.1</strong> &ndash; 2006-12-16<br />
source: <a href="jquery-fieldselection.js">jquery-fieldselection.js</a> &middot; <a href="jquery-fieldselection.pack.js">jquery-fieldselection.pack.js</a><br />
this test: <a href="test.html">test.html</a> (tested with <a href="http://jQuery.com/">jQuery</a> 1.0.2+)
</p>
<p>Read more about the subject in <a href="http://blog.0xab.cd/2006/12/16/jquery-plugin-fieldselection/">my related blog entry</a>.</p>
<hr />
<div id="content">
<div id="debug">
<input id="ws" name="ws" type="checkbox" /><label>show whitespace?</label>
<pre id="output"></pre>
</div>
<p>
<a class="replace" href="#" title="foo">replace selection in #field1</a>
</p>
<fieldset>
<label for="field1">field1:</label><input id="field1" name="field1" type="text" value="this is an input field" />
<label for="area1">area1:</label><textarea id="area1" name="area1">textarea: foo bar baz</textarea>
<label for="field2">field2:</label><input id="field2" name="field2" type="text" value="this is another input field" />
<label for="area2">area2</label><textarea id="area2" name="area1">this is a multi-
line-
break test</textarea>
</fieldset>
</div>
</body>
</html>