This repository has been archived by the owner on Jun 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (124 loc) · 4.87 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
let postcss = require('postcss');
module.exports = postcss.plugin('postcss-mediator', opts => {
opts = opts || {};
let supportedMediaTypes = ['all', 'aural', 'braille', 'handheld', 'print',
'projection', 'screen', 'tty', 'tv', 'embossed'];
let mediaRegexMatch = new RegExp('^((not|only)-)?' +
supportedMediaTypes.join('|') + '$', 'i');
function extractOneMode(mediatorModes, rule) {
if (rule.name === 'mediator') {
let firstSpace = rule.params.indexOf(' ');
let key = rule.params.substr(0, firstSpace);
let value = rule.params.substr(firstSpace + 1);
// Make sure mediator modes are not named after media types
// to avoid possible conflicts
if (mediaRegexMatch.test(key)) {
throw rule.error('Naming a Mediator Mode after a media ' +
'type (all, screen, print, speech) may cause undesired ' +
'conflicts. Please rename it.',
{ plugin: 'postcss-mediator' });
}
mediatorModes[key] = value;
rule.remove();
}
}
function extractModes(css) {
let mediatorModes = {};
let extractor = extractOneMode.bind(this, mediatorModes);
css.walkAtRules(extractor);
return mediatorModes;
}
function extractOneRule(mediatorModes, mediatorOutputRules, decl) {
if (decl.prop.indexOf('.') === -1) {
return;
}
// Breaks the property declaration to extract its modes
let modes = decl.prop.split('.');
// First element is the propety
let prop = modes.shift();
// Check if any of the modes is actually a media type
let mediaTypes = modes.filter(element => {
return mediaRegexMatch.test(element);
});
// If so, remove media types from modes array
if (mediaTypes.length > 0) {
modes = modes.filter(element => {
return mediaTypes.indexOf(element) === -1;
});
} else {
// Default media type
mediaTypes = ['all'];
}
// Filter modes to make sure only pre-defined modes will be used
modes = modes.filter(element => {
return mediatorModes[element] !== undefined;
});
if (modes.length === 0 && mediaTypes.length === 1 &&
mediaTypes[0] === 'all') {
return;
}
// Have the modes ordered to prevent mode mismatching
modes.sort();
// Create unique string to repesent the mode
// Currently only one media type is supported per declaration
let ruleMode = mediaTypes[0];
if (modes.length > 0) {
ruleMode += '.' + modes.join('.');
}
if (typeof mediatorOutputRules[ruleMode] === 'undefined') {
mediatorOutputRules[ruleMode] = {};
}
let selector = decl.parent.selector;
let mediatorRuleSelector = mediatorOutputRules[ruleMode][selector];
if (typeof mediatorRuleSelector === 'undefined') {
mediatorOutputRules[ruleMode][selector] = {};
}
mediatorOutputRules[ruleMode][selector][prop] = decl.value;
if (decl.parent.nodes.length === 1) {
decl.parent.remove();
} else {
decl.remove();
}
}
function extractRules(mediatorModes, css) {
let mediatorOutputRules = {};
let extractor = extractOneRule.bind(this,
mediatorModes,
mediatorOutputRules);
css.walkDecls(extractor);
return mediatorOutputRules;
}
function mutateCSS(mediatorModes, mediatorOutputRules, css) {
for (let mode in mediatorOutputRules) {
let artifacts = mode.split('.');
let mediaq = '@media ';
for (let i in artifacts) {
if (parseInt(i) === 0) {
// Media type
mediaq += artifacts[i].replace(/-/g, ' ') + ' ';
} else {
// Mediator mode (media expression)
mediaq += 'and ';
mediaq += '(' + mediatorModes[artifacts[i]] + ') ';
}
}
mediaq += '{';
for (let element in mediatorOutputRules[mode]) {
mediaq += element + ' {';
for (let prop in mediatorOutputRules[mode][element]) {
mediaq += prop + ': ';
mediaq += mediatorOutputRules[mode][element][prop] + ';';
}
mediaq += '}';
}
mediaq += '}';
css.append(mediaq);
}
}
return function (css) {
let mediatorModes = extractModes(css);
let mediatorOutputRules = extractRules(mediatorModes, css);
mutateCSS(mediatorModes, mediatorOutputRules, css);
};
});