Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/js/entries/editor.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -19,3 +21,11 @@ registerHelper('lint', 'php', (text: string) => {

return linter.annotations
})

const cssLint = (<{ lint?: Record<string, LintHelper> }> <unknown> 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(/(?<digit>\d)fr\b/gi, '$1px'), ...args))
}
26 changes: 26 additions & 0 deletions tests/e2e/css-lint-fr-units.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading