Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80758 views
1
// Generated by CoffeeScript 1.9.1
2
var $, SPACES_ONLY, Serialiser, TEXT_LEADING_WHITESPACE, TEXT_TRAILING_WHITESPACE, WHITESPACE_ONLY, containsNewlines, entityDecode, exports, find, firstNonWhitespaceChild, genericBranchSerialiser, genericLeafSerialiser, joinList, last, nodeSerialisers, ref, serialise, stringEscape, tagConvention;
3
4
ref = require('./helpers'), last = ref.last, find = ref.find;
5
6
$ = require('./symbols');
7
8
stringEscape = require('./stringescape');
9
10
entityDecode = require('./entitydecode');
11
12
module.exports = exports = serialise = function(parseTree) {
13
return new Serialiser().serialise(parseTree);
14
};
15
16
Serialiser = (function() {
17
function Serialiser() {}
18
19
Serialiser.prototype.serialise = function(parseTree) {
20
var domObjectParts;
21
if (parseTree.children && parseTree.children.length && parseTree.children[0].type === $.CJSX_PRAGMA) {
22
this.domObject = parseTree.children[0].value;
23
} else {
24
this.domObject = 'React.DOM';
25
}
26
domObjectParts = this.domObject.split('.');
27
if (domObjectParts.length > 0 && domObjectParts[0] !== '') {
28
this.reactObject = domObjectParts[0];
29
} else {
30
this.reactObject = 'React';
31
}
32
return this.serialiseNode(parseTree);
33
};
34
35
Serialiser.prototype.serialiseNode = function(node) {
36
var serialised;
37
if (nodeSerialisers[node.type] == null) {
38
throw new Error("unknown parseTree node type " + node.type);
39
}
40
serialised = nodeSerialisers[node.type].call(this, node);
41
if (!(typeof serialised === 'string' || serialised === null)) {
42
throw new Error("serialiser " + node.type + " didn\'t return a string");
43
}
44
return serialised;
45
};
46
47
Serialiser.prototype.serialiseSpreadAndPairAttributes = function(children) {
48
var accumulatedWhitespace, assignIndex, assignItem, assigns, assignsWithWhitespace, child, childIndex, flushPairs, j, joinedAssigns, k, lastAssignWithWhitespace, len, len1, pairAttrsBuffer, ref1, trailingWhiteplace;
49
assigns = [];
50
pairAttrsBuffer = [];
51
flushPairs = (function(_this) {
52
return function() {
53
var serialisedChild, serialisedPairs;
54
if (pairAttrsBuffer.length) {
55
serialisedChild = _this.serialiseAttributePairs(pairAttrsBuffer);
56
if (serialisedChild) {
57
assigns.push({
58
type: $.CS,
59
value: serialisedChild
60
});
61
} else {
62
serialisedPairs = pairAttrsBuffer.map(function(p) {
63
return _this.serialiseNode(p);
64
}).join('').replace('\n', '\\\n');
65
assigns.push({
66
type: $.CJSX_WHITESPACE,
67
value: serialisedPairs
68
});
69
}
70
return pairAttrsBuffer = [];
71
}
72
};
73
})(this);
74
if (((ref1 = firstNonWhitespaceChild(children)) != null ? ref1.type : void 0) === $.CJSX_ATTR_SPREAD) {
75
assigns.push({
76
type: $.CS,
77
value: '{}'
78
});
79
}
80
for (childIndex = j = 0, len = children.length; j < len; childIndex = ++j) {
81
child = children[childIndex];
82
if (child.type === $.CJSX_ATTR_SPREAD) {
83
flushPairs();
84
assigns.push({
85
type: $.CS,
86
value: child.value
87
});
88
} else {
89
pairAttrsBuffer.push(child);
90
}
91
}
92
flushPairs();
93
accumulatedWhitespace = '';
94
assignsWithWhitespace = [];
95
for (assignIndex = k = 0, len1 = assigns.length; k < len1; assignIndex = ++k) {
96
assignItem = assigns[assignIndex];
97
if (assignItem != null) {
98
if (assignItem.type === $.CJSX_WHITESPACE) {
99
accumulatedWhitespace += this.serialiseNode(assignItem);
100
} else {
101
assignsWithWhitespace.push(accumulatedWhitespace + this.serialiseNode(assignItem));
102
accumulatedWhitespace = '';
103
}
104
}
105
}
106
if (assignsWithWhitespace.length) {
107
lastAssignWithWhitespace = assignsWithWhitespace.pop();
108
trailingWhiteplace = accumulatedWhitespace.replace('\\\n', '\n');
109
assignsWithWhitespace.push(lastAssignWithWhitespace + trailingWhiteplace);
110
}
111
joinedAssigns = joinList(assignsWithWhitespace);
112
return "React.__spread(" + (joinList(assignsWithWhitespace)) + ")";
113
};
114
115
Serialiser.prototype.serialiseAttributePairs = function(children) {
116
var child, childIndex, indexOfLastSemanticChild, isBeforeLastSemanticChild, ref1, semanticChildren, serialisedChild, serialisedChildren, whitespaceChildren;
117
ref1 = children.reduce(function(partitionedChildren, child) {
118
if (child.type === $.CJSX_WHITESPACE) {
119
partitionedChildren[0].push(child);
120
} else {
121
partitionedChildren[1].push(child);
122
}
123
return partitionedChildren;
124
}, [[], []]), whitespaceChildren = ref1[0], semanticChildren = ref1[1];
125
indexOfLastSemanticChild = children.lastIndexOf(last(semanticChildren));
126
isBeforeLastSemanticChild = function(childIndex) {
127
return childIndex < indexOfLastSemanticChild;
128
};
129
if (semanticChildren.length) {
130
serialisedChildren = (function() {
131
var j, len, results;
132
results = [];
133
for (childIndex = j = 0, len = children.length; j < len; childIndex = ++j) {
134
child = children[childIndex];
135
serialisedChild = this.serialiseNode(child);
136
if (child.type === $.CJSX_WHITESPACE) {
137
if (containsNewlines(serialisedChild)) {
138
if (isBeforeLastSemanticChild(childIndex)) {
139
results.push(serialisedChild.replace('\n', ' \\\n'));
140
} else {
141
results.push(serialisedChild);
142
}
143
} else {
144
results.push(null);
145
}
146
} else if (isBeforeLastSemanticChild(childIndex)) {
147
results.push(serialisedChild + ', ');
148
} else {
149
results.push(serialisedChild);
150
}
151
}
152
return results;
153
}).call(this);
154
return '{' + serialisedChildren.join('') + '}';
155
} else {
156
return null;
157
}
158
};
159
160
return Serialiser;
161
162
})();
163
164
genericBranchSerialiser = function(node) {
165
return node.children.map((function(_this) {
166
return function(child) {
167
return _this.serialiseNode(child);
168
};
169
})(this)).join('');
170
};
171
172
genericLeafSerialiser = function(node) {
173
return node.value;
174
};
175
176
tagConvention = /^[a-z]|\-/;
177
178
nodeSerialisers = {
179
ROOT: genericBranchSerialiser,
180
CJSX_PRAGMA: function() {
181
return "`/** @jsx " + this.domObject + " */`";
182
},
183
CJSX_EL: function(node) {
184
var accumulatedWhitespace, child, element, j, len, ref1, serialisedChild, serialisedChildren;
185
serialisedChildren = [];
186
accumulatedWhitespace = '';
187
ref1 = node.children;
188
for (j = 0, len = ref1.length; j < len; j++) {
189
child = ref1[j];
190
serialisedChild = this.serialiseNode(child);
191
if (child != null) {
192
if (serialisedChild.length === 0 || WHITESPACE_ONLY.test(serialisedChild)) {
193
accumulatedWhitespace += serialisedChild;
194
} else {
195
serialisedChildren.push(accumulatedWhitespace + serialisedChild);
196
accumulatedWhitespace = '';
197
}
198
}
199
}
200
if (serialisedChildren.length) {
201
serialisedChildren[serialisedChildren.length - 1] += accumulatedWhitespace;
202
accumulatedWhitespace = '';
203
}
204
if (tagConvention.test(node.value)) {
205
element = '"' + node.value + '"';
206
} else {
207
element = node.value;
208
}
209
return this.reactObject + ".createElement(" + element + ", " + (joinList(serialisedChildren)) + ")";
210
},
211
CJSX_COMMENT: function(node) {
212
return '';
213
},
214
CJSX_ESC: function(node) {
215
var childrenSerialised;
216
childrenSerialised = node.children.map((function(_this) {
217
return function(child) {
218
return _this.serialiseNode(child);
219
};
220
})(this)).join('');
221
return '(' + childrenSerialised + ')';
222
},
223
CJSX_ATTRIBUTES: function(node) {
224
if (node.children.some(function(child) {
225
return child.type === $.CJSX_ATTR_SPREAD;
226
})) {
227
return this.serialiseSpreadAndPairAttributes(node.children);
228
} else {
229
return this.serialiseAttributePairs(node.children) || 'null';
230
}
231
},
232
CJSX_ATTR_PAIR: function(node) {
233
return node.children.map((function(_this) {
234
return function(child) {
235
return _this.serialiseNode(child);
236
};
237
})(this)).join(': ');
238
},
239
CJSX_ATTR_SPREAD: function(node) {
240
return node.value;
241
},
242
CS: genericLeafSerialiser,
243
CS_COMMENT: genericLeafSerialiser,
244
CS_HEREDOC: genericLeafSerialiser,
245
CS_STRING: genericLeafSerialiser,
246
CS_REGEX: genericLeafSerialiser,
247
CS_HEREGEX: genericLeafSerialiser,
248
JS_ESC: genericLeafSerialiser,
249
CJSX_WHITESPACE: genericLeafSerialiser,
250
CJSX_TEXT: function(node) {
251
var escapedText, leftSpace, leftTrim, rightSpace, rightTrim, text, trimmedText;
252
text = node.value;
253
if (containsNewlines(text)) {
254
if (WHITESPACE_ONLY.test(text)) {
255
return text;
256
} else {
257
leftSpace = text.match(TEXT_LEADING_WHITESPACE);
258
rightSpace = text.match(TEXT_TRAILING_WHITESPACE);
259
if (leftSpace) {
260
leftTrim = text.indexOf('\n');
261
} else {
262
leftTrim = 0;
263
}
264
if (rightSpace) {
265
rightTrim = text.lastIndexOf('\n') + 1;
266
} else {
267
rightTrim = text.length;
268
}
269
trimmedText = text.substring(leftTrim, rightTrim);
270
escapedText = stringEscape(entityDecode(trimmedText), {
271
preserveNewlines: true
272
});
273
return '"""' + escapedText + '"""';
274
}
275
} else {
276
if (text === '') {
277
return null;
278
} else {
279
return '"' + stringEscape(entityDecode(text)) + '"';
280
}
281
}
282
},
283
CJSX_ATTR_KEY: genericLeafSerialiser,
284
CJSX_ATTR_VAL: genericLeafSerialiser
285
};
286
287
firstNonWhitespaceChild = function(children) {
288
return find.call(children, function(child) {
289
return child.type !== $.CJSX_WHITESPACE;
290
});
291
};
292
293
containsNewlines = function(text) {
294
return text.indexOf('\n') > -1;
295
};
296
297
joinList = function(items) {
298
var i, output;
299
output = items[items.length - 1];
300
i = items.length - 2;
301
while (i >= 0) {
302
if (output.charAt(0) === '\n') {
303
output = items[i] + ',' + output;
304
} else {
305
output = items[i] + ', ' + output;
306
}
307
i--;
308
}
309
return output;
310
};
311
312
SPACES_ONLY = /^\s+$/;
313
314
WHITESPACE_ONLY = /^[\n\s]+$/;
315
316
TEXT_LEADING_WHITESPACE = /^\s*?\n\s*/;
317
318
TEXT_TRAILING_WHITESPACE = /\s*?\n\s*?$/;
319
320
exports.Serialiser = Serialiser;
321
322
exports.nodeSerialisers = nodeSerialisers;
323
324