Merge pull request #10610 from Snuffleupagus/prefs-type-validation

Add type validation to the `default_preferences` generation (PR 10548 follow-up)
This commit is contained in:
Tim van der Meij 2019-03-06 23:37:01 +01:00 committed by GitHub
commit 07ef11f1a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 8 deletions

View File

@ -535,7 +535,7 @@ gulp.task('default_preferences-pre', function() {
], { base: 'src/', }),
gulp.src([
'web/*.js',
'!web/{pdfjs,preferences,viewer}.js',
'!web/{app,pdfjs,preferences,viewer}.js',
], { base: '.', }),
]).pipe(transform('utf8', preprocess))
.pipe(gulp.dest(DEFAULT_PREFERENCES_DIR + 'lib/'));

View File

@ -187,7 +187,9 @@ let PDFViewerApplication = {
for (const name in prefs) {
AppOptions.set(name, prefs[name]);
}
} catch (reason) { }
} catch (reason) {
console.error(`_readPreferences: "${reason.message}".`);
}
},
/**

View File

@ -222,7 +222,7 @@ const defaultOptions = {
},
};
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || GENERIC')) {
PDFJSDev.test('!PRODUCTION || (GENERIC && !LIB)')) {
defaultOptions.disablePreferences = {
/** @type {boolean} */
value: false,
@ -262,9 +262,15 @@ class AppOptions {
if ((kind & defaultOption.kind) === 0) {
continue;
}
if ((kind & OptionKind.PREFERENCE) !== 0) {
options[name] = defaultOption.value;
continue;
if (kind === OptionKind.PREFERENCE) {
const value = defaultOption.value, valueType = typeof value;
if (valueType === 'boolean' || valueType === 'string' ||
(valueType === 'number' && Number.isInteger(value))) {
options[name] = value;
continue;
}
throw new Error(`Invalid type for preference: ${name}`);
}
}
const userOption = userOptions[name];

View File

@ -35,8 +35,6 @@ function getDefaultPreferences() {
}
}).then(function({ AppOptions, OptionKind, }) {
return AppOptions.getAll(OptionKind.PREFERENCE);
}, function(reason) {
console.error(reason);
});
}
}