strip only dummy sections from the template

This commit is contained in:
tophf 2022-01-22 12:06:57 +03:00
parent b4b135826c
commit 45eeedbe97

View File

@ -154,12 +154,11 @@ function SourceEditor() {
} }
function setupNewStyle() { function setupNewStyle() {
style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) + const comment = `/* ${t('usercssReplaceTemplateSectionBody')} */`;
`/* ${t('usercssReplaceTemplateSectionBody')} */`; const sec0 = style.sections[0];
let section = MozDocMapper.styleToCss(style); sec0.code = ' '.repeat(prefs.get('editor.tabSize')) + comment;
if (!section.includes('@-moz-document')) { if (Object.keys(sec0).length === 1) { // the only key is 'code'
style.sections[0].domains = ['example.com']; sec0.domains = ['example.com'];
section = MozDocMapper.styleToCss(style);
} }
const DEFAULT_CODE = ` const DEFAULT_CODE = `
/* ==UserStyle== /* ==UserStyle==
@ -171,10 +170,13 @@ function SourceEditor() {
==/UserStyle== */ ==/UserStyle== */
`.replace(/^\s+/gm, ''); `.replace(/^\s+/gm, '');
style.name = [style.name, new Date().toLocaleString()].filter(Boolean).join(' - '); 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) style.sourceCode = (editor.template || DEFAULT_CODE)
.replace(/(@name)(?:([\t\x20]+).*|\n)/, (_, k, space) => `${k}${space || ' '}${style.name}`) .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.startOperation();
cm.setValue(style.sourceCode); cm.setValue(style.sourceCode);
cm.clearHistory(); cm.clearHistory();