Skip to content

Commit

Permalink
improvement: replace CSS custom properties more efficiently (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladitasev authored Mar 19, 2019
1 parent 0365aed commit e27c445
Showing 1 changed file with 3 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ const findCSSVars = styleString => {
};

/**
* Replaces all occurrences of CSS vars with their values (and fallback values)
* Replaces all occurrences of CSS vars with their values (or fallback values)
* @param styleString - string containing CSS selectors
* @returns {*}
*/
const applyCSSVars = styleString => {
// Replace all variables, with or without default value (default value removed too)
vars.forEach((varValue, varName) => {
const re = new RegExp(`var\\(\\s*${varName}.*?\\)`, "g");
styleString = styleString.replace(re, varValue);
styleString = styleString.replace(/var\(\s*([^,]+),?\s*(.*?)?\)/g, (match, name, fallback) => {
return vars.get(name) || fallback || "";
});

// Replace all unresolved variables with their default values
styleString = styleString.replace(/var\(.*?,\s*(.*?)\)/g, "$1");

return styleString;
};

Expand Down

0 comments on commit e27c445

Please sign in to comment.