forked from peterbe/minimalcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
50 lines (48 loc) · 1.4 KB
/
utils.js
File metadata and controls
50 lines (48 loc) · 1.4 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Take call "important comments" and extract them all to the
* beginning of the CSS string.
* This makes it possible to merge when minifying across blocks of CSS.
* For example, if you have (ignore the escaping for the sake of demonstration):
*
* /*! important 1 *\/
* p { color: red; }
* /*! important 2 *\/
* p { background-color: red; }
*
* You can then instead get:
*
* /*! important 1 *\/
* /*! important 2 *\/
* p { color: red; background-color: red; }
*
* @param {string} css
* @return {string}
*/
const collectImportantComments = css => {
const once = new Set()
let cleaned = css.replace(/\/\*\![\s\S]*?\*\/\n*/gm, match => {
once.add(match)
return ''
})
let combined = Array.from(once)
combined.push(cleaned)
return combined.join('\n')
}
/**
* 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]
}
module.exports = { collectImportantComments, reduceCSSSelector }