From e44426b8790421263204d204d87185cf8de91c26 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Mon, 15 Apr 2019 07:36:01 -0500 Subject: [PATCH] Revert sections checks for empty array --- background/style-manager.js | 2 +- edit/edit.js | 25 ++++++++++--------------- edit/sections-editor.js | 6 +++--- edit/source-editor.js | 3 +-- edit/util.js | 2 +- install-usercss/install-usercss.js | 2 +- js/messaging.js | 8 ++------ js/usercss.js | 2 +- manage/manage.js | 4 ++-- 9 files changed, 22 insertions(+), 32 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index 02d051e2..1560f13e 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -358,7 +358,7 @@ const styleManager = (() => { if (match === 'excluded') { excluded = true; } - for (const section of (data.sections || [])) { + for (const section of data.sections) { if (styleCodeEmpty(section.code)) { continue; } diff --git a/edit/edit.js b/edit/edit.js index 0c0d6d3e..78b6e78c 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -170,6 +170,7 @@ preinit(); $('#name').title = usercss ? t('usercssReplaceTemplateName') : ''; $('#preview-label').classList.toggle('hidden', !style.id); + $('#beautify').onclick = () => beautify(editor.getEditors()); $('#lint').addEventListener('scroll', hideLintHeaderOnScroll, {passive: true}); window.addEventListener('resize', () => debounce(rememberWindowSize, 100)); @@ -340,20 +341,19 @@ function initStyleData() { // TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425) const params = new URLSearchParams(location.search.replace(/^\?/, '')); const id = Number(params.get('id')); - const createNewSection = () => [ - Object.assign({code: ''}, - ...Object.keys(CssToProperty) - .map(name => ({ - [CssToProperty[name]]: params.get(name) && [params.get(name)] || [] - })) - ) - ]; const createEmptyStyle = () => ({ name: params.get('domain') || tryCatch(() => new URL(params.get('url-prefix')).hostname) || '', enabled: true, - sections: createNewSection(), + sections: [ + Object.assign({code: ''}, + ...Object.keys(CssToProperty) + .map(name => ({ + [CssToProperty[name]]: params.get(name) && [params.get(name)] || [] + })) + ) + ], }); return fetchStyle() .then(style => { @@ -372,12 +372,7 @@ function initStyleData() { function fetchStyle() { if (id) { - return API.getStyle(id).then(style => { - if (!style.sections || !style.sections.length) { - style.sections = createNewSection(); - } - return style; - }); + return API.getStyle(id); } return Promise.resolve(createEmptyStyle()); } diff --git a/edit/sections-editor.js b/edit/sections-editor.js index ac72c63e..99772f2b 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -46,7 +46,7 @@ function createSectionsEditor({style, onTitleChanged}) { let sectionOrder = ''; const initializing = new Promise(resolve => initSection({ - sections: (style.sections || []).slice(), + sections: style.sections.slice(), done:() => { // FIXME: implement this with CSS? // https://github.com/openstyles/stylus/commit/2895ce11e271788df0e4f7314b3b981fde086574 @@ -436,7 +436,7 @@ function createSectionsEditor({style, onTitleChanged}) { function chunk() { if (!originalSections.length) { setGlobalProgress(); - if (focusOn !== false && sections[focusOn]) { + if (focusOn !== false) { setTimeout(() => sections[focusOn].cm.focus()); } container.classList.remove('hidden'); @@ -545,7 +545,7 @@ function createSectionsEditor({style, onTitleChanged}) { updateSectionOrder(); } - function replaceSections(originalSections = []) { + function replaceSections(originalSections) { for (const section of sections) { section.remove(true); } diff --git a/edit/source-editor.js b/edit/source-editor.js index 310c319b..7119cfa5 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -18,7 +18,7 @@ function createSourceEditor({style, onTitleChanged}) { const dirty = dirtyReporter(); // normalize style - if (!style.id || !style.sections.length) setupNewStyle(style); + if (!style.id) setupNewStyle(style); const cm = cmFactory.create($('.single-editor'), { value: style.sourceCode, @@ -123,7 +123,6 @@ function createSourceEditor({style, onTitleChanged}) { } function setupNewStyle(style) { - if (!style.sections) style.sections = []; style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) + `/* ${t('usercssReplaceTemplateSectionBody')} */`; let section = sectionsToMozFormat(style); diff --git a/edit/util.js b/edit/util.js index 7a522b2a..cc18b515 100644 --- a/edit/util.js +++ b/edit/util.js @@ -103,7 +103,7 @@ function sectionsToMozFormat(style) { domains: 'domain', regexps: 'regexp', }; - return (style.sections || []).map(section => { + return style.sections.map(section => { let cssMds = []; for (const i in propertyToCss) { if (section[i]) { diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 53da010d..afbd6419 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -365,7 +365,7 @@ function getAppliesTo(style) { function *_gen() { - for (const section of (style.sections || [])) { + for (const section of style.sections) { for (const type of ['urls', 'urlPrefixes', 'domains', 'regexps']) { if (section[type]) { yield *section[type]; diff --git a/js/messaging.js b/js/messaging.js index 8d4ac72b..1ab433b5 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -290,12 +290,8 @@ function ignoreChromeError() { function getStyleWithNoCode(style) { const stripped = deepCopy(style); - if (stripped.sections && stripped.sections.length) { - for (const section of stripped.sections) section.code = null; - stripped.sourceCode = null; - } else { - stripped.sections = []; // Fixes #687 - } + for (const section of stripped.sections) section.code = null; + stripped.sourceCode = null; return stripped; } diff --git a/js/usercss.js b/js/usercss.js index bfe62bb4..deebcbbb 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -71,7 +71,7 @@ const usercss = (() => { if (!sections.length || errors && !allowErrors) { throw errors; } - style.sections = sections || []; + style.sections = sections; return allowErrors ? {style, errors} : style; }); } diff --git a/manage/manage.js b/manage/manage.js index f6e90894..741fc9bc 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -230,7 +230,7 @@ function createStyleElement({style, name}) { // clear the code to free up some memory // (note, style is already a deep copy) style.sourceCode = null; - (style.sections || []).forEach(section => (section.code = null)); + style.sections.forEach(section => (section.code = null)); const entry = parts.entry.cloneNode(true); entry.id = ENTRY_ID_PREFIX_RAW + style.id; @@ -266,7 +266,7 @@ function createStyleTargetsElement({entry, style}) { let numTargets = 0; const displayed = new Set(); for (const type of TARGET_TYPES) { - for (const section of (style.sections || [])) { + for (const section of style.sections) { for (const targetValue of section[type] || []) { if (displayed.has(targetValue)) { continue;