Repository URL to install this package:
|
Version:
1.2.13 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.fixGlobalPlaceholders = exports.splitByPlaceholders = exports.containsPlaceholders = exports.temporaryClassname = exports.makePlaceholder = exports.isUnendedMixin = void 0; // The capture group makes sure that the split contains the interpolation index
// Alternative regex that splits without a capture group
const placeholderRegex = /__PLACEHOLDER_(\d+?)__/; // This matches the global group w/o a selector
const placeholderNonCapturingRegex = /__PLACEHOLDER_(?:\d+?)__/;
const globalRulesetRegex = /^{([^}]*)}/;
/**
* Return position of needle in string or Infinity
*/
function lastInArr(arr) {
return arr[arr.length - 1];
}
/**
* Looks if the CSS partial after mixin needs to be prefixed with a semicolon
*/
function findIndex(string, needle) {
let index = string.indexOf(needle);
return index > -1 ? index : Infinity;
}
function isUnendedMixin(css) {
const newlinePos = findIndex(css, '\n');
const semicolonPos = findIndex(css, ';');
const colonPos = findIndex(css, ':');
const openParensPos = findIndex(css, '{');
const closingParensPos = findIndex(css, '}'); // If newline isn't first, prefixed interpolation can't be a mixin
const isNewlineFirst = isFinite(newlinePos) && newlinePos === Math.min(newlinePos, semicolonPos, colonPos, openParensPos, closingParensPos);
if (!isNewlineFirst) {
return false;
} // If this is followed by a semicolon or colon, then we don't want to add a semicolon
const minCharPos = Math.min(semicolonPos, colonPos, openParensPos, closingParensPos); // Generates a placeholder from an index
return isFinite(minCharPos) && minCharPos !== semicolonPos;
} // Our temporary classname
function makePlaceholder(index) {
return '__PLACEHOLDER_'.concat(index, '__');
} // Checks whether the CSS already contains something that matches our placeholders
const temporaryClassname = '__TEMPORARY_CLASSNAME__'; // Splits CSS by placeholders
function containsPlaceholders(css) {
return !!css.match(placeholderRegex);
} // Remove curly braces around global placeholders
// We need to replace mixin-semicolons with newlines to not break browser CSS parsing
function splitByPlaceholders(css) {
const capture = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return css.split(capture ? placeholderRegex : placeholderNonCapturingRegex);
}
function fixGlobalPlaceholders(css) {
return css.replace(globalRulesetRegex, function (_, p1) {
return p1 ? p1.replace(';', '\n') : '';
});
}
exports.makePlaceholder = makePlaceholder;
exports.temporaryClassname = temporaryClassname;
exports.containsPlaceholders = containsPlaceholders;
exports.splitByPlaceholders = splitByPlaceholders;
exports.fixGlobalPlaceholders = fixGlobalPlaceholders;
exports.lastInArr = lastInArr;
exports.isUnendedMixin = isUnendedMixin;