Skip to content

Commit 8ef5544

Browse files
committed
fix(css): avoid color decorators inside non-CSS documents (php) (#333351)
1 parent 3aa5403 commit 8ef5544

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

extensions/css-language-features/server/src/cssServer.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
4242
// for open, change and close text document events
4343
documents.listen(connection);
4444

45-
const stylesheets = getLanguageModelCache<Stylesheet>(10, 60, document => getLanguageService(document).parseStylesheet(document));
45+
const stylesheets = getLanguageModelCache<Stylesheet>(10, 60, document => {
46+
// Avoid parsing documents that are not handled by any language service (e.g. plain PHP files).
47+
// Parsing a non-CSS document as CSS produced false positives (colors in selectors). Return
48+
// an empty stylesheet instead of trying to parse arbitrary content.
49+
if (!languageServices[document.languageId]) {
50+
// Create an empty CSS document to produce an empty stylesheet
51+
try {
52+
const emptyDoc = TextDocument.create(document.uri + '.css', 'css', document.version, '');
53+
return languageServices['css'].parseStylesheet(emptyDoc);
54+
} catch (e) {
55+
// Fallback: if creation/parsing fails for any reason, parse the original document
56+
return getLanguageService(document).parseStylesheet(document);
57+
}
58+
}
59+
return getLanguageService(document).parseStylesheet(document);
60+
});
4661
documents.onDidClose(e => {
4762
stylesheets.onDocumentRemoved(e.document);
4863
});
@@ -195,10 +210,17 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
195210
});
196211
}
197212

