MediaWiki:Gadget-DetectDir.js
Appearance
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// This is the function that does all the hard work.
// Just pass a form element element (or array of form elements) to the
// i18n.addElements method.
// The idea and regular expressions originally located within the goog.i18n.bidi
// Javascript library (http://code.google.com/apis/gadgets/docs/i18n.html) and I
// take no credit what-so-ever for the idea.
var i18n = (function i18n() {
var ltrChars = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF',
rtlChars = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC',
ltrDirCheckRe = new RegExp('^[^'+rtlChars+']*['+ltrChars+']'),
rtlDirCheckRe = new RegExp('^[^'+ltrChars+']*['+rtlChars+']');
// Inbuilt addEvent function
function addEvent(obj, type, fn, tmp) {
tmp || (tmp = true);
if( obj.attachEvent ) {
obj["e"+type+fn] = fn;
obj[type+fn] = function(){obj["e"+type+fn]( window.event );};
obj.attachEvent( "on"+type, obj[type+fn] );
} else {
obj.addEventListener( type, fn, true );
};
};
function addElemEvents(elem) {
addEvent(elem, "keydown", check);
addEvent(elem, "keyup", check);
checkDirection(elem);
};
function check(e) {
checkDirection(this);
};
function checkDirection(elem) {
var text = elem.value;
elem.dir = isRtlText(text) ? 'rtl' : (isLtrText(text) ? 'ltr' : '');
};
function isRtlText(text) {
return rtlDirCheckRe.test(text);
};
function isLtrText(text) {
return ltrDirCheckRe.test(text);
};
return {
addElements: function(elems) {
if(!elems.length) elems = [elems];
for(var i = 0, elem; elem = elems[i]; i++) {
addElemEvents(elem);
};
}
};
})();
window.onload = function(e) {
i18n.addElements([document.getElementById('wpTextbox1'), document.getElementById('searchInput')]);
};