diff --git a/src/__tests__/exceptions.mjs b/src/__tests__/exceptions.mjs index 358382a..5555900 100644 --- a/src/__tests__/exceptions.mjs +++ b/src/__tests__/exceptions.mjs @@ -7,6 +7,13 @@ throws("unclosed pseudo element", "button::"); throws("unclosed pseudo class", "a:"); throws("unclosed attribute selector", '[name="james"][href'); +for (const selector of [".", "a.", ".a..b", "#a.", ":not(.)", "a > ."]) { + throws(`empty class name in ${selector}`, selector, "Expected a class name."); +} +for (const selector of ["#", "a#", "#a#", ".a#", ":is(#)", "a, #"]) { + throws(`empty ID in ${selector}`, selector, "Expected an id."); +} + // Constructs left open at end of input. These threw a raw TypeError before // `attribute`, `namespace` and `parentheses` guarded against running out of // tokens. Asserted by message rather than by type: the default diff --git a/src/parser.js b/src/parser.js index 9206c8b..badab9f 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1154,6 +1154,9 @@ export default class Parser { const sourceIndex = current[TOKEN.START_POS] + indices[i]; const source = getSource(current[1], current[2] + ind, current[3], current[2] + (index - 1)); if (classIndexes.has(ind)) { + if (value.length === 1) { + return this.expected("class name", sourceIndex); + } let classNameOpts = { value: value.slice(1), source, @@ -1161,6 +1164,9 @@ export default class Parser { }; node = new ClassName(unescapeProp(classNameOpts, "value")); } else if (idIndexes.has(ind)) { + if (value.length === 1) { + return this.expected("id", sourceIndex); + } let idOpts = { value: value.slice(1), source,