pretty-lights
Version:
CSS-in-JS with a reliable API
83 lines (65 loc) • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function generateStyleTag(cssKey, ids, styles, nonceString) {
return "<style data-lights-".concat(cssKey, "=\"").concat(ids.substring(1), "\"").concat(nonceString, ">").concat(styles, "</style>");
}
var createRenderStylesToString = function createRenderStylesToString(cache) {
return function (html) {
var nonceString = cache.nonce !== undefined ? " nonce=\"".concat(cache.nonce, "\"") : '';
var inserted = cache.inserted,
cssKey = cache.key,
registered = cache.registered;
var regex = new RegExp("<|".concat(cssKey, "-([a-zA-Z0-9-_]+)"), 'gm');
var seen = {};
var result = '';
var globalIds = '';
var globalStyles = ''; // eslint-disable-next-line no-restricted-syntax
for (var id in inserted) {
// eslint-disable-next-line no-prototype-builtins
if (inserted.hasOwnProperty(id)) {
var style = inserted[id];
var key = "".concat(cssKey, "-").concat(id);
if (style !== true && registered[key] === undefined) {
globalStyles += style;
globalIds += " ".concat(id);
}
}
}
if (globalStyles !== '') {
result = generateStyleTag(cssKey, globalIds, globalStyles, nonceString);
}
var ids = '';
var styles = '';
var lastInsertionPoint = 0;
var match; // eslint-disable-next-line no-cond-assign
while ((match = regex.exec(html)) !== null) {
// $FlowFixMe
if (match[0] === '<') {
if (ids !== '') {
result += generateStyleTag(cssKey, ids, styles, nonceString);
ids = '';
styles = '';
} // $FlowFixMe
result += html.substring(lastInsertionPoint, match.index); // $FlowFixMe
lastInsertionPoint = match.index; // eslint-disable-next-line no-continue
continue;
} // $FlowFixMe
var _id = match[1];
var _style = inserted[_id];
if (_style === true || _style === undefined || seen[_id]) {
// eslint-disable-next-line no-continue
continue;
}
seen[_id] = true;
styles += _style;
ids += " ".concat(_id);
}
result += html.substring(lastInsertionPoint);
return result;
};
};
var _default = createRenderStylesToString;
exports["default"] = _default;