213+
function isSupportedDocument(document: TextDocument) {
214+
return !!languageServices[document.languageId];
215+
}
216+
198217
connection.onCompletion((textDocumentPosition, token) => {
199218
return runSafeAsync(runtime, async () => {
200219
const document = documents.get(textDocumentPosition.textDocument.uri);
201220
if (document) {
221+
if (!isSupportedDocument(document)) {
222+
return null;
223+
}
202224
const [settings,] = await Promise.all([getDocumentSettings(document), dataProvidersReady]);
203225
const styleSheet = stylesheets.get(document);
204226
const documentContext = getDocumentContext(document.uri, workspaceFolders);
@@ -212,6 +234,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
212234
return runSafeAsync(runtime, async () => {
213235
const document = documents.get(textDocumentPosition.textDocument.uri);
214236
if (document) {
237+
if (!isSupportedDocument(document)) {
238+
return null;
239+
}
215240
const [settings,] = await Promise.all([getDocumentSettings(document), dataProvidersReady]);
216241
const styleSheet = stylesheets.get(document);
217242
return getLanguageService(document).doHover(document, textDocumentPosition.position, styleSheet, settings?.hover);
@@ -224,6 +249,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
224249
return runSafeAsync(runtime, async () => {
225250
const document = documents.get(documentSymbolParams.textDocument.uri);
226251
if (document) {
252+
if (!isSupportedDocument(document)) {
253+
return [];
254+
}
227255
await dataProvidersReady;
228256
const stylesheet = stylesheets.get(document);
229257
return getLanguageService(document).findDocumentSymbols2(document, stylesheet);
@@ -236,6 +264,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
236264
return runSafeAsync(runtime, async () => {
237265
const document = documents.get(documentDefinitionParams.textDocument.uri);
238266
if (document) {
267+
if (!isSupportedDocument(document)) {
268+
return null;
269+
}
239270
await dataProvidersReady;
240271
const stylesheet = stylesheets.get(document);
241272
return getLanguageService(document).findDefinition(document, documentDefinitionParams.position, stylesheet);
@@ -248,6 +279,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
248279
return runSafeAsync(runtime, async () => {
249280
const document = documents.get(documentHighlightParams.textDocument.uri);
250281
if (document) {
282+
if (!isSupportedDocument(document)) {
283+
return [];
284+
}
251285
await dataProvidersReady;
252286
const stylesheet = stylesheets.get(document);
253287
return getLanguageService(document).findDocumentHighlights(document, documentHighlightParams.position, stylesheet);
@@ -261,6 +295,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
261295
return runSafeAsync(runtime, async () => {
262296
const document = documents.get(documentLinkParams.textDocument.uri);
263297
if (document) {
298+
if (!isSupportedDocument(document)) {
299+
return [];
300+
}
264301
await dataProvidersReady;
265302
const documentContext = getDocumentContext(document.uri, workspaceFolders);
266303
const stylesheet = stylesheets.get(document);
@@ -275,6 +312,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
275312
return runSafeAsync(runtime, async () => {
276313
const document = documents.get(referenceParams.textDocument.uri);
277314
if (document) {
315+
if (!isSupportedDocument(document)) {
316+
return [];
317+
}
278318
await dataProvidersReady;
279319
const stylesheet = stylesheets.get(document);
280320
return getLanguageService(document).findReferences(document, referenceParams.position, stylesheet);
@@ -287,6 +327,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
287327
return runSafeAsync(runtime, async () => {
288328
const document = documents.get(codeActionParams.textDocument.uri);
289329
if (document) {
330+
if (!isSupportedDocument(document)) {
331+
return [];
332+
}
290333
await dataProvidersReady;
291334
const stylesheet = stylesheets.get(document);
292335
return getLanguageService(document).doCodeActions2(document, codeActionParams.range, codeActionParams.context, stylesheet);
@@ -299,6 +342,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
299342
return runSafeAsync(runtime, async () => {
300343
const document = documents.get(params.textDocument.uri);
301344
if (document) {
345+
if (!isSupportedDocument(document)) {
346+
return [];
347+
}
302348
await dataProvidersReady;
303349
const stylesheet = stylesheets.get(document);
304350
return getLanguageService(document).findDocumentColors(document, stylesheet);
@@ -311,6 +357,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
311357
return runSafeAsync(runtime, async () => {
312358
const document = documents.get(params.textDocument.uri);
313359
if (document) {
360+
if (!isSupportedDocument(document)) {
361+
return [];
362+
}
314363
await dataProvidersReady;
315364
const stylesheet = stylesheets.get(document);
316365
return getLanguageService(document).getColorPresentations(document, stylesheet, params.color, params.range);
@@ -323,6 +372,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
323372
return runSafeAsync(runtime, async () => {
324373
const document = documents.get(renameParameters.textDocument.uri);
325374
if (document) {
375+
if (!isSupportedDocument(document)) {
376+
return null;
377+
}
326378
await dataProvidersReady;
327379
const stylesheet = stylesheets.get(document);
328380
return getLanguageService(document).doRename(document, renameParameters.position, renameParameters.newName, stylesheet);
@@ -335,6 +387,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
335387
return runSafeAsync(runtime, async () => {
336388
const document = documents.get(params.textDocument.uri);
337389
if (document) {
390+
if (!isSupportedDocument(document)) {
391+
return null;
392+
}
338393
await dataProvidersReady;
339394
return getLanguageService(document).getFoldingRanges(document, { rangeLimit: foldingRangeLimit });
340395
}
@@ -348,6 +403,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
348403
const positions: Position[] = params.positions;
349404

350405
if (document) {
406+
if (!isSupportedDocument(document)) {
407+
return [];
408+
}
351409
await dataProvidersReady;
352410
const stylesheet = stylesheets.get(document);
353411
return getLanguageService(document).getSelectionRanges(document, positions, stylesheet);
@@ -359,6 +417,9 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
359417
async function onFormat(textDocument: TextDocumentIdentifier, range: Range | undefined, options: FormattingOptions): Promise<TextEdit[]> {
360418
const document = documents.get(textDocument.uri);
361419
if (document) {
420+
if (!isSupportedDocument(document)) {
421+
return [];
422+
}
362423
const edits = getLanguageService(document).format(document, range ?? getFullRange(document), options);
363424
if (edits.length > formatterMaxNumberOfEdits) {
364425
const newText = TextDocument.applyEdits(document, edits);

0 commit comments

Comments
 (0)