fix jasmine and add some specs for direction detection

This commit is contained in:
Florian Staudacher 2014-09-12 03:18:34 +02:00
parent be86014540
commit 10609c3692
6 changed files with 88 additions and 7 deletions

View file

@ -18,7 +18,7 @@
return false;
}
var charCode = str.charCodeAt(0);
var charCode = this._fixedCharCodeAt(str, 0);
if(charCode >= 1536 && charCode <= 1791) // Sarabic, Persian, ...
return true;
@ -34,6 +34,12 @@
else if(charCode>=64256 && charCode<=64335) // Hebrew present
return true;
else if(charCode>=68096 && charCode<=68184) // Kharoshthi
return true;
else if(charCode>=67840 && charCode<=67871) // Phoenician
return true;
else if(charCode>=1792 && charCode<=1871) // Syriac
return true;
@ -47,6 +53,39 @@
return true;
return false;
},
// source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
_fixedCharCodeAt: function(str, idx) {
str += '';
var code,
end = str.length;
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
while ((surrogatePairs.exec(str)) != null) {
var li = surrogatePairs.lastIndex;
if (li - 2 < idx) {
idx++;
}
else {
break;
}
}
if (idx >= end || idx < 0) {
return NaN;
}
code = str.charCodeAt(idx);
var hi, low;
if (0xD800 <= code && code <= 0xDBFF) {
hi = code;
low = str.charCodeAt(idx+1);
// Go one further, since one of the "characters" is part of a surrogate pair
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
return code;
}
};
})();

View file

@ -59,8 +59,9 @@ Handlebars.registerHelper('hovercardable', function(person) {
Handlebars.registerHelper('personImage', function(person, size, imageClass) {
/* we return here if person.avatar is blank, because this happens when a
* user is unauthenticated. we don't know why this happens... */
if( !person.avatar &&
!(person.profile && person.profile.avatar) ) return;
var avatar = person.avatar || person.profile.avatar;
if( !avatar ) return;
var name = ( person.name ) ? person.name : 'avatar';
size = ( !_.isString(size) ) ? "small" : size;

View file

@ -6,6 +6,5 @@
//= require inbox
//= require mobile
//= require profile
//= require people
//= require contact-list
//= require sinon

View file

@ -0,0 +1,37 @@
describe("app.helpers.txtDirection", function() {
context("#isRTL", function() {
beforeEach(function() {
this.samples = {
"ثم بغزو ناجازاكي الأوروبي بال, ": "rtl", // arabic
"אם ברית מחליטה זכר, צ'ט לשון": "rtl", // hebrew
"ߊߍߌߐߎ": "rtl", // n'ko
"𐨙𐨜𐨪𐨭𐨢": "rtl", // Kharoshthi
"𐤂𐤃𐤄𐤅𐤆𐤇𐤈𐤉𐤊": "rtl", // Phoenecian
"ܫܠܡܐ": "rtl", //syriac
"ހަށް ގޮސް އުޅޭ އިރު": "rtl", // thaana
"ⴻⴼⴽⵄⵅⵆⵇ": "rtl", // Tifinagh
"ᚳᚴᚵᚶᚷᚸᚹᛅᛆᛇᛈᛉᛊᛋ": "ltr", // Runes
"ΘΛΞΠΣΦΨΩέαβγζλφχψϖϗ": "ltr", // Greek
"経担裁洋府時話家": "ltr", // Chinese
"Анёмал зэнтынтиаэ": "ltr", // Cyrillic
"उपेक्ष सोफ़्टवेर विचारशिलता": "ltr", // Hindi
"選そ前制数えほ長春セ名": "ltr", // Japanese
"ascii text": "ltr",
};
});
it("detects the right text direction", function() {
_.each(this.samples, function(dir, str) {
var result = app.helpers.txtDirection.isRTL(str);
if( result ) {
expect(dir).toEqual('rtl');
} else {
expect(dir).toEqual('ltr');
}
});
});
});
});
101

View file

@ -1,10 +1,15 @@
describe("app.views.AspectsDropdown", function(){
beforeEach(function() {
spec.loadFixture("bookmarklet");
Diaspora.I18n.load({
Diaspora.I18n.reset({
'aspect_dropdown': {
'select_aspects': "Select aspects"
});
'select_aspects': "Select aspects",
'all_aspects': "All Aspects",
'toggle': {
'zero': "Select aspects",
'one': "In <%= count %> aspect",
'other': "In <%= count %> aspects"
}}});
this.view = new app.views.AspectsDropdown({el: $('.aspect_dropdown')});
});

View file

@ -29,7 +29,7 @@ describe("app.views.Poll", function(){
this.view.vote(answer.id);
var obj = jasmine.Ajax.requests.mostRecent().params);
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
expect(obj.poll_id).toBe(poll.poll_id);
expect(obj.poll_answer_id).toBe(answer.id);
})