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
4 changes: 2 additions & 2 deletions src/grammars/assembly.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default {
},
strings: {
patterns: [
{ match: "'(?:\\\\.|[^'\\\\])*'", name: "string.quoted.single" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" }
{ match: "'(?:\\\\.|[^'\\\\\\r\\n])*'", name: "string.quoted.single" },
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" }
]
},
"label-declaration": {
Expand Down
6 changes: 3 additions & 3 deletions src/grammars/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
name: "keyword.control.directive.include",
patterns: [
{ match: "<[^>]*>", name: "string.quoted.other.lt-gt" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" }
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" }
]
},
{
Expand All @@ -39,8 +39,8 @@ export default {
},
strings: {
patterns: [
{ match: "'(?:\\\\.|[^'\\\\])'", name: "string.quoted.char" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" }
{ match: "'(?:\\\\.|[^'\\\\\\r\\n])'", name: "string.quoted.char" },
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" }
]
},
"storage-types": {
Expand Down
6 changes: 3 additions & 3 deletions src/grammars/csharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default {
{ match: "@\"(?:\"\"|[^\"])*\"", name: "string.quoted.double.verbatim" },
{ match: "\\$@\"(?:\"\"|\\{\\{|\\}\\}|[^\"])*\"", name: "string.quoted.double.interpolated" },
{ match: "@\\$\"(?:\"\"|\\{\\{|\\}\\}|[^\"])*\"", name: "string.quoted.double.interpolated" },
{ match: "\\$\"(?:\\\\.|\\{\\{|\\}\\}|[^\"\\\\])*\"", name: "string.quoted.double.interpolated" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\])'", name: "string.quoted.char" }
{ match: "\\$\"(?:\\\\.|\\{\\{|\\}\\}|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double.interpolated" },
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\\\r\\n])'", name: "string.quoted.char" }
]
},
attributes: { match: "\\[[A-Za-z_][\\w.]*\\]", name: "entity.name.decorator" },
Expand Down
6 changes: 3 additions & 3 deletions src/grammars/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
],
repository: {
comments: { begin: "/\\*", end: "\\*/", name: "comment.block" },
strings: { match: "(['\"])(?:\\\\.|(?!\\1)[^\\\\])*\\1", name: "string.quoted" },
strings: { match: "(['\"])(?:\\\\.|(?!\\1)[^\\\\\\r\\n])*\\1", name: "string.quoted" },
keyframes: {
begin: "(@(?:-\\w+-)?keyframes)\\s+([a-zA-Z_-][\\w-]*)\\s*\\{",
end: "\\}",
Expand All @@ -22,7 +22,7 @@ export default {
patterns: [{ include: "$self" }]
},
"at-rule-block": {
begin: "(@(?:container|document|font-feature-values|layer|media|scope|starting-style|supports)\\b)[^{;]*\\{",
begin: "(@(?:container|counter-style|document|font-face|font-feature-values|font-palette-values|layer|media|page|position-try|scope|starting-style|supports|view-transition)\\b)[^{;]*\\{",
end: "\\}",
beginCaptures: { 1: { name: "keyword.control.at-rule" } },
patterns: [{ include: "$self" }]
Expand All @@ -38,7 +38,7 @@ export default {
]
},
"rule-set": {
begin: "([^\\s@{};<][^@{};<;]*?)\\s*\\{",
begin: "([^\\s@{};<][^@{};<]*?)\\s*\\{",
end: "\\}",
beginCaptures: { 1: { name: "entity.name.selector" } },
patterns: [
Expand Down
4 changes: 2 additions & 2 deletions src/grammars/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default {
patterns: [
{ begin: "r?\"\"\"", end: "\"\"\"", name: "string.quoted.triple.double" },
{ begin: "r?'''", end: "'''", name: "string.quoted.triple.single" },
{ match: "r?\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" },
{ match: "r?'(?:\\\\.|[^'\\\\])*'", name: "string.quoted.single" }
{ match: "r?\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" },
{ match: "r?'(?:\\\\.|[^'\\\\\\r\\n])*'", name: "string.quoted.single" }
]
},
annotations: { match: "@[A-Za-z_][\\w]*", name: "entity.name.decorator" },
Expand Down
2 changes: 1 addition & 1 deletion src/grammars/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
strings: {
patterns: [
{ begin: "\"\"\"", end: "\"\"\"", name: "string.quoted.triple" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" }
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" }
]
},
"type-declaration": {
Expand Down
4 changes: 2 additions & 2 deletions src/grammars/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default {
strings: {
patterns: [
{ begin: "\"\"\"", end: "\"\"\"", name: "string.quoted.triple" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\])'", name: "string.quoted.char" }
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\\\r\\n])'", name: "string.quoted.char" }
]
},
annotations: { match: "@[A-Za-z_][\\w.]*", name: "entity.name.decorator" },
Expand Down
4 changes: 2 additions & 2 deletions src/grammars/kotlin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default {
strings: {
patterns: [
{ begin: "\"\"\"", end: "\"\"\"", name: "string.quoted.triple" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\])'", name: "string.quoted.char" }
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\\\r\\n])'", name: "string.quoted.char" }
]
},
annotations: { match: "@[A-Za-z_][\\w.]*", name: "entity.name.decorator" },
Expand Down
4 changes: 2 additions & 2 deletions src/grammars/lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default {
strings: {
patterns: [
{ begin: "\\[(=*)\\[", end: "\\]\\1\\]", name: "string.quoted.other.multiline" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\])*'", name: "string.quoted.single" }
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" },
{ match: "'(?:\\\\.|[^'\\\\\\r\\n])*'", name: "string.quoted.single" }
]
},
"function-declaration": {
Expand Down
2 changes: 1 addition & 1 deletion src/grammars/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
strings: {
patterns: [
{ begin: "\"\"\"", end: "\"\"\"", name: "string.quoted.triple" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" }
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" }
]
},
attributes: { match: "@[A-Za-z_][\\w]*", name: "entity.name.decorator" },
Expand Down
2 changes: 1 addition & 1 deletion src/grammars/toml.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default {
patterns: [
{ begin: "\"\"\"", end: "\"\"\"", name: "string.quoted.triple.double" },
{ begin: "'''", end: "'''", name: "string.quoted.triple.single" },
{ match: "\"(?:\\\\.|[^\"\\\\])*\"", name: "string.quoted.double" },
{ match: "\"(?:\\\\.|[^\"\\\\\\r\\n])*\"", name: "string.quoted.double" },
{ match: "'[^'\\r\\n]*'", name: "string.quoted.single" }
]
},
Expand Down
4 changes: 3 additions & 1 deletion src/grammars/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export default {
{ match: "^(?:---|\\.\\.\\.)\\s*$|^%YAML\\b.*$", name: "keyword.control.document" },
{ match: "^\\s*(?:-\\s+)?([^#\\s][^\\r\\n:#]*?)(?=\\s*:)", captures: { 1: { name: "entity.name.key" } } },
{ match: "[&*][a-zA-Z_][\\w-]*|![^\\s]+", name: "entity.name.anchor" },
{ match: "(['\"])(?:\\\\.|(?!\\1)[^\\\\])*\\1", name: "string.quoted" },
// Quoted scalars may fold over lines, but only onto more-indented ones, so
// a stray apostrophe in a plain scalar cannot swallow the rest of the file.
{ match: "(['\"])(?:\\\\.|(?!\\1)[^\\\\\\r\\n]|\\r?\\n[ \\t]+)*\\1", name: "string.quoted" },
{ match: "(?<=:\\s)[|>][-+]?\\s*$", name: "keyword.control.block-scalar" },
{ match: "\\b(?:true|false|yes|no|on|off)\\b", name: "constant.language.boolean" },
{ match: "\\bnull\\b|~", name: "constant.language" },
Expand Down
102 changes: 102 additions & 0 deletions test/highlight.spec.pw.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ const readHighlights = page =>
};
});

/**
* Highlight a single injected code block and read back its ranges by category.
*/
const highlightSnippet = (page, language, code) =>
page.evaluate(async ({ language, code }) => {
const root = document.createElement("div");
const block = document.createElement("code");
const pre = document.createElement("pre");
block.className = `language-${language}`;
block.textContent = code;
pre.append(block);
root.append(pre);
document.body.append(root);

const { highlightAll } = await import("/docs/microlighter/index.js");
await highlightAll({ root });

const ranges = {};
for (const [category, highlight] of CSS.highlights) {
for (const range of highlight) {
if (range.startContainer.parentElement !== block) continue;
(ranges[category] ??= []).push(range.toString());
}
}
return ranges;
}, { language, code });

test.describe("MicroLighter demo site (docs/index.html)", () => {
test("registers highlight ranges across every code block", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });
Expand Down Expand Up @@ -231,6 +258,81 @@ test.describe("MicroLighter demo site (docs/index.html)", () => {
expect(sampleLanguages).toEqual(["html", "markdown", "python", "sql", "cpp", "tsx"]);
});

test("stops unterminated quotes from swallowing later lines", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });

const css = await highlightSnippet(page, "css", [
"a { font-family: Don't; }",
'b { content: "kept"; }',
"c { font-family: Won't; }"
].join("\n"));
const yaml = await highlightSnippet(page, "yaml", [
"first: don't",
'second: "kept"',
"third: won't"
].join("\n"));

expect(css.string).toEqual(['"kept"']);
expect(yaml.string).toEqual(['"kept"']);
});

