Fix Zotero.Date methods within translator sandbox

Only a few methods are imported, so some of the changes in 7bdcc17ed
have to be reverted.
This commit is contained in:
Dan Stillman 2017-01-17 02:23:15 -05:00
parent b00d1366b0
commit a017fe6666
2 changed files with 26 additions and 6 deletions

View file

@ -260,7 +260,7 @@ Zotero.Date = new function(){
this.isoToSQL = function (isoDate) {
return this.dateToSQL(this.isoToDate(isoDate), true);
return Zotero.Date.dateToSQL(Zotero.Date.isoToDate(isoDate), true); // no 'this' for translator sandbox
}
@ -294,13 +294,13 @@ Zotero.Date = new function(){
// Parse 'yesterday'/'today'/'tomorrow'
var lc = (string + '').toLowerCase();
if (lc == 'yesterday' || (Zotero.getString && lc === Zotero.getString('date.yesterday'))) {
string = this.dateToSQL(new Date(Date.now() - 1000*60*60*24)).substr(0, 10);
string = Zotero.Date.dateToSQL(new Date(Date.now() - 1000*60*60*24)).substr(0, 10); // no 'this' for translator sandbox
}
else if (lc == 'today' || (Zotero.getString && lc == Zotero.getString('date.today'))) {
string = this.dateToSQL(new Date()).substr(0, 10);
string = Zotero.Date.dateToSQL(new Date()).substr(0, 10);
}
else if (lc == 'tomorrow' || (Zotero.getString && lc == Zotero.getString('date.tomorrow'))) {
string = this.dateToSQL(new Date(Date.now() + 1000*60*60*24)).substr(0, 10);
string = Zotero.Date.dateToSQL(new Date(Date.now() + 1000*60*60*24)).substr(0, 10);
}
else {
string = string.toString().replace(/^\s+|\s+$/g, "").replace(/\s+/, " ");
@ -425,7 +425,7 @@ Zotero.Date = new function(){
// MONTH
if(date.month === undefined) {
// compile month regular expression
let months = this.getMonths(true);
let months = Zotero.Date.getMonths(true); // no 'this' for translator sandbox
months = months.short.map(m => m.toLowerCase())
.concat(months.long.map(m => m.toLowerCase()));
@ -601,7 +601,7 @@ Zotero.Date = new function(){
string += date.part+" ";
}
var months = this.getMonths().long;
var months = Zotero.Date.getMonths().long; // no 'this' for translator sandbox
if(date.month != undefined && months[date.month]) {
// get short month strings from CSL interpreter
string += months[date.month];

View file

@ -156,6 +156,26 @@ describe("Zotero.Date", function() {
})
})
describe("#strToDate()", function () {
it("should work in translator sandbox", function* () {
var item = createUnsavedDataObject('item');
item.libraryID = Zotero.Libraries.userLibraryID;
item.setField('date', '2017-01-17');
var called = false;
var translation = new Zotero.Translate.Export();
translation.setItems([item]);
translation.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4"); // BibTeX
translation.setHandler("done", function (obj, worked) {
called = true;
assert.isTrue(worked);
assert.include(obj.string, "{2017}");
});
yield translation.translate();
assert.ok(called);
});
});
describe("#isHTTPDate()", function() {
it("should determine whether a date is an RFC 2822 compliant date", function() {
assert.ok(Zotero.Date.isHTTPDate("Mon, 13 Jun 2016 02:09:08 +4000"));