From 94a940f933b82c993412ef9660afe6213b783e10 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Tue, 16 Apr 2019 04:59:12 -0500 Subject: [PATCH] Add styleJSONseemsValid function --- js/sections-util.js | 11 +++++++++++ manage/import-export.js | 11 +++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/js/sections-util.js b/js/sections-util.js index aeeeb19b..9a45b1c2 100644 --- a/js/sections-util.js +++ b/js/sections-util.js @@ -96,3 +96,14 @@ function calcStyleDigest(style) { return parts.join(''); } } + +function styleJSONseemsValid(json) { + return json + && json.name + && json.name.trim() + && Array.isArray(json.sections) + && json.sections + && json.sections.length + && typeof json.sections.every === 'function' + && typeof json.sections[0].code === 'string'; +} diff --git a/manage/import-export.js b/manage/import-export.js index 7f4ed619..327314c0 100644 --- a/manage/import-export.js +++ b/manage/import-export.js @@ -1,5 +1,6 @@ /* global messageBox styleSectionsEqual getOwnTab API onDOMready - tryJSONparse scrollElementIntoView $ $$ API $create t animateElement */ + tryJSONparse scrollElementIntoView $ $$ API $create t animateElement + styleJSONseemsValid */ 'use strict'; const STYLISH_DUMP_FILE_EXT = '.txt'; @@ -144,13 +145,7 @@ function importFromString(jsonString) { .then(done); function analyze(item, index) { - if (typeof item !== 'object' || - !item || - !item.name || - !item.name.trim() || - !item.sections || - !Array.isArray(item.sections) - ) { + if (typeof item !== 'object' || !styleJSONseemsValid(item)) { stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`); return; }