Reduce the overall indentation level in Catalog_readDocumentOutline
, by using early returns, in order to improve readability
This commit is contained in:
parent
e9a1a47d28
commit
98db068079
113
src/core/obj.js
113
src/core/obj.js
@ -128,66 +128,67 @@ var Catalog = (function CatalogClosure() {
|
|||||||
return shadow(this, 'documentOutline', obj);
|
return shadow(this, 'documentOutline', obj);
|
||||||
},
|
},
|
||||||
readDocumentOutline: function Catalog_readDocumentOutline() {
|
readDocumentOutline: function Catalog_readDocumentOutline() {
|
||||||
var xref = this.xref;
|
|
||||||
var obj = this.catDict.get('Outlines');
|
var obj = this.catDict.get('Outlines');
|
||||||
|
if (!isDict(obj)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
obj = obj.getRaw('First');
|
||||||
|
if (!isRef(obj)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
var root = { items: [] };
|
var root = { items: [] };
|
||||||
if (isDict(obj)) {
|
var queue = [{obj: obj, parent: root}];
|
||||||
obj = obj.getRaw('First');
|
// To avoid recursion, keep track of the already processed items.
|
||||||
var processed = new RefSet();
|
var processed = new RefSet();
|
||||||
if (isRef(obj)) {
|
processed.put(obj);
|
||||||
var queue = [{obj: obj, parent: root}];
|
var xref = this.xref;
|
||||||
// to avoid recursion keeping track of the items
|
|
||||||
// in the processed dictionary
|
while (queue.length > 0) {
|
||||||
processed.put(obj);
|
var i = queue.shift();
|
||||||
while (queue.length > 0) {
|
var outlineDict = xref.fetchIfRef(i.obj);
|
||||||
var i = queue.shift();
|
if (outlineDict === null) {
|
||||||
var outlineDict = xref.fetchIfRef(i.obj);
|
continue;
|
||||||
if (outlineDict === null) {
|
}
|
||||||
continue;
|
assert(outlineDict.has('Title'), 'Invalid outline item');
|
||||||
}
|
|
||||||
if (!outlineDict.has('Title')) {
|
var actionDict = outlineDict.get('A'), dest = null, url = null;
|
||||||
error('Invalid outline item');
|
if (actionDict) {
|
||||||
}
|
var destEntry = actionDict.get('D');
|
||||||
var actionDict = outlineDict.get('A'), dest = null, url = null;
|
if (destEntry) {
|
||||||
if (actionDict) {
|
dest = destEntry;
|
||||||
var destEntry = actionDict.get('D');
|
} else {
|
||||||
if (destEntry) {
|
var uriEntry = actionDict.get('URI');
|
||||||
dest = destEntry;
|
if (isString(uriEntry) && isValidUrl(uriEntry, false)) {
|
||||||
} else {
|
url = uriEntry;
|
||||||
var uriEntry = actionDict.get('URI');
|
|
||||||
if (isString(uriEntry) && isValidUrl(uriEntry, false)) {
|
|
||||||
url = uriEntry;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (outlineDict.has('Dest')) {
|
|
||||||
dest = outlineDict.getRaw('Dest');
|
|
||||||
if (isName(dest)) {
|
|
||||||
dest = dest.name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var title = outlineDict.get('Title');
|
|
||||||
var outlineItem = {
|
|
||||||
dest: dest,
|
|
||||||
url: url,
|
|
||||||
title: stringToPDFString(title),
|
|
||||||
color: outlineDict.get('C') || [0, 0, 0],
|
|
||||||
count: outlineDict.get('Count'),
|
|
||||||
bold: !!(outlineDict.get('F') & 2),
|
|
||||||
italic: !!(outlineDict.get('F') & 1),
|
|
||||||
items: []
|
|
||||||
};
|
|
||||||
i.parent.items.push(outlineItem);
|
|
||||||
obj = outlineDict.getRaw('First');
|
|
||||||
if (isRef(obj) && !processed.has(obj)) {
|
|
||||||
queue.push({obj: obj, parent: outlineItem});
|
|
||||||
processed.put(obj);
|
|
||||||
}
|
|
||||||
obj = outlineDict.getRaw('Next');
|
|
||||||
if (isRef(obj) && !processed.has(obj)) {
|
|
||||||
queue.push({obj: obj, parent: i.parent});
|
|
||||||
processed.put(obj);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (outlineDict.has('Dest')) {
|
||||||
|
dest = outlineDict.getRaw('Dest');
|
||||||
|
if (isName(dest)) {
|
||||||
|
dest = dest.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var title = outlineDict.get('Title');
|
||||||
|
var outlineItem = {
|
||||||
|
dest: dest,
|
||||||
|
url: url,
|
||||||
|
title: stringToPDFString(title),
|
||||||
|
color: outlineDict.get('C') || [0, 0, 0],
|
||||||
|
count: outlineDict.get('Count'),
|
||||||
|
bold: !!(outlineDict.get('F') & 2),
|
||||||
|
italic: !!(outlineDict.get('F') & 1),
|
||||||
|
items: []
|
||||||
|
};
|
||||||
|
i.parent.items.push(outlineItem);
|
||||||
|
obj = outlineDict.getRaw('First');
|
||||||
|
if (isRef(obj) && !processed.has(obj)) {
|
||||||
|
queue.push({obj: obj, parent: outlineItem});
|
||||||
|
processed.put(obj);
|
||||||
|
}
|
||||||
|
obj = outlineDict.getRaw('Next');
|
||||||
|
if (isRef(obj) && !processed.has(obj)) {
|
||||||
|
queue.push({obj: obj, parent: i.parent});
|
||||||
|
processed.put(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (root.items.length > 0 ? root.items : null);
|
return (root.items.length > 0 ? root.items : null);
|
||||||
|
@ -430,6 +430,19 @@ describe('api', function() {
|
|||||||
loadingTask.destroy();
|
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() {
|
it('gets outline', function() {
|
||||||
var promise = doc.getOutline();
|
var promise = doc.getOutline();
|
||||||
waitsForPromiseResolved(promise, function(outline) {
|
waitsForPromiseResolved(promise, function(outline) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user