(selector)
| 2 | import { addTypePrefix } from "./utilities.js"; |
| 3 | |
| 4 | function 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 | |
| 38 | export default parseSelector; |
no test coverage detected