Reduce the overall indentation level in Catalog_readDocumentOutline, by using early returns, in order to improve readability

This commit is contained in:
Jonas Jenwald 2016-02-13 23:13:01 +01:00
parent e9a1a47d28
commit 98db068079
2 changed files with 70 additions and 56 deletions

View File

@ -128,26 +128,29 @@ var Catalog = (function CatalogClosure() {
return shadow(this, 'documentOutline', obj);
},
readDocumentOutline: function Catalog_readDocumentOutline() {
var xref = this.xref;
var obj = this.catDict.get('Outlines');
var root = { items: [] };
if (isDict(obj)) {
if (!isDict(obj)) {
return null;
}
obj = obj.getRaw('First');
var processed = new RefSet();
if (isRef(obj)) {
if (!isRef(obj)) {
return null;
}
var root = { items: [] };
var queue = [{obj: obj, parent: root}];
// to avoid recursion keeping track of the items
// in the processed dictionary
// To avoid recursion, keep track of the already processed items.
var processed = new RefSet();
processed.put(obj);
var xref = this.xref;
while (queue.length > 0) {
var i = queue.shift();
var outlineDict = xref.fetchIfRef(i.obj);
if (outlineDict === null) {
continue;
}
if (!outlineDict.has('Title')) {
error('Invalid outline item');
}
assert(outlineDict.has('Title'), 'Invalid outline item');
var actionDict = outlineDict.get('A'), dest = null, url = null;
if (actionDict) {
var destEntry = actionDict.get('D');
@ -188,8 +191,6 @@ var Catalog = (function CatalogClosure() {
processed.put(obj);
}
}
}
}
return (root.items.length > 0 ? root.items : null);
},
get numPages() {

View File

@ -430,6 +430,19 @@ describe('api', function() {
loadingTask.destroy();
});
});
it('gets non-existent outline', function() {
var url = combineUrl(window.location.href, '../pdfs/tracemonkey.pdf');
var loadingTask = PDFJS.getDocument(url);
var promise = loadingTask.promise.then(function (pdfDocument) {
return pdfDocument.getOutline();
});
waitsForPromiseResolved(promise, function (outline) {
expect(outline).toEqual(null);
loadingTask.destroy();
});
});
it('gets outline', function() {
var promise = doc.getOutline();
waitsForPromiseResolved(promise, function(outline) {