mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Add better support for AppOptions with multiple types (PR 18465 follow-up)
For options with varying types, see `useSystemFonts`, we're not sufficiently validating the type when setting a new value. This means that for an option that uses `OptionKind.UNDEF_ALLOWED` we'd allow *any* value, which is obviously not the intention. Hence we instead introduce a new and *optional* `type`-field that allows specifying exactly which types are valid when multiple ones are supported. *Note:* This obviously didn't occur to me until after PR 18465 landed, sorry about that!
This commit is contained in:
parent
deaac2839e
commit
b3ea789286
1 changed files with 18 additions and 11 deletions
|
@ -55,10 +55,18 @@ const OptionKind = {
|
||||||
API: 0x04,
|
API: 0x04,
|
||||||
WORKER: 0x08,
|
WORKER: 0x08,
|
||||||
EVENT_DISPATCH: 0x10,
|
EVENT_DISPATCH: 0x10,
|
||||||
UNDEF_ALLOWED: 0x20,
|
|
||||||
PREFERENCE: 0x80,
|
PREFERENCE: 0x80,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Should only be used with options that allow multiple types.
|
||||||
|
const Type = {
|
||||||
|
BOOLEAN: 0x01,
|
||||||
|
NUMBER: 0x02,
|
||||||
|
OBJECT: 0x04,
|
||||||
|
STRING: 0x08,
|
||||||
|
UNDEFINED: 0x10,
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE: These options are used to generate the `default_preferences.json` file,
|
* NOTE: These options are used to generate the `default_preferences.json` file,
|
||||||
* see `OptionKind.PREFERENCE`, hence the values below must use only
|
* see `OptionKind.PREFERENCE`, hence the values below must use only
|
||||||
|
@ -405,7 +413,8 @@ const defaultOptions = {
|
||||||
)
|
)
|
||||||
? false
|
? false
|
||||||
: undefined,
|
: undefined,
|
||||||
kind: OptionKind.API + OptionKind.UNDEF_ALLOWED,
|
kind: OptionKind.API,
|
||||||
|
type: Type.BOOLEAN + Type.UNDEFINED,
|
||||||
},
|
},
|
||||||
verbosity: {
|
verbosity: {
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
|
@ -484,7 +493,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
|
||||||
// Ensure that the `defaultOptions` are correctly specified.
|
// Ensure that the `defaultOptions` are correctly specified.
|
||||||
for (const name in defaultOptions) {
|
for (const name in defaultOptions) {
|
||||||
const { value, kind } = defaultOptions[name];
|
const { value, kind, type } = defaultOptions[name];
|
||||||
|
|
||||||
if (kind & OptionKind.PREFERENCE) {
|
if (kind & OptionKind.PREFERENCE) {
|
||||||
if (kind === OptionKind.PREFERENCE) {
|
if (kind === OptionKind.PREFERENCE) {
|
||||||
|
@ -493,9 +502,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
|
||||||
if (kind & OptionKind.BROWSER) {
|
if (kind & OptionKind.BROWSER) {
|
||||||
throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`);
|
throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`);
|
||||||
}
|
}
|
||||||
if (kind & OptionKind.UNDEF_ALLOWED) {
|
if (type !== undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Cannot allow \`undefined\` value for "PREFERENCE" kind: ${name}`
|
`Cannot have \`type\`-field for "PREFERENCE" kind: ${name}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (typeof compatParams === "object" && compatParams.has(name)) {
|
if (typeof compatParams === "object" && compatParams.has(name)) {
|
||||||
|
@ -512,9 +521,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
|
||||||
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
|
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
|
||||||
}
|
}
|
||||||
} else if (kind & OptionKind.BROWSER) {
|
} else if (kind & OptionKind.BROWSER) {
|
||||||
if (kind & OptionKind.UNDEF_ALLOWED) {
|
if (type !== undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Cannot allow \`undefined\` value for "BROWSER" kind: ${name}`
|
`Cannot have \`type\`-field for "BROWSER" kind: ${name}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (typeof compatParams === "object" && compatParams.has(name)) {
|
if (typeof compatParams === "object" && compatParams.has(name)) {
|
||||||
|
@ -565,8 +574,7 @@ class AppOptions {
|
||||||
!defaultOpt ||
|
!defaultOpt ||
|
||||||
!(
|
!(
|
||||||
typeof value === typeof defaultOpt.value ||
|
typeof value === typeof defaultOpt.value ||
|
||||||
(defaultOpt.kind & OptionKind.UNDEF_ALLOWED &&
|
Type[(typeof value).toUpperCase()] & defaultOpt.type
|
||||||
(value === undefined || defaultOpt.value === undefined))
|
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
|
@ -585,8 +593,7 @@ class AppOptions {
|
||||||
!defaultOpt ||
|
!defaultOpt ||
|
||||||
!(
|
!(
|
||||||
typeof userOpt === typeof defaultOpt.value ||
|
typeof userOpt === typeof defaultOpt.value ||
|
||||||
(defaultOpt.kind & OptionKind.UNDEF_ALLOWED &&
|
Type[(typeof userOpt).toUpperCase()] & defaultOpt.type
|
||||||
(userOpt === undefined || defaultOpt.value === undefined))
|
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue