DirectionDetector is a JQuery plugin witch can set dir-attribute/style-attribute/class-attribute for elements with RTL content. so it can be used in multi language web applications. for example Arabic is a RTL language, it can found elements with Arabic content and change direction of element(s) to RTL using dir/style/class.
Direction detector is available under GPL license version 3. you can download it here.
DirectionDetector walks on html tree looking for text nodes, then if beginning of string was a RTL language it will set passed arguments to element, so elements with RTL content could have specific style.
$('p.example1').setDirections();
پارسی
as you can see plugin will not touch elements with LTR content
from end:
$('p.example2').setDirections(
{
direction:'rtl',
textAlign:'right'
}, true
);
as start point is end of element so this text will not effect and cheching will begin from last text node witch is after <br/> element - we have 3 nodes here: 1. the text you are reading 2. <br/> element 3. Arabic text
العربية
nested check
$('p.example3').setDirections('rtl-content', false, true)
עִבְרִית
now it checks children too. as we putted some Hebrew content in <a> this paragraph will have rtl-class witch is defined in header
ignore characters
$('p.example4').setDirections(
'rtl-content',
false,
false,
'@: \n\r\t'+String.fromCharCode(9851)
)
♻: @twitterLike @reply نمونهی شبیه twitter
Custom function
$('.example5').setdirections(
function(el, isRtl, foundString){
if(isRtl==true)
$(el).css({
'background-color':'yellow',
'direction': 'rtl'}
);
else
$(el).css('background-color', 'orange');
},
false,
false,
''
);
نمونهی راست به چپ
a left to right example