From 45eeedbe97a7f55fbc59def198748a2147a2f6c0 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 22 Jan 2022 12:06:57 +0300 Subject: [PATCH] strip only dummy sections from the template --- edit/source-editor.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/edit/source-editor.js b/edit/source-editor.js index fb118b1c..86810a28 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -154,12 +154,11 @@ function SourceEditor() { } function setupNewStyle() { - style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) + - `/* ${t('usercssReplaceTemplateSectionBody')} */`; - let section = MozDocMapper.styleToCss(style); - if (!section.includes('@-moz-document')) { - style.sections[0].domains = ['example.com']; - section = MozDocMapper.styleToCss(style); + const comment = `/* ${t('usercssReplaceTemplateSectionBody')} */`; + const sec0 = style.sections[0]; + sec0.code = ' '.repeat(prefs.get('editor.tabSize')) + comment; + if (Object.keys(sec0).length === 1) { // the only key is 'code' + sec0.domains = ['example.com']; } const DEFAULT_CODE = ` /* ==UserStyle== @@ -171,10 +170,13 @@ function SourceEditor() { ==/UserStyle== */ `.replace(/^\s+/gm, ''); style.name = [style.name, new Date().toLocaleString()].filter(Boolean).join(' - '); - // strip the last dummy section if any, add an empty line followed by the section style.sourceCode = (editor.template || DEFAULT_CODE) .replace(/(@name)(?:([\t\x20]+).*|\n)/, (_, k, space) => `${k}${space || ' '}${style.name}`) - .replace(/\s*@-moz-document[^{]*{[^}]*}\s*$|\s+$/g, '') + '\n\n' + section; + .replace(/\s*@-moz-document[^{]*{([^}]*)}\s*$/g, // stripping dummy sections + (s, body) => body.trim() === comment ? '\n\n' : s) + .trim() + + '\n\n' + + MozDocMapper.styleToCss(style); cm.startOperation(); cm.setValue(style.sourceCode); cm.clearHistory();