From d2c4677ea0f1e9bb815cf2a6481be1ce793398c1 Mon Sep 17 00:00:00 2001 From: Kevin Miller Date: Sat, 4 Jan 2014 11:52:41 -0800 Subject: [PATCH] Added LTR text direction support to languageDirAttributeIsUsed. #72 --- src/js/core.js | 7 +++-- src/js/custom/languageDirAttributeIsUsed.js | 13 +++++++-- test/quail.html | 2 ++ .../languageDirAttributeIsUsed-fail-3.html | 28 +++++++++++++++++++ .../languageDirAttributeIsUsed-pass-2.html | 28 +++++++++++++++++++ 5 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 test/testfiles/common/languageDirAttributeIsUsed-fail-3.html create mode 100644 test/testfiles/common/languageDirAttributeIsUsed-pass-2.html diff --git a/src/js/core.js b/src/js/core.js index 73a85a035..da4a1705a 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -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, diff --git a/src/js/custom/languageDirAttributeIsUsed.js b/src/js/custom/languageDirAttributeIsUsed.js index 3c828df66..81ead2bab 100644 --- a/src/js/custom/languageDirAttributeIsUsed.js +++ b/src/js/custom/languageDirAttributeIsUsed.js @@ -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; } diff --git a/test/quail.html b/test/quail.html index 0b04571f0..f2101197f 100644 --- a/test/quail.html +++ b/test/quail.html @@ -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", diff --git a/test/testfiles/common/languageDirAttributeIsUsed-fail-3.html b/test/testfiles/common/languageDirAttributeIsUsed-fail-3.html new file mode 100644 index 000000000..90d948962 --- /dev/null +++ b/test/testfiles/common/languageDirAttributeIsUsed-fail-3.html @@ -0,0 +1,28 @@ + + + + + + languageDirAttributeIsUsed-fail-3 + + + + +
+

+ וואַכטל זענען זייער "some english text" קיוט איך טראַכטן. +

+
+ + + + + + + + \ No newline at end of file diff --git a/test/testfiles/common/languageDirAttributeIsUsed-pass-2.html b/test/testfiles/common/languageDirAttributeIsUsed-pass-2.html new file mode 100644 index 000000000..0f9dcbc49 --- /dev/null +++ b/test/testfiles/common/languageDirAttributeIsUsed-pass-2.html @@ -0,0 +1,28 @@ + + + + + + languageDirAttributeIsUsed-pass-2 + + + + +
+

+ וואַכטל זענען זייער "some english text" קיוט איך טראַכטן. +

+
+ + + + + + + + \ No newline at end of file