Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Added LTR text direction support to languageDirAttributeIsUsed. #72
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Miller committed Jan 4, 2014
1 parent b23554e commit d2c4677
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ var quail = {
focusElements : 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]',

/**
* Characters that are right-to-left only.
* Regex to find right-to-left or left-to-right characters
*/
rtlCharacters : /[\u0600-\u06FF]|[\u0750-\u077F]|[\u0590-\u05FF]|[\uFE70-\uFEFF]/mg,
textDirection : {
rtl : /[\u0600-\u06FF]|[\u0750-\u077F]|[\u0590-\u05FF]|[\uFE70-\uFEFF]/mg,
ltr : /[\u0041-\u007A]|[\u00C0-\u02AF]|[\u0388-\u058F]/mg
},

/**
* Main run function for quail. It bundles up some accessibility tests,
Expand Down
13 changes: 10 additions & 3 deletions src/js/custom/languageDirAttributeIsUsed.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
quail.languageDirAttributeIsUsed = function() {
var currentDirection = (quail.html.attr('dir')) ? quail.html.attr('dir').toLowerCase() : 'ltr';
var oppositeDirection = (currentDirection === 'ltr') ? 'rtl' : 'ltr';
quail.html.find(quail.textSelector).each(function() {
var matches = $(this).text().match(quail.rtlCharacters);
currentDirection = ($(this).attr('dir')) ? $(this).attr('dir').toLowerCase() : currentDirection;
if(typeof quail.textDirection[currentDirection] === 'undefined') {
currentDirection = 'ltr';
}
oppositeDirection = (currentDirection === 'ltr') ? 'rtl' : 'ltr';
var matches = $(this).text().match(quail.textDirection[currentDirection]);
if(!matches) {
return;
}
matches = matches.length;
$(this).find('[dir=rtl]').each(function() {
var childMatches = $(this).text().match(quail.rtlCharacters);
$(this).find('[dir=' + oppositeDirection + ']').each(function() {
var childMatches = $(this).text().match(quail.textDirection[currentDirection]);
if(childMatches) {
matches = matches - childMatches.length;
}
Expand Down
2 changes: 2 additions & 0 deletions test/quail.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,10 @@
"testfiles/common/labelMustNotBeEmpty-pass-2.html",
"testfiles/common/labelMustNotBeEmpty-pass.html",
"testfiles/common/languageDirAttributeIsUsed-pass.html",
"testfiles/common/languageDirAttributeIsUsed-pass-2.html",
"testfiles/common/languageDirAttributeIsUsed-fail.html",
"testfiles/common/languageDirAttributeIsUsed-fail-2.html",
"testfiles/common/languageDirAttributeIsUsed-fail-3.html",
"testfiles/common/legendDescribesListOfChoices-fail-2.html",
"testfiles/common/legendDescribesListOfChoices-fail.html",
"testfiles/common/legendTextNotEmpty-fail.html",
Expand Down
28 changes: 28 additions & 0 deletions test/testfiles/common/languageDirAttributeIsUsed-fail-3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!doctype html>
<html>

<head>
<title>languageDirAttributeIsUsed-fail-3</title>
<link rel="stylesheet" href="../../../lib/qunit/qunit/qunit.css" media="screen">
</head>

<body>
<div id="quail-scope" dir="rtl">
<p>
וואַכטל זענען זייער "some english text" קיוט איך טראַכטן.
</p>
</div>
<script id="qunit-jquery" src="../../../lib/jquery/jquery.js"></script>
<script id="qunit-quail" src="../../../dist/quail.jquery.js"></script>
<script id="qunit-composite" src="../../composite.js"></script>
<script id="qunit-qunit" src="../../../lib/qunit/qunit/qunit.js"></script>
<script id="qunit-script">
test('languageDirAttributeIsUsed', function() {
quailTest.runTest( 'languageDirAttributeIsUsed' );
equal(true, quailTest.confirmIsTag('p'), 'No errors were found');
});
</script>
</body>

</html>
28 changes: 28 additions & 0 deletions test/testfiles/common/languageDirAttributeIsUsed-pass-2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!doctype html>
<html>

<head>
<title>languageDirAttributeIsUsed-pass-2</title>
<link rel="stylesheet" href="../../../lib/qunit/qunit/qunit.css" media="screen">
</head>

<body>
<div id="quail-scope" dir="rtl">
<p>
וואַכטל זענען זייער "<span lang="en" dir="ltr">some english text</span>" קיוט איך טראַכטן.
</p>
</div>
<script id="qunit-jquery" src="../../../lib/jquery/jquery.js"></script>
<script id="qunit-quail" src="../../../dist/quail.jquery.js"></script>
<script id="qunit-composite" src="../../composite.js"></script>
<script id="qunit-qunit" src="../../../lib/qunit/qunit/qunit.js"></script>
<script id="qunit-script">
test('languageDirAttributeIsUsed', function() {
quailTest.runTest( 'languageDirAttributeIsUsed' );
equal(true, quailTest.confirmIsEmpty(), 'No errors were found');
});
</script>
</body>

</html>

0 comments on commit d2c4677

Please sign in to comment.