@stylable/core
Version:
CSS for Components
30 lines (25 loc) • 862 B
text/typescript
import type { FunctionNode } from 'postcss-value-parser';
export const PROPERTY = `-st-global`;
export const GLOBAL_FUNC = 'st-global';
const globalValueRegExp = new RegExp(`^${GLOBAL_FUNC}\\((.*?)\\)$`);
export function globalValue(str: string) {
const match = str.match(globalValueRegExp);
return match?.[1];
}
export function globalValueFromFunctionNode(funcNode: FunctionNode) {
if (funcNode.value !== GLOBAL_FUNC) {
return;
}
let globalValue = '';
let unknownInput = false;
for (const { type, value } of funcNode.nodes) {
if (type === 'word') {
globalValue = value;
} else if (type === 'comment' || type === 'space') {
// allow comments & spaced: do nothing
} else {
unknownInput = true;
}
}
return unknownInput ? undefined : globalValue;
}