Repository URL to install this package:
|
Version:
1.1.21 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hasNoSelectors = hasNoSelectors;
exports.handleSelector = handleSelector;
exports.getSelectors = getSelectors;
exports.hasSelectors = hasSelectors;
exports.matchSelectors = matchSelectors;
exports.fromSelectorsAndStylesToBlock = fromSelectorsAndStylesToBlock;
exports.isClassName = void 0;
var _exotic = require("../../exotic");
const matchParen = /{([^}]*)}/gm;
const matchIdOrClass = /([#|\.]?)([\w|:|\s|\.]+)/gim;
/**
* @param {String} styleString
* @param {*} Target
* @throws {Error} if has no selectors
*/
function handleSelector(styleString, Target) {
if (hasNoSelectors(styleString)) {
throw new Error('@TODO connect auto-id: ' + styleString);
}
}
/**
* @param {String} css
* @return {Boolean}
*/
function hasZeroSimpleSelectors(css) {
return !css.includes('.') && !css.includes('#') && !css.includes('[') && !css.includes('=') && !css.includes('\n') && !css.includes(';') && !css.includes(':');
}
/**
* @param {String} css
* @return {Array}
*/
function matchSelectors(css) {
// hardcoding with only checking class & id selectors
if (hasZeroSimpleSelectors(css)) {
return [];
}
return css.replace(matchParen, '{}').match(matchIdOrClass) || [];
}
/**
* @param {String | Function | Object | React.Component} css
* @return {String | Array | Boolean}
*/
function getSelectorsFromComponent(css) {
console.log({
css,
has: (0, _exotic.isObj)(css) && (0, _exotic.isFunction)(css.getSelector),
getSelector: css.getSelector
});
return (0, _exotic.isObj)(css) && (0, _exotic.isFunction)(css.getSelector) ? css.getSelector() : false;
}
/**
* @param {String} css
* @return {Array}
*/
function getSelectors(css) {
return (0, _exotic.isString)(css) ? matchSelectors(css) : (0, _exotic.isObj)(css) ? getSelectorsFromComponent(css) : [];
}
/**
* @extends getSelectors
*
* @param {String} css
* @return {Array}
*/
function hasSelectors(css) {
return getSelectors(css).length !== 0;
}
/**
* @extends hasSelectors
*
* @param {String} css
* @return {Array}
*/
function hasNoSelectors(css) {
return !hasSelectors(css);
}
/**
* @see exotic.isString
* @see view-container.hasZeroSimpleSelector
* @param {CSSSelector | String | *} x
* @return {Boolean}
*/
const isClassName = x => (0, _exotic.isString)(x) && hasZeroSimpleSelectors(x);
/**
* @param {String} selectors selector to scope css under
* @param {String | CSS} css styles
* @return {String | CSS}
*/
exports.isClassName = isClassName;
function fromSelectorsAndStylesToBlock(selectors, css) {
return `
${selectors} {
${css}
}
`;
}