-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.js
91 lines (74 loc) · 2.65 KB
/
main.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var postcss = require("postcss");
(function () {
"use strict";
/**
* @param {String} prefixSelector
* @param {Object} options
*/
function PostCSSPrefixWrap(prefixSelector, options) {
this.anyWhitespaceAtBeginningOrEnd = /(^\s*|\s*$)/g;
this.isPrefixSelector = new RegExp("^\s*" + prefixSelector + ".*$");
this.isRootTag = /^(body|html).*$/;
this.prefixRootTags = options !== undefined && options.hasOwnProperty("prefixRootTags") ? options.prefixRootTags : false;
this.prefixSelector = prefixSelector;
}
/**
* @param {String} cssSelector
* @returns {Boolean}
*/
PostCSSPrefixWrap.prototype.invalidCSSSelectors = function (cssSelector) {
return cssSelector !== null;
};
/**
* @param cssSelector
* @param cssRule
* @returns {null|String}
*/
PostCSSPrefixWrap.prototype.prefixWrapCSSSelector = function (cssSelector, cssRule) {
var that = this;
var cleanSelector = cssSelector.replace(that.anyWhitespaceAtBeginningOrEnd, "");
if (cleanSelector === "") {
return null;
}
if (cssRule.parent.type === "atrule" && ["keyframes", "-webkit-keyframes", "-moz-keyframes", "-o-keyframes"].indexOf(cssRule.parent.name) !== -1) {
return cleanSelector;
}
// Anything other than a root tag is always prefixed.
if (!cleanSelector.match(that.isRootTag)) {
return that.prefixSelector + " " + cleanSelector;
}
// Handle special case where root tags should be converted into classes rather than being replaced.
if (that.prefixRootTags) {
return that.prefixSelector + " ." + cleanSelector;
}
// HTML and Body elements cannot be contained within our container so lets extract their styles.
return cleanSelector.replace(/^(body|html)/, that.prefixSelector);
};
/**
* @param {Rule} cssRule
*/
PostCSSPrefixWrap.prototype.prefixWrapCSSRule = function (cssRule) {
var that = this;
// We have found our prefix selector, we just want to leave it as it is already.
if (!cssRule.selector.match(that.isPrefixSelector)) {
cssRule.selector = cssRule.selector
.split(",")
.map(function (cssSelector) {
return that.prefixWrapCSSSelector(cssSelector, cssRule);
})
.filter(that.invalidCSSSelectors)
.join(", ");
}
};
PostCSSPrefixWrap.prototype.asPlugin = function () {
var that = this;
return function (css) {
css.walkRules(function (cssRule) {
that.prefixWrapCSSRule(cssRule);
});
};
};
module.exports = postcss.plugin("postcss-prefixwrap", function (prefixSelector, options) {
return new PostCSSPrefixWrap(prefixSelector, options).asPlugin();
});
}());