Fix nested translators that rely on child translator tags
This commit is contained in:
parent
e34c9e04e8
commit
7608e127f1
|
@ -127,8 +127,10 @@ Zotero.Translate.Sandbox = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Canonicalize tags
|
// If we're not in a child translator, canonicalize tags
|
||||||
if(item.tags) item.tags = translate._cleanTags(item.tags);
|
if (!translate._parentTranslator) {
|
||||||
|
if(item.tags) item.tags = translate._cleanTags(item.tags);
|
||||||
|
}
|
||||||
|
|
||||||
// if we're not supposed to save the item or we're in a child translator,
|
// if we're not supposed to save the item or we're in a child translator,
|
||||||
// just return the item array
|
// just return the item array
|
||||||
|
@ -158,8 +160,10 @@ Zotero.Translate.Sandbox = {
|
||||||
delete attachment.document;
|
delete attachment.document;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Canonicalize tags
|
// If we're not in a child translator, canonicalize tags
|
||||||
if(attachment.tags !== undefined) attachment.tags = translate._cleanTags(attachment.tags);
|
if (!translate._parentTranslator) {
|
||||||
|
if(attachment.tags !== undefined) attachment.tags = translate._cleanTags(attachment.tags);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,13 +173,14 @@ Zotero.Translate.Sandbox = {
|
||||||
var note = notes[j];
|
var note = notes[j];
|
||||||
if(!note) {
|
if(!note) {
|
||||||
notes.splice(j--, 1);
|
notes.splice(j--, 1);
|
||||||
} else if(typeof(note) == "object") {
|
} else if(typeof(note) != "object") {
|
||||||
// Canonicalize tags
|
|
||||||
if(note.tags !== undefined) note.tags = translate._cleanTags(note.tags);
|
|
||||||
} else {
|
|
||||||
// Convert to object
|
// Convert to object
|
||||||
notes[j] = {"note":note.toString()}
|
notes[j] = {"note":note.toString()}
|
||||||
}
|
}
|
||||||
|
// If we're not in a child translator, canonicalize tags
|
||||||
|
if (!translate._parentTranslator) {
|
||||||
|
if(note.tags !== undefined) note.tags = translate._cleanTags(note.tags);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -757,10 +757,10 @@ var generateTranslatorExportData = Zotero.Promise.coroutine(function* generateTr
|
||||||
/**
|
/**
|
||||||
* Build a dummy translator that can be passed to Zotero.Translate
|
* Build a dummy translator that can be passed to Zotero.Translate
|
||||||
*/
|
*/
|
||||||
function buildDummyTranslator(translatorType, code) {
|
function buildDummyTranslator(translatorType, code, translatorID="dummy-translator") {
|
||||||
let info = {
|
let info = {
|
||||||
"translatorID":"dummy-translator",
|
"translatorID":translatorID,
|
||||||
"translatorType":1, // import
|
"translatorType":translatorType,
|
||||||
"label":"Dummy Translator",
|
"label":"Dummy Translator",
|
||||||
"creator":"Simon Kornblith",
|
"creator":"Simon Kornblith",
|
||||||
"target":"",
|
"target":"",
|
||||||
|
@ -771,6 +771,7 @@ function buildDummyTranslator(translatorType, code) {
|
||||||
};
|
};
|
||||||
let translator = new Zotero.Translator(info);
|
let translator = new Zotero.Translator(info);
|
||||||
translator.code = code;
|
translator.code = code;
|
||||||
|
translator.getCode = function() {return Promise.resolve(code)};
|
||||||
return translator;
|
return translator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -551,6 +551,49 @@ describe("Zotero.Translate", function() {
|
||||||
assert.equal(pdf.attachmentLinkMode, Zotero.Attachments.LINK_MODE_IMPORTED_URL);
|
assert.equal(pdf.attachmentLinkMode, Zotero.Attachments.LINK_MODE_IMPORTED_URL);
|
||||||
checkTestTags(pdf, true);
|
checkTestTags(pdf, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not convert tags to canonical form in child translators', function* () {
|
||||||
|
var childTranslator = buildDummyTranslator(1,
|
||||||
|
`function detectWeb() {}
|
||||||
|
function doImport() {
|
||||||
|
var item = new Zotero.Item;
|
||||||
|
item.itemType = "book";
|
||||||
|
item.title = "The Definitive Guide of Owls";
|
||||||
|
item.tags = ['owl', 'tag'];
|
||||||
|
item.complete();
|
||||||
|
}`, 'child-dummy-translator'
|
||||||
|
);
|
||||||
|
sinon.stub(Zotero.Translators, 'get').withArgs('child-dummy-translator').returns(childTranslator);
|
||||||
|
|
||||||
|
var parentTranslator = buildDummyTranslator(1,
|
||||||
|
`function detectWeb() {}
|
||||||
|
function doImport() {
|
||||||
|
var translator = Zotero.loadTranslator("import");
|
||||||
|
translator.setTranslator('child-dummy-translator');
|
||||||
|
translator.setHandler('itemDone', Zotero.childItemDone);
|
||||||
|
translator.translate();
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
|
||||||
|
function childItemDone(obj, item) {
|
||||||
|
// Non-canonical tags after child translator is done
|
||||||
|
assert.deepEqual(['owl', 'tag'], item.tags);
|
||||||
|
item.complete();
|
||||||
|
}
|
||||||
|
|
||||||
|
var translate = new Zotero.Translate.Import();
|
||||||
|
translate.setTranslator(parentTranslator);
|
||||||
|
translate.setString("");
|
||||||
|
yield translate._loadTranslator(parentTranslator);
|
||||||
|
translate._sandboxManager.importObject({childItemDone});
|
||||||
|
|
||||||
|
var items = yield translate.translate();
|
||||||
|
|
||||||
|
// Canonicalized tags after parent translator
|
||||||
|
assert.deepEqual([{tag: 'owl'}, {tag: 'tag'}], items[0].getTags());
|
||||||
|
|
||||||
|
Zotero.Translators.get.restore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user