Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/js/entries/editor.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<typeof lintCss>[0] }><unknown>window).CSSLint, text, options) : [])
47 changes: 47 additions & 0 deletions src/js/utils/cssLint.ts
Original file line number Diff line number Diff line change
@@ -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(/(?<digit>\d)fr\b/g, '$<digit>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)
}))
44 changes: 44 additions & 0 deletions tests/e2e/css-lint.spec.ts
Original file line number Diff line number Diff line change
@@ -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<LintResult[]> =>
page.evaluate(text => {
const { wp } = <{ wp: {
CodeMirror: { helpers: { lint: { css: (text: string, options: unknown) => LintResult[] } } }
codeEditor: { defaultSettings: { csslint: unknown } }
} }><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)
})
})
Loading