Permalink
Cannot retrieve contributors at this time
87 lines (78 sloc)
1.97 KB
| /** | |
| * Contains helpers for safely splitting lists of CSS values, | |
| * preserving parentheses and quotes. | |
| * | |
| * @example | |
| * const list = postcss.list | |
| * | |
| * @namespace list | |
| */ | |
| let list = { | |
| split (string, separators, last) { | |
| let array = [] | |
| let current = '' | |
| let split = false | |
| let func = 0 | |
| let quote = false | |
| let escape = false | |
| for (let letter of string) { | |
| if (quote) { | |
| if (escape) { | |
| escape = false | |
| } else if (letter === '\\') { | |
| escape = true | |
| } else if (letter === quote) { | |
| quote = false | |
| } | |
| } else if (letter === '"' || letter === '\'') { | |
| quote = letter | |
| } else if (letter === '(') { | |
| func += 1 | |
| } else if (letter === ')') { | |
| if (func > 0) func -= 1 | |
| } else if (func === 0) { | |
| if (separators.includes(letter)) split = true | |
| } | |
| if (split) { | |
| if (current !== '') array.push(current.trim()) | |
| current = '' | |
| split = false | |
| } else { | |
| current += letter | |
| } | |
| } | |
| if (last || current !== '') array.push(current.trim()) | |
| return array | |
| }, | |
| /** | |
| * Safely splits space-separated values (such as those for `background`, | |
| * `border-radius`, and other shorthand properties). | |
| * | |
| * @param {string} string Space-separated values. | |
| * | |
| * @return {string[]} Split values. | |
| * | |
| * @example | |
| * postcss.list.space('1px calc(10% + 1px)') //=> ['1px', 'calc(10% + 1px)'] | |
| */ | |
| space (string) { | |
| let spaces = [' ', '\n', '\t'] | |
| return list.split(string, spaces) | |
| }, | |
| /** | |
| * Safely splits comma-separated values (such as those for `transition-*` | |
| * and `background` properties). | |
| * | |
| * @param {string} string Comma-separated values. | |
| * | |
| * @return {string[]} Split values. | |
| * | |
| * @example | |
| * postcss.list.comma('black, linear-gradient(white, black)') | |
| * //=> ['black', 'linear-gradient(white, black)'] | |
| */ | |
| comma (string) { | |
| return list.split(string, [','], true) | |
| } | |
| } | |
| module.exports = list |