UNPKG

1.79 kBJavaScriptView Raw
1const Stringifier = require('postcss/lib/stringifier');
2
3module.exports = class ValuesStringifier extends Stringifier {
4 static stringify(node, builder) {
5 const stringifier = new ValuesStringifier(builder);
6 stringifier.stringify(node);
7 }
8
9 basic(node, value) {
10 const print = value || node.value;
11 const after = node.raws.after ? this.raw(node, 'after') || '' : '';
12 // NOTE: before is handled by postcss in stringifier.body
13
14 this.builder(print, node, 'start');
15 this.builder(after, node, 'end');
16 }
17
18 atword(...args) {
19 this.atrule(...args);
20 }
21
22 comment(node) {
23 if (node.inline) {
24 const left = this.raw(node, 'left', 'commentLeft');
25 const right = this.raw(node, 'right', 'commentRight');
26 this.builder(`//${left}${node.text}${right}`, node);
27 } else {
28 super.comment(node);
29 }
30 }
31
32 func(node) {
33 const after = this.raw(node, 'after') || '';
34
35 this.builder(`${node.name}(`, node, 'start');
36
37 for (const child of node.nodes) {
38 // since we're duplicating this.body here, we have to handle `before`
39 // but we don't want the postcss default \n value, so check it's non-empty first
40 const before = child.raws.before ? this.raw(child, 'before') : '';
41 if (before) {
42 this.builder(before);
43 }
44 this.stringify(child);
45 }
46
47 this.builder(`)${after}`, node, 'end');
48 }
49
50 interpolation(node) {
51 this.basic(node, node.prefix + node.params);
52 }
53
54 numeric(node) {
55 const print = node.value + node.unit;
56 this.basic(node, print);
57 }
58
59 operator(node) {
60 this.basic(node);
61 }
62
63 punctuation(node) {
64 this.basic(node);
65 }
66
67 quoted(node) {
68 this.basic(node);
69 }
70
71 unicodeRange(node) {
72 this.basic(node);
73 }
74
75 word(node) {
76 this.basic(node);
77 }
78};