1 | #!/usr/bin/env node
|
---|
2 |
|
---|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
---|
4 |
|
---|
5 | var fs = _interopDefault(require('fs'));
|
---|
6 | var parser = _interopDefault(require('postcss-selector-parser'));
|
---|
7 | var postcss = _interopDefault(require('postcss'));
|
---|
8 |
|
---|
9 | const selectorRegExp = /:has/;
|
---|
10 | var plugin = postcss.plugin('css-has-pseudo', opts => {
|
---|
11 | const preserve = Boolean('preserve' in Object(opts) ? opts.preserve : true);
|
---|
12 | return root => {
|
---|
13 | root.walkRules(selectorRegExp, rule => {
|
---|
14 | const modifiedSelector = parser(selectors => {
|
---|
15 | selectors.walkPseudos(selector => {
|
---|
16 | if (selector.value === ':has' && selector.nodes) {
|
---|
17 | const isNotHas = checkIfParentIsNot(selector);
|
---|
18 | selector.value = isNotHas ? ':not-has' : ':has';
|
---|
19 | const attribute = parser.attribute({
|
---|
20 | attribute: encodeURIComponent(String(selector)).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',').replace(/[():%\[\],]/g, '\\$&')
|
---|
21 | });
|
---|
22 |
|
---|
23 | if (isNotHas) {
|
---|
24 | selector.parent.parent.replaceWith(attribute);
|
---|
25 | } else {
|
---|
26 | selector.replaceWith(attribute);
|
---|
27 | }
|
---|
28 | }
|
---|
29 | });
|
---|
30 | }).processSync(rule.selector);
|
---|
31 | const clone = rule.clone({
|
---|
32 | selector: modifiedSelector
|
---|
33 | });
|
---|
34 |
|
---|
35 | if (preserve) {
|
---|
36 | rule.before(clone);
|
---|
37 | } else {
|
---|
38 | rule.replaceWith(clone);
|
---|
39 | }
|
---|
40 | });
|
---|
41 | };
|
---|
42 | });
|
---|
43 |
|
---|
44 | function checkIfParentIsNot(selector) {
|
---|
45 | return Object(Object(selector.parent).parent).type === 'pseudo' && selector.parent.parent.value === ':not';
|
---|
46 | }
|
---|
47 |
|
---|
48 | const fileRegExp = /^[\w\/.]+$/;
|
---|
49 | const argRegExp = /^--(\w+)=("|')?(.+)\2$/;
|
---|
50 | const relaxedJsonPropRegExp = /(['"])?([a-z0-9A-Z_]+)(['"])?:/g;
|
---|
51 | const relaxedJsonValueRegExp = /("[a-z0-9A-Z_]+":\s*)(?!true|false|null|\d+)'?([A-z0-9]+)'?([,}])/g;
|
---|
52 | const argo = process.argv.slice(2).reduce((object, arg) => {
|
---|
53 | const argMatch = arg.match(argRegExp);
|
---|
54 | const fileMatch = arg.match(fileRegExp);
|
---|
55 |
|
---|
56 | if (argMatch) {
|
---|
57 | object[argMatch[1]] = argMatch[3];
|
---|
58 | } else if (fileMatch) {
|
---|
59 | if (object.from === '<stdin>') {
|
---|
60 | object.from = arg;
|
---|
61 | } else if (object.to === '<stdout>') {
|
---|
62 | object.to = arg;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | return object;
|
---|
67 | }, {
|
---|
68 | from: '<stdin>',
|
---|
69 | to: '<stdout>',
|
---|
70 | opts: 'null'
|
---|
71 | }); // get css from command line arguments or stdin
|
---|
72 |
|
---|
73 | (argo.from === '<stdin>' ? getStdin() : readFile(argo.from)).then(css => {
|
---|
74 | if (argo.from === '<stdin>' && !css) {
|
---|
75 | console.log(['CSS Has Pseudo\n', ' Transforms CSS with :has {}\n', 'Usage:\n', ' css-has-pseudo source.css transformed.css', ' css-has-pseudo --from=source.css --to=transformed.css --opts={}', ' echo "body:has(:focus) {}" | css-has-pseudo\n'].join('\n'));
|
---|
76 | process.exit(0);
|
---|
77 | }
|
---|
78 |
|
---|
79 | const pluginOpts = JSON.parse(argo.opts.replace(relaxedJsonPropRegExp, '"$2": ').replace(relaxedJsonValueRegExp, '$1"$2"$3'));
|
---|
80 | const processOptions = Object.assign({
|
---|
81 | from: argo.from,
|
---|
82 | to: argo.to || argo.from
|
---|
83 | }, argo.map ? {
|
---|
84 | map: JSON.parse(argo.map)
|
---|
85 | } : {});
|
---|
86 | const result = plugin.process(css, processOptions, pluginOpts);
|
---|
87 |
|
---|
88 | if (argo.to === '<stdout>') {
|
---|
89 | return result.css;
|
---|
90 | } else {
|
---|
91 | return writeFile(argo.to, result.css).then(() => `CSS was written to "${argo.to}"`);
|
---|
92 | }
|
---|
93 | }).catch(error => {
|
---|
94 | if (Object(error).name === 'CssSyntaxError') {
|
---|
95 | throw new Error(`PostCSS had trouble reading the file (${error.reason} on line ${error.line}, column ${error.column}).`);
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (Object(error).errno === -2) {
|
---|
99 | throw new Error(`Sorry, "${error.path}" could not be read.`);
|
---|
100 | }
|
---|
101 |
|
---|
102 | throw error;
|
---|
103 | }).then(result => {
|
---|
104 | console.log(result);
|
---|
105 | process.exit(0);
|
---|
106 | }, error => {
|
---|
107 | console.error(Object(error).message || 'Something bad happened and we don’t even know what it was.');
|
---|
108 | process.exit(1);
|
---|
109 | });
|
---|
110 |
|
---|
111 | function readFile(pathname) {
|
---|
112 | return new Promise((resolve, reject) => {
|
---|
113 | fs.readFile(pathname, 'utf8', (error, data) => {
|
---|
114 | if (error) {
|
---|
115 | reject(error);
|
---|
116 | } else {
|
---|
117 | resolve(data);
|
---|
118 | }
|
---|
119 | });
|
---|
120 | });
|
---|
121 | }
|
---|
122 |
|
---|
123 | function writeFile(pathname, data) {
|
---|
124 | return new Promise((resolve, reject) => {
|
---|
125 | fs.writeFile(pathname, data, (error, content) => {
|
---|
126 | if (error) {
|
---|
127 | reject(error);
|
---|
128 | } else {
|
---|
129 | resolve(content);
|
---|
130 | }
|
---|
131 | });
|
---|
132 | });
|
---|
133 | }
|
---|
134 |
|
---|
135 | function getStdin() {
|
---|
136 | return new Promise(resolve => {
|
---|
137 | let data = '';
|
---|
138 |
|
---|
139 | if (process.stdin.isTTY) {
|
---|
140 | resolve(data);
|
---|
141 | } else {
|
---|
142 | process.stdin.setEncoding('utf8');
|
---|
143 | process.stdin.on('readable', () => {
|
---|
144 | let chunk;
|
---|
145 |
|
---|
146 | while (chunk = process.stdin.read()) {
|
---|
147 | data += chunk;
|
---|
148 | }
|
---|
149 | });
|
---|
150 | process.stdin.on('end', () => {
|
---|
151 | resolve(data);
|
---|
152 | });
|
---|
153 | }
|
---|
154 | });
|
---|
155 | }
|
---|