@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
64 lines • 2.11 kB
JavaScript
/**
* Recognizes simple, parseable selector lists made only from nesting and non-functional pseudos.
* Returning false means the caller must use the full selector parser.
*/
export function isSimpleSelector(selectorText) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var index = 0;
while (index < selectorText.length) {
index = skipWhitespace(selectorText, index);
var hasNesting = selectorText.charCodeAt(index) === 38;
if (hasNesting) {
index++;
if (selectorText.charCodeAt(index) === 38) {
return false;
}
} else if (!options.allowLeadingPseudo || selectorText.charCodeAt(index) !== 58) {
return false;
}
var pseudoCount = 0;
while (selectorText.charCodeAt(index) === 58) {
var pseudoStart = index++;
if (selectorText.charCodeAt(index) === 58) {
index++;
}
var nameStart = index;
while (index < selectorText.length && isPseudoNameCharacter(selectorText.charCodeAt(index))) {
index++;
}
if (index === nameStart || selectorText.charCodeAt(index) === 40) {
return false;
}
if (options.allowedPseudos && !options.allowedPseudos.has(selectorText.slice(pseudoStart, index))) {
return false;
}
pseudoCount++;
}
if (pseudoCount === 0 && (!hasNesting || !options.allowBareNesting)) {
return false;
}
index = skipWhitespace(selectorText, index);
if (index === selectorText.length) {
return true;
}
if (selectorText.charCodeAt(index) !== 44) {
return false;
}
index++;
}
return false;
}
function skipWhitespace(value, start) {
var index = start;
while (index < value.length) {
var character = value.charCodeAt(index);
if (character !== 32 && (character < 9 || character > 13)) {
break;
}
index++;
}
return index;
}
function isPseudoNameCharacter(character) {
return character === 45 || character === 95 || character >= 48 && character <= 57 || character >= 65 && character <= 90 || character >= 97 && character <= 122;
}