Skip to content

Commit 2c11f68

Browse files
committed
feat: css validation api
1 parent afc3e63 commit 2c11f68

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ retrieving CSS selectors(document symbols such as classes, IDs, and other select
8989

9090
### Methods
9191

92-
#### `getAllSymbols(text, cssMode, filePath)`
92+
### `getAllSymbols(text, cssMode, filePath)`
9393

9494
Retrieves all CSS selectors from the provided CSS text as an array of strings.
9595

@@ -111,6 +111,52 @@ const selectors = CSSLanguageService.getAllSymbols(cssText, CSSLanguageService.C
111111
console.log(selectors); // Output: [".class", "#id"]
112112
```
113113

114+
### `validateCSS(text, cssMode, filePath, lintSettings)`
115+
116+
Validates CSS code using specified settings and returns an array of diagnostic messages.
117+
118+
**Parameters:**
119+
120+
- `text` (string): The CSS code to be validated.
121+
- `cssMode` (string): The CSS mode used for parsing the code.
122+
- `filePath` (string): The path of the CSS file being validated.
123+
- `lintSettings` (object): The lint settings to be used for validation.
124+
125+
Possible keys and their descriptions:
126+
- `compatibleVendorPrefixes`: Unnecessary vendor prefixes checker.
127+
- `vendorPrefix`: Warns on missing vendor prefixes.
128+
- `duplicateProperties`: Flags duplicated CSS properties.
129+
- `emptyRules`: Detects CSS rules that have no properties.
130+
- `importStatement`: Flags the use of @import within CSS files.
131+
- `boxModel`: Warns if CSS box model is potentially misused.
132+
- `universalSelector`: Warns against the use of the universal selector (*).
133+
- `zeroUnits`: Warns when units specification for zero values is unnecessary.
134+
- `fontFaceProperties`: Ensures necessary properties are included in @font-face declarations.
135+
- `hexColorLength`: Enforces consistency in hex color definitions.
136+
- `argumentsInColorFunction`: Validates arguments within color functions.
137+
- `unknownProperties`: Warns on unrecognized or mistyped CSS properties.
138+
- `ieHack`: Warns about CSS hacks for older versions of Internet Explorer.
139+
- `unknownVendorSpecificProperties`: Flags vendor-specific properties that might not be universally recognized.
140+
- `propertyIgnoredDueToDisplay`: Notifies when CSS properties are ignored due to the `display` setting of an element.
141+
- `important`: Warns against the excessive use of `!important`.
142+
- `float`: Advises on the use of `float`, recommending modern layout alternatives.
143+
- `idSelector`: Advises against using ID selectors for styling.
144+
145+
Each key's value can be "warning" or "error".
146+
147+
**Return Value:**
148+
149+
Returns an `Array` of objects with the following properties:
150+
- `code` (string)
151+
- `source` (string)
152+
- `message` (string)
153+
- `severity` (number)
154+
- `range` (object) which includes:
155+
- `start` (object): contains `line` (number) and `character` (number)
156+
- `end` (object): contains `line` (number) and `character` (number)
157+
158+
These objects represent the diagnostic messages produced during validation.
159+
114160
### Constants
115161

116162
#### `CSSLanguageService.CSS_MODES`

src/css-ls.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export const CSS_MODES = {
77
SCSS: "scss"
88
};
99

10+
export const DiagnosticSeverity = {
11+
error: 1,
12+
warning: 2,
13+
information: 3,
14+
hint: 4
15+
};
16+
1017
function getTextDocument(text, languageID, filePath = "file://placeholder.css") {
1118
return cssLangService.TextDocument.create(filePath, languageID, 1, text);
1219
}
@@ -27,3 +34,56 @@ export function getAllSymbols(text, cssMode, filePath) {
2734
}
2835
return output;
2936
}
37+
38+
/**
39+
* Validates CSS code using specified settings and returns array of diagnostic messages.
40+
*
41+
* @param {string} text - The CSS code to be validated.
42+
* @param {string} cssMode - The CSS mode used for parsing the code.
43+
* @param {string} filePath - The path of the CSS file being validated.
44+
* @param {Object} lintSettings - The lint settings to be used for validation. Keys may include:
45+
* - "compatibleVendorPrefixes": Unnecessary vendor prefixes checker.
46+
* - "vendorPrefix": Warns on missing vendor prefixes.
47+
* - "duplicateProperties": Flags duplicated CSS properties.
48+
* - "emptyRules": Detects CSS rules that have no properties.
49+
* - "importStatement": Flags the use of @import within CSS files.
50+
* - "boxModel": Warns if CSS box model is potentially misused.
51+
* - "universalSelector": Warns against the use of the universal selector (*).
52+
* - "zeroUnits": Warns when units specification for zero values is unnecessary.
53+
* - "fontFaceProperties": Ensures necessary properties are included in @font-face declarations.
54+
* - "hexColorLength": Enforces consistency in hex color definitions.
55+
* - "argumentsInColorFunction": Validates arguments within color functions.
56+
* - "unknownProperties": Warns on unrecognized or mistyped CSS properties.
57+
* - "ieHack": Warns about CSS hacks for older versions of Internet Explorer.
58+
* - "unknownVendorSpecificProperties": Flags vendor-specific properties that might not be universally recognized.
59+
* - "propertyIgnoredDueToDisplay": Notifies when CSS properties are ignored due to the `display` setting of an element.
60+
* - "important": Warns against the excessive use of `!important`.
61+
* - "float": Advises on the use of `float`, recommending modern layout alternatives.
62+
* - "idSelector": Advises against using ID selectors for styling.
63+
* Each key's value can be "warning" or "error".
64+
*
65+
* @returns {Array.<{
66+
* code: string,
67+
* source: string,
68+
* message: string,
69+
* severity: number,
70+
* range: {
71+
* start: {
72+
* line: number,
73+
* character: number
74+
* },
75+
* end: {
76+
* line: number,
77+
* character: number
78+
* }
79+
* }
80+
* }>} diagnosticMessages An array of diagnostic messages produced during validation.
81+
*/
82+
export function validateCSS(text, cssMode, filePath, lintSettings) {
83+
const textDocument = getTextDocument(text, cssMode, filePath);
84+
const stylesheet = service.parseStylesheet(textDocument);
85+
return service.doValidation(textDocument, stylesheet, {
86+
validate: true,
87+
lint: lintSettings
88+
});
89+
}

src/worker.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import {getAllSymbols, CSS_MODES} from "./css-ls";
1+
import {getAllSymbols, CSS_MODES, validateCSS, DiagnosticSeverity} from "./css-ls";
22
import {HTML_MODES, getAllDocumentLinks} from "./html-ls";
33

44
self.CSSLanguageService = {
55
getAllSymbols,
6-
CSS_MODES
6+
validateCSS,
7+
CSS_MODES,
8+
DiagnosticSeverity
79
};
810

911
self.HTMLLanguageService = {
1012
getAllDocumentLinks,
11-
HTML_MODES
13+
HTML_MODES,
1214
};

0 commit comments

Comments
 (0)