diff --git a/src/js/components/language.js b/src/js/components/language.js index 6a2441dfc..e5ae0e44c 100644 --- a/src/js/components/language.js +++ b/src/js/components/language.js @@ -6,7 +6,7 @@ quail.components.language = { scripts: { basicLatin: { - expression: /[\u0000-\u007F]/g, + regularExpression: /[\u0041-\u007F]/g, languages: [ "ceb", "en", @@ -29,7 +29,7 @@ quail.components.language = { ] }, extendedLatin: { - expression: /[\u0100-\u017F]|[\u0180-\u024F]/g, + regularExpression: /[\u0100-\u017F]|[\u0180-\u024F]/g, languages: [ "af", "az", @@ -64,7 +64,7 @@ quail.components.language = { ] }, arabic: { - expression: /[\u0600-\u06FF]/g, + regularExpression: /[\u0600-\u06FF]/g, languages: [ "ar", "fa", @@ -73,7 +73,7 @@ quail.components.language = { ] }, cryllic: { - expression: /[\u0400-\u04FF]|[\u0500-\u052F]/g, + regularExpression: /[\u0400-\u04FF]|[\u0500-\u052F]/g, languages: [ "bg", "kk", diff --git a/src/js/custom/languageChangesAreIdentified.js b/src/js/custom/languageChangesAreIdentified.js index 82df2d284..58b1e7c50 100644 --- a/src/js/custom/languageChangesAreIdentified.js +++ b/src/js/custom/languageChangesAreIdentified.js @@ -1,3 +1,48 @@ quail.languageChangesAreIdentified = function() { + var currentLanguage = quail.components.language.getDocumentLanguage(true); + var text, regularExpression, matches, $element; + var noCharactersMatch = function($element, language, matches, regularExpression) { + var $children = $element.find('[lang=' + language + ']'); + var childMatches; + if($children.length === 0) { + return true; + } + matches = matches.length; + $children.each(function() { + childMatches = quail.getTextContents($(this)).match(regularExpression); + if(childMatches) { + matches -= childMatches.length; + } + }); + return matches > 0; + }; + + quail.html.find(quail.textSelector).each(function() { + $element = $(this); + if($element.attr('lang')) { + currentLanguage = $element.attr('lang').trim().toLowerCase().split('-')[0]; + } + text = quail.getTextContents($element); + + $.each(quail.components.language.scriptSingletons, function(code, regularExpression) { + if(code === currentLanguage) { + return; + } + matches = text.match(regularExpression); + if(matches && matches.length && noCharactersMatch($element, code, matches, regularExpression)) { + quail.testFails('languageChangesAreIdentified', $element, { language : code }); + } + }); + $.each(quail.components.language.scripts, function(code, script) { + if(script.languages.indexOf(currentLanguage) !== -1) { + return; + } + matches = text.match(script.regularExpression); + if(matches && matches.length && noCharactersMatch($element, code, matches, regularExpression)) { + quail.testFails('languageChangesAreIdentified', $element, { language : code }); + } + }); + + }); }; \ No newline at end of file diff --git a/test/quail.html b/test/quail.html index 99da830de..a2f7cf871 100644 --- a/test/quail.html +++ b/test/quail.html @@ -411,6 +411,12 @@ "testfiles/common/labelMustNotBeEmpty-fail.html", "testfiles/common/labelMustNotBeEmpty-pass-2.html", "testfiles/common/labelMustNotBeEmpty-pass.html", + "testfiles/common/languageChangesAreIdentified-fail.html", + "testfiles/common/languageChangesAreIdentified-fail-2.html", + "testfiles/common/languageChangesAreIdentified-fail-3.html", + "testfiles/common/languageChangesAreIdentified-pass.html", + "testfiles/common/languageChangesAreIdentified-pass-2.html", + "testfiles/common/languageChangesAreIdentified-pass-3.html", "testfiles/common/languageDirAttributeIsUsed-pass.html", "testfiles/common/languageDirAttributeIsUsed-pass-2.html", "testfiles/common/languageDirAttributeIsUsed-fail.html", diff --git a/test/testfiles/common/languageChangesAreIdentified-fail-2.html b/test/testfiles/common/languageChangesAreIdentified-fail-2.html new file mode 100644 index 000000000..060cf300c --- /dev/null +++ b/test/testfiles/common/languageChangesAreIdentified-fail-2.html @@ -0,0 +1,27 @@ + + + + + languageChangesAreIdentified-fail-2 + + + + +
+

This paragraph is just fine, it is written in English.

+

এটি একটি ভাষা একক স্ক্রিপ্ট করা হবে না.

+
+ + + + + + + + \ No newline at end of file diff --git a/test/testfiles/common/languageChangesAreIdentified-fail-3.html b/test/testfiles/common/languageChangesAreIdentified-fail-3.html new file mode 100644 index 000000000..40f99f52b --- /dev/null +++ b/test/testfiles/common/languageChangesAreIdentified-fail-3.html @@ -0,0 +1,26 @@ + + + + + languageChangesAreIdentified-fail-3 + + + + +
+

This paragraph is just fine, but this arabic script is not OK. هذا هو مكتوب باللغة العربية.

+
+ + + + + + + + \ No newline at end of file diff --git a/test/testfiles/common/languageChangesAreIdentified-fail.html b/test/testfiles/common/languageChangesAreIdentified-fail.html new file mode 100644 index 000000000..7b00b5b02 --- /dev/null +++ b/test/testfiles/common/languageChangesAreIdentified-fail.html @@ -0,0 +1,27 @@ + + + + + languageChangesAreIdentified-fail + + + + +
+

This paragraph is just fine, it is written in English.

+

এটি একটি ভাষা একক স্ক্রিপ্ট করা হবে না.

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

This paragraph is just fine, it is written in English. এটি একটি ভাষা একক স্ক্রিপ্ট করা হবে না.

+
+ + + + + + + + \ No newline at end of file diff --git a/test/testfiles/common/languageChangesAreIdentified-pass-3.html b/test/testfiles/common/languageChangesAreIdentified-pass-3.html new file mode 100644 index 000000000..c1465f50d --- /dev/null +++ b/test/testfiles/common/languageChangesAreIdentified-pass-3.html @@ -0,0 +1,27 @@ + + + + + languageChangesAreIdentified-pass-3 + + + + +
+

This paragraph is just fine, it is written in English.

+

هذا هو مكتوب باللغة العربية

+
+ + + + + + + + \ No newline at end of file diff --git a/test/testfiles/common/languageChangesAreIdentified-pass.html b/test/testfiles/common/languageChangesAreIdentified-pass.html index 7b00b5b02..4d3c5b4e1 100644 --- a/test/testfiles/common/languageChangesAreIdentified-pass.html +++ b/test/testfiles/common/languageChangesAreIdentified-pass.html @@ -2,7 +2,7 @@ - languageChangesAreIdentified-fail + languageChangesAreIdentified-pass @@ -10,7 +10,7 @@

This paragraph is just fine, it is written in English.

-

এটি একটি ভাষা একক স্ক্রিপ্ট করা হবে না.

+

এটি একটি ভাষা একক স্ক্রিপ্ট করা হবে না.

@@ -19,7 +19,7 @@ diff --git a/test/testfiles/common/languageDirAttributeIsUsed-fail-4.html b/test/testfiles/common/languageDirAttributeIsUsed-fail-4.html index 9c5e7f422..cb25afc0d 100644 --- a/test/testfiles/common/languageDirAttributeIsUsed-fail-4.html +++ b/test/testfiles/common/languageDirAttributeIsUsed-fail-4.html @@ -25,7 +25,6 @@