From ae6e2647a37689c203287e68f6df6e8edcf62851 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Sat, 8 Sep 2018 09:46:29 -0500 Subject: [PATCH] Add proper number & range validation --- _locales/en/messages.json | 9 +++++++++ js/usercss.js | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 0e9e5938..1f2a3c1b 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1051,6 +1051,15 @@ }, "description": "Error displayed when the value of @var color is invalid" }, + "styleMetaErrorRangeOrNumber": { + "message": "Invalid @var $type$: value must be an array containing at least one number at index zero", + "description": "Error displayed when the value of @var number or @var range is invalid", + "placeholders": { + "type": { + "content": "$1" + } + } + }, "styleMetaErrorPreprocessor": { "message": "Unsupported @preprocessor: $preprocessor$", "placeholders": { diff --git a/js/usercss.js b/js/usercss.js index 35067d08..c57eb8e9 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -231,10 +231,6 @@ var usercss = (() => { // but should not contain any numbers '4px' => 'px' result.units = labelIndex < 0 ? '' : state.value.splice(labelIndex, 1)[0].toString().replace(/[\d.+-]/g, ''); result.range = state.value.filter(item => !nonDigit.test(item)); - } else { - // not a range, fallback to text - result.type = 'text'; - result.default = state.value; } break; } @@ -617,6 +613,11 @@ var usercss = (() => { throw new Error(chrome.i18n.getMessage('styleMetaErrorCheckbox')); } else if (va.type === 'color') { va[value] = colorConverter.format(colorConverter.parse(va[value]), 'rgb'); + } else if ( + (va.type === 'number' || va.type === 'range') && + !(typeof va[value] === 'number' || Array.isArray(va.range)) + ) { + throw new Error(chrome.i18n.getMessage('styleMetaErrorRangeOrNumber', va.type)); } }