MCPcopy Create free account
hub / github.com/prettier/prettier / parseSelector

Function parseSelector

src/language-css/parse/parse-selector.js:4–36  ·  view source on GitHub ↗
(selector)

Source from the content-addressed store, hash-verified

2import { addTypePrefix } from "./utilities.js";
3
4function parseSelector(selector) {
5 // If there's a comment inside of a selector, the parser tries to parse
6 // the content of the comment as selectors which turns it into complete
7 // garbage. Better to print the whole selector as-is and not try to parse
8 // and reformat it.
9 if (/\/[/*]/.test(selector.replaceAll(/"[^"]+"|'[^']+'/g, ""))) {
10 return {
11 type: "selector-unknown",
12 value: selector.trim(),
13 };
14 }
15
16 let result;
17
18 try {
19 new PostcssSelectorParser((selectors) => {
20 result = selectors;
21 }).process(selector);
22 } catch {
23 // Fail silently. It's better to print it as is than to try and parse it
24 // Note: A common failure is for SCSS nested properties. `background:
25 // none { color: red; }` is parsed as a NestedDeclaration by
26 // postcss-scss, while `background: { color: red; }` is parsed as a Rule
27 // with a selector ending with a colon. See:
28 // https://github.com/postcss/postcss-scss/issues/39
29 return {
30 type: "selector-unknown",
31 value: selector,
32 };
33 }
34
35 return addTypePrefix(result, "selector-");
36}
37
38export default parseSelector;

Callers 2

parseNestedCSSFunction · 0.85
parseValueNodeFunction · 0.85

Calls 3

addTypePrefixFunction · 0.90
testMethod · 0.45
trimMethod · 0.45

Tested by

no test coverage detected