/** * Tests of the editor's language support (`language.ts`): the language * picked per file extension, and the highlight classes (`lt-syntax-*`) on * the spans CodeMirror draws for PHP, embedded HTML and the other * languages, each coloured by its `--lt-syntax-*` token. */ import { ensureSyntaxTree } from "@codemirror/language"; import { EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { editorExtensions, lutraEditorTheme } from "../../src/components/editor/extensions"; import { languageOf, SYNTAX_TOKENS } from "../../src/components/editor/language"; /** Views to destroy after each test. */ let views: EditorView[] = []; /** jsdom has no layout; give text ranges empty rectangles. */ function stubRangeGeometry(): void { const empty = (): DOMRectList => Object.assign([] as DOMRect[], { item: () => null }); Range.prototype.getClientRects = empty; Range.prototype.getBoundingClientRect = () => new DOMRect(0, 0, 0, 0); } /** * Shows a document in a view with the extensions of a file path, parsed * completely. * * @param path - the file path that picks the language. * @param doc - the text. */ function show(path: string, doc: string): EditorView { const state = EditorState.create({ doc, extensions: editorExtensions(path) }); ensureSyntaxTree(state, state.doc.length, 5000); const view = new EditorView({ state, parent: document.body }); views.push(view); return view; } /** * The text of every span with a highlight class, by token. * * @param view - the view. */ function highlighted(view: EditorView): Record { const out: Record = {}; for (const token of SYNTAX_TOKENS) { const spans = [...view.contentDOM.querySelectorAll(`.lt-syntax-${token}`)]; if (spans.length > 0) { out[token] = spans.map((span) => span.textContent); } } return out; } beforeEach(() => { stubRangeGeometry(); }); afterEach(() => { for (const view of views) { view.destroy(); } views = []; }); describe("languageOf", () => { it.each([ ["src/Controller/Home.php", "php"], ["templates/view.PHTML", "php"], ["legacy\\lib.inc", "php"], ["public/index.html", "html"], ["assets/app.css", "css"], ["assets/app.js", "javascript"], ["assets/app.tsx", "typescript"], ["composer.json", "json"], ["config/services.yaml", "yaml"], [".github/ci.yml", "yaml"], ["README.md", "markdown"], ])("%s is %s", (path, id) => { expect(languageOf(path)).toBe(id); }); it.each(["Makefile", ".env", "notes.txt", "archive.tar.gz", "dir.php/file", ".php", ""])( "%s is plain text", (path) => { expect(languageOf(path)).toBeNull(); }, ); }); describe("highlighting", () => { it("marks PHP tokens with lt-syntax classes", () => { const view = show( "a.php", [ " { const marks = highlighted(show("view.php", '
\n')); expect(marks.tag).toEqual(expect.arrayContaining(["div"])); expect(marks.attribute).toContain("class"); expect(marks.variable).toContain("$name"); }); it.each([ ["composer.json", '{"name": "lutra/demo", "n": 1}', "string", '"lutra/demo"'], ["ci.yml", "on: push # build\n", "comment", "# build"], ["app.ts", "const x: number = 1;\n", "keyword", "const"], ["app.css", "a { color: red; }\n", "tag", "a"], ["README.md", "# Lutra\n", "keyword", " Lutra"], ])("highlights %s", (path, doc, token, text) => { expect(highlighted(show(path, doc))[token]).toContain(text); }); it("leaves plain text unmarked", () => { expect(highlighted(show("notes.txt", "class Foo { $x = 1; }"))).toEqual({}); }); it("colours every highlight class with its theme token", () => { const view = new EditorView({ state: EditorState.create({ extensions: lutraEditorTheme }), parent: document.body }); views.push(view); const css = [...document.querySelectorAll("style")].map((style) => style.textContent).join("\n"); for (const token of SYNTAX_TOKENS) { expect(css).toMatch(new RegExp(`\\.lt-syntax-${token}\\s*\\{\\s*color:\\s*var\\(--lt-syntax-${token}\\)`)); } }); });