All files / minimalcss/src utils.js

33.33% Statements 3/9
0% Branches 0/5
0% Functions 0/2
33.33% Lines 3/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                        1x                     1x                 1x  
/**
 * Reduce a CSS selector to be without any pseudo class parts.
 * For example, from `a:hover` return `a`. And from `input::-moz-focus-inner`
 * to `input`.
 * Also, more advanced ones like `a[href^="javascript:"]:after` to
 * `a[href^="javascript:"]`.
 * The last example works too if the input was `a[href^='javascript:']:after`
 * instead (using ' instead of ").
 *
 * @param {string} selector
 * @return {string}
 */
const reduceCSSSelector = selector => {
  return selector.split(
    /:(?=([^"'\\]*(\\.|["']([^"'\\]*\\.)*[^"'\\]*['"]))*[^"']*$)/g
  )[0]
}
 
/**
 * Remove the ' and/or " at the beginning and end of a string if it has it.
 * @param {string} string
 * @return {string}
 */
const unquoteString = string => {
  const first = string.charAt(0)
  const last = string.charAt(string.length - 1)
  if (first === last && (first === '"' || first === "'")) {
    return string.substring(1, string.length - 1)
  }
  return string
}
 
module.exports = { reduceCSSSelector, unquoteString }