test("terminates single-line string literals at the end of the line", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });

// Languages whose grammars only allow multi-line strings through a
// dedicated triple-quoted or long-bracket rule.
const languages = [
"assembly", "c", "csharp", "css", "dart", "graphql",
"java", "kotlin", "lua", "swift", "toml"
];
const unexpected = {};

for (const language of languages) {
const ranges = await highlightSnippet(page, language, 'first = \'x\nsecond = "kept"\nthird = \'y\'\n');
const strings = ranges.string ?? [];
if (!strings.includes('"kept"') || strings.some(value => value.includes("\n"))) {
unexpected[language] = strings;
}
}

expect(unexpected).toEqual({});
});

test("keeps highlighting yaml quoted scalars folded over several lines", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });

const ranges = await highlightSnippet(page, "yaml", 'key: "folded\n continuation"\nnext: 1\n');

expect(ranges.string).toEqual(['"folded\n continuation"']);
});

test("categorizes block at-rules as at-rule keywords, not selectors", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });

const ranges = await highlightSnippet(page, "css", [
'@font-face { font-family: "Example"; }',
"@page { margin: 1cm; }",
"@counter-style thumbs { system: cyclic; }",
"@view-transition { navigation: auto; }"
].join("\n"));

expect(ranges["at-rule"]).toEqual([
"@font-face",
"@page",
"@counter-style",
"@view-transition"
]);
expect(ranges.selector ?? []).toEqual([]);
});

test("scopes multi-line selectors without their trailing whitespace", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });

const ranges = await highlightSnippet(page, "css", "a,\nb\n{\n color: red;\n}\n");

expect(ranges.selector).toEqual(["a,\nb"]);
});

test("highlights a large stylesheet without pathological rescanning", async ({ page }) => {
await page.goto(HOMEPAGE, { waitUntil: "networkidle" });

Expand Down
Loading