diff --git a/src/js/entries/editor.ts b/src/js/entries/editor.ts index bc6774dcd..e85ecb559 100644 --- a/src/js/entries/editor.ts +++ b/src/js/entries/editor.ts @@ -1,5 +1,6 @@ import { defineMode, getMode, registerHelper } from 'codemirror' import { Linter } from '../utils/Linter' +import { lintCss } from '../utils/cssLint' import type { EditorConfiguration, ModeSpec } from 'codemirror' interface ModeSpecOptions { @@ -19,3 +20,7 @@ registerHelper('lint', 'php', (text: string) => { return linter.annotations }) + +// Replaces the CSS helper from the WordPress CodeMirror bundle, which reports the fr unit and custom properties as errors. +registerHelper('lint', 'css', (text: string, options: unknown) => + 'CSSLint' in window ? lintCss((<{ CSSLint: Parameters[0] }>window).CSSLint, text, options) : []) diff --git a/src/js/utils/cssLint.ts b/src/js/utils/cssLint.ts new file mode 100644 index 000000000..84c8b0a40 --- /dev/null +++ b/src/js/utils/cssLint.ts @@ -0,0 +1,47 @@ +import CodeMirror from 'codemirror' + +interface CSSLintMessage { + type: string + line: number + col: number + message: string + rule: { id: string } +} + +interface CSSLintInstance { + verify: (text: string, ruleset?: unknown) => { messages: CSSLintMessage[] } +} + +export interface CssLintAnnotation { + message: string + severity: string + from: CodeMirror.Position + to: CodeMirror.Position +} + +/** + * CSSLint 1.0.5, the version WordPress ships, predates the fr unit and custom properties. Its parser stops at + * '1fr' and at '--name', and reports a parse error for valid CSS. Both are rewritten here, keeping every + * character position, into a form the parser accepts: '1fr' becomes '1px', and '--name' becomes '-xname', + * which CSSLint reads as a vendor-prefixed property and does not validate. + */ +export const maskModernCss = (code: string): string => + code + .replace(/(?\d)fr\b/g, '$px') + .replace(/--(?=[A-Za-z_])/g, '-x') + +/** + * CSSLint cannot resolve var(), so it reports a property that uses one as having an invalid value. + */ +const isVarFalsePositive = (message: CSSLintMessage): boolean => + 'known-properties' === message.rule.id && message.message.includes('var(') + +export const lintCss = (cssLint: CSSLintInstance, text: string, options?: unknown): CssLintAnnotation[] => + cssLint.verify(maskModernCss(text), options).messages + .filter(message => !isVarFalsePositive(message)) + .map(message => ({ + message: message.message, + severity: message.type, + from: CodeMirror.Pos(message.line - 1, message.col - 1), + to: CodeMirror.Pos(message.line - 1, message.col) + })) diff --git a/tests/e2e/css-lint.spec.ts b/tests/e2e/css-lint.spec.ts new file mode 100644 index 000000000..cac7a3ec1 --- /dev/null +++ b/tests/e2e/css-lint.spec.ts @@ -0,0 +1,44 @@ +import { expect, test } from '@playwright/test' +import { SELECTORS, TIMEOUTS, URLS } from './helpers/constants' +import type { Page } from '@playwright/test' + +interface LintResult { + message: string + severity: string +} + +const lintCss = async (page: Page, css: string): Promise => + page.evaluate(text => { + const { wp } = <{ wp: { + CodeMirror: { helpers: { lint: { css: (text: string, options: unknown) => LintResult[] } } } + codeEditor: { defaultSettings: { csslint: unknown } } + } }>window + + // The same CSSLint ruleset WordPress passes to the editor. + return wp.CodeMirror.helpers.lint.css(text, wp.codeEditor.defaultSettings.csslint) + .map(({ message, severity }) => ({ message, severity })) + }, css) + +test.describe('CSS snippet linting', () => { + test.beforeEach(async ({ page }) => { + await page.goto(URLS.ADD_SNIPPET_ADMIN) + await page.waitForSelector(SELECTORS.CODE_MIRROR_TEXTAREA, { timeout: TIMEOUTS.DEFAULT }) + }) + + test('The fr unit is not reported as an error', async ({ page }) => { + expect(await lintCss(page, '.grid { grid-template-columns: minmax(0, 1fr) 2fr; }')).toEqual([]) + }) + + test('Custom properties and var() are not reported', async ({ page }) => { + const css = ':root { --main-color: #fff; --gap: 4px; }\n.box { color: var(--main-color); margin: var(--gap, 2px); }' + expect(await lintCss(page, css)).toEqual([]) + }) + + test('Real errors are still reported', async ({ page }) => { + expect(await lintCss(page, '.box { colr: red; }')) + .toContainEqual({ message: "Unknown property 'colr'.", severity: 'warning' }) + + const unclosed = await lintCss(page, '.open { color: red;') + expect(unclosed.some(({ severity }) => 'error' === severity)).toBe(true) + }) +})