Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public rewriteSelector(
selector: string,
usageMapping: Record,
globals: Record = {}
) {
const ast = parseSelector(selector);
traverseNode(ast, node => {
if (node.type === 'class' && !globals[node.name]) {
const isState = Object.keys(usageMapping).some(namespace => {
return node.name.startsWith(
'' + namespace + pseudoStates.booleanStateDelimiter
);
});
if (!isState) {
// is not a state
if (!this.context.names[node.name]) {
this.generateName(node.name);
}
node.name = this.context.names[node.name];
}
}
});
private isContainsUnusedParts(
selectorAst: SelectorAstNode,
usageMapping: Record,
matchNamespace: RegExp
) {
// TODO: !!-!-!! last working point
let isContainsUnusedParts = false;
traverseNode(selectorAst, node => {
if (isContainsUnusedParts) {
return false;
}
if (node.type === 'class') {
const parts = matchNamespace.exec(node.name);
if (parts) {
if (usageMapping[parts[1]] === false) {
isContainsUnusedParts = true;
}
}
} else if (node.type === 'nested-pseudo-element') {
return false;
}
return undefined;
});
return isContainsUnusedParts;
public scopeSelector(selector?: string): string {
if (!selector) {
return this.scopeSelector('.root');
}
const ast = parseSelector(selector);
traverseNode(ast, (node: any) => {
if (node.type === 'class') {
node.name = this.stylesheet.classes[node.name] || node.name;
} else if (node.type === 'pseudo-class') {
const param = node.content;
if (!param) {
node.type = 'class';
node.name = this.stylesheet.cssStates({ [node.name]: true });
} else {
const state = this.stylesheet.cssStates({ [node.name]: param });
if (isValidClassName(param)) {
node.type = 'class';
node.name = state;
} else {
node.type = 'attribute';
node.content = state;
}
function hasStates(selector: SelectorAstNode, context: AddForceStateSelectorsContext) {
let hasStates = false;
traverseNode(selector, node => {
if (node.type === 'pseudo-class') {
return (hasStates = true);
} else if (node.type === 'class' && context.isStateClassName(node.name)) {
return (hasStates = true);
} else if (node.type === 'attribute' && node.content && context.isStateAttr(node.content)) {
return (hasStates = true);
}
return undefined;
});
return hasStates;
}
function transformStates(selector: SelectorAstNode, context: AddForceStateSelectorsContext) {
traverseNode(selector, node => {
if (node.type === 'pseudo-class') {
node.type = 'attribute';
node.content = isNative(node.name)
? context.getForceStateAttrContentFromNative(node.name)
: context.getForceStateAttrContent(node.name);
context.onMapping(node.name, node.content);
} else if (node.type === 'class' && context.isStateClassName(node.name)) {
node.type = 'attribute';
const name = context.getStateClassName(node.name);
node.content = context.getForceStateAttrContent(name);
context.onMapping(name, node.content);
} else if (node.type === 'attribute' && node.content && context.isStateAttr(node.content)) {
node.type = 'attribute';
const name = context.getStateAttr(node.content);
node.content = context.getForceStateAttrContent(name);