1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-21 23:58:07 +02:00

Replace the remaining occurences of instanceof Array with Array.isArray()

*Follow-up to PRs 8864 and 8813.*

As explained in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray, `instanceof Array` can have inconsistent behavior. To ensure that only `Array.isArray` is used, an ESLint plugin/rule is added to enforce this.
This commit is contained in:
Jonas Jenwald 2018-07-09 13:11:35 +02:00
parent 200e3d6bd8
commit 61186698c3
8 changed files with 93 additions and 13 deletions

View file

@ -499,7 +499,7 @@ class BaseViewer {
}
if (!labels) {
this._pageLabels = null;
} else if (!(labels instanceof Array &&
} else if (!(Array.isArray(labels) &&
this.pdfDocument.numPages === labels.length)) {
this._pageLabels = null;
console.error(`${this._name}.setPageLabels: Invalid page labels.`);

View file

@ -164,7 +164,7 @@ class PDFHistory {
return;
}
if ((namedDest && typeof namedDest !== 'string') ||
!(explicitDest instanceof Array) ||
!Array.isArray(explicitDest) ||
!(Number.isInteger(pageNumber) &&
pageNumber > 0 && pageNumber <= this.linkService.pagesCount)) {
console.error('PDFHistory.push: Invalid parameters.');
@ -567,7 +567,7 @@ function isDestArraysEqual(firstDest, secondDest) {
if (typeof first !== typeof second) {
return false;
}
if (first instanceof Array || second instanceof Array) {
if (Array.isArray(first) || Array.isArray(second)) {
return false;
}
if (first !== null && typeof first === 'object' && second !== null) {
@ -584,7 +584,7 @@ function isDestArraysEqual(firstDest, secondDest) {
return first === second || (Number.isNaN(first) && Number.isNaN(second));
}
if (!(firstDest instanceof Array && secondDest instanceof Array)) {
if (!(Array.isArray(firstDest) && Array.isArray(secondDest))) {
return false;
}
if (firstDest.length !== secondDest.length) {

View file

@ -162,7 +162,7 @@ class PDFLinkService {
explicitDest: dest,
});
}).then((data) => {
if (!(data.explicitDest instanceof Array)) {
if (!Array.isArray(data.explicitDest)) {
console.error(`PDFLinkService.navigateTo: "${data.explicitDest}" is` +
` not a valid destination array, for dest="${dest}".`);
return;
@ -179,7 +179,7 @@ class PDFLinkService {
if (typeof dest === 'string') {
return this.getAnchorUrl('#' + escape(dest));
}
if (dest instanceof Array) {
if (Array.isArray(dest)) {
let str = JSON.stringify(dest);
return this.getAnchorUrl('#' + escape(str));
}
@ -273,7 +273,7 @@ class PDFLinkService {
try {
dest = JSON.parse(dest);
if (!(dest instanceof Array)) {
if (!Array.isArray(dest)) {
// Avoid incorrectly rejecting a valid named destination, such as
// e.g. "4.3" or "true", because `JSON.parse` converted its type.
dest = dest.toString();
@ -368,7 +368,7 @@ class PDFLinkService {
}
function isValidExplicitDestination(dest) {
if (!(dest instanceof Array)) {
if (!Array.isArray(dest)) {
return false;
}
let destLength = dest.length, allowNull = true;

View file

@ -208,7 +208,7 @@ class PDFThumbnailViewer {
}
if (!labels) {
this._pageLabels = null;
} else if (!(labels instanceof Array &&
} else if (!(Array.isArray(labels) &&
this.pdfDocument.numPages === labels.length)) {
this._pageLabels = null;
console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');