diff --git a/src/js/entries/editor.ts b/src/js/entries/editor.ts index bc6774dcd..846ee57d9 100644 --- a/src/js/entries/editor.ts +++ b/src/js/entries/editor.ts @@ -1,7 +1,9 @@ -import { defineMode, getMode, registerHelper } from 'codemirror' +import CodeMirror, { defineMode, getMode, registerHelper } from 'codemirror' import { Linter } from '../utils/Linter' import type { EditorConfiguration, ModeSpec } from 'codemirror' +type LintHelper = (text: string, ...args: unknown[]) => unknown + interface ModeSpecOptions { startOpen: boolean } @@ -19,3 +21,11 @@ registerHelper('lint', 'php', (text: string) => { return linter.annotations }) + +const cssLint = (<{ lint?: Record }> CodeMirror).lint?.css + +// CSSLint rejects the fr unit, so lint with px in its place. Both are two characters, which keeps marker positions. +if (cssLint) { + registerHelper('lint', 'css', (text: string, ...args: unknown[]) => + cssLint(text.replace(/(?\d)fr\b/gi, '$1px'), ...args)) +} diff --git a/tests/e2e/css-lint-fr-units.spec.ts b/tests/e2e/css-lint-fr-units.spec.ts new file mode 100644 index 000000000..16ede1fdb --- /dev/null +++ b/tests/e2e/css-lint-fr-units.spec.ts @@ -0,0 +1,26 @@ +import { expect, test } from '@playwright/test' +import { SnippetsTestHelper } from './helpers/SnippetsTestHelper' +import { URLS } from './helpers/constants' + +const LINT_ERROR = '.snippet-editor .CodeMirror-lint-mark-error' + +test.describe('CSS snippet linting', () => { + test('grid fr units are not reported as errors', async ({ page }) => { + const helper = new SnippetsTestHelper(page) + await page.goto(URLS.ADD_SNIPPET_ADMIN) + + // A real syntax error first, so the empty check below cannot pass before the linter has run. + await helper.fillSnippetForm({ + name: SnippetsTestHelper.makeUniqueSnippetName(), + type: 'CSS', + code: '.a {\n\tcolor: red;\n' + }) + await expect(page.locator(LINT_ERROR).first()).toBeVisible() + + await helper.fillSnippetForm({ + name: SnippetsTestHelper.makeUniqueSnippetName(), + code: '.a {\n\tgrid-template-columns: minmax(0, 1fr) repeat(2, 1fr);\n}\n' + }) + await expect(page.locator(LINT_ERROR)).toHaveCount(0) + }) +})