forked from alotaiba/Auto-Input-Text-Direction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-input-text-direction.jquery.js
78 lines (67 loc) · 2.35 KB
/
auto-input-text-direction.jquery.js
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
/**
* Auto Input Text Direction is a little JavaScript function that detects the input language,
* on html forms, and changes the input direction based on the language, i.e., right to left
* for the Arabic language.
* Copyright (C) 2010 Abdulrahman Alotaiba <http://www.mawqey.com/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Converted to a jQuery plugin by Abdullah Diab
* http://mpcabd.xyz
*/
//Arabic - Range:
//0600–06FF
var rtlChars = '\u0600-\u06FF';
//Arabic Supplement - Range:
//0750–077F
rtlChars += '\u0750-\u077F';
//Arabic Presentation Forms-A - Range:
//FB50–FDFF
rtlChars += '\uFB50-\uFDFF';
//Arabic Presentation Forms-B - Range:
//FE70–FEFF
rtlChars += '\uFE70-\uFEFF';
//ASCII Punctuation - Range:
//0000-0020
var controlChars = '\u0000-\u0020';
//General Punctuation - Range
//2000-200D
controlChars += '\u2000-\u200D';
//Start Regular Expression magic
var reRTL = new RegExp('[' + rtlChars + ']', 'g');
var reNotRTL = new RegExp('[^' + rtlChars + controlChars + ']', 'g');
$.fn.automaticDirection = function () {
for (var i = 0; i < this.length; i++) {
var value = $(this[i]).val();
if (value == '')
value = $(this[i]).text();
if (value != '') {
var rtls = value.match(reRTL);
if (rtls != null)
rtls = rtls.length;
else
rtls = 0;
var notrtls = value.match(reNotRTL);
if (notrtls != null)
notrtls = notrtls.length;
else
notrtls = 0;
if (rtls > notrtls)
$(this[i]).css('direction', 'rtl').css('text-align', 'right');
else
$(this[i]).css('direction', 'ltr').css('text-align', 'left');
}
}
};