-
Notifications
You must be signed in to change notification settings - Fork 160
/
result.js
298 lines (253 loc) · 7.81 KB
/
result.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';
const SimpleDOM = require('simple-dom');
const HTMLSerializer = new SimpleDOM.HTMLSerializer(SimpleDOM.voidMap);
const SHOEBOX_TAG_PATTERN = '<script type="fastboot/shoebox"';
const HTML_HEAD_REGEX = /^([\s\S]*<\/head>)([\s\S]*)/;
/**
* Represents the rendered result of visiting an Ember app at a particular URL.
* A `Result` object is returned from calling {@link FastBoot}'s `visit()`
* method.
*/
class Result {
constructor(doc, html, fastbootInfo) {
this.isDestroyed = false;
this._doc = doc;
this._html = html;
this._fastbootInfo = fastbootInfo;
this.applicationInstance = undefined;
this.applicationInstanceInstance = undefined;
this.analytics = {};
}
/**
* Returns the HTML representation of the rendered route, inserted
* into the application's `index.html`.
*
* @returns {Promise<String>} the application's DOM serialized to HTML
*/
html() {
let response = this._fastbootInfo.response;
let statusCode = response && this._fastbootInfo.response.statusCode;
if (statusCode === 204) {
this._html = '';
this._head = '';
this._body = '';
} else if (statusCode >= 300 && statusCode <= 399) {
let location = response.headers.get('location');
this._html = '<body><!-- EMBER_CLI_FASTBOOT_BODY --></body>';
this._head = '';
this._body = '';
if (location) {
this._body = `<h1>Redirecting to <a href="${location}">${location}</a></h1>`;
}
}
return insertIntoIndexHTML(
this._html,
this._htmlAttributes,
this._htmlClass,
this._head,
this._body,
this._bodyAttributes,
this._bodyClass
);
}
/**
* Returns the HTML representation of the rendered route, inserted
* into the application's `index.html`, split into chunks.
* The first chunk contains the document's head, the second contains the body
* until just before the shoebox tags (if there are any) and the last chunk
* contains the shoebox tags and the closing `body` tag. If there are no
* shoebox tags, there are only 2 chunks and the second one contains the
* complete document body, including the closing `body` tag.
*
* @returns {Promise<Array<String>>} the application's DOM serialized to HTML, split into chunks
*/
chunks() {
return insertIntoIndexHTML(
this._html,
this._htmlAttributes,
this._htmlClass,
this._head,
this._body,
this._bodyAttributes,
this._bodyClass
).then(html => {
let docParts = html.match(HTML_HEAD_REGEX);
if (!docParts || docParts.length === 1) {
return [html];
}
let [, head, body] = docParts;
if (!head || !body) {
throw new Error(
'Could not idenfity head and body of the document! Make sure the document is well formed.'
);
}
let [plainBody, ...shoeboxes] = body.split(SHOEBOX_TAG_PATTERN);
let chunks = [head, plainBody].concat(
shoeboxes.map(shoebox => `${SHOEBOX_TAG_PATTERN}${shoebox}`)
);
return chunks;
});
}
/**
* Returns the serialized representation of DOM HEAD and DOM BODY
*
* @returns {Object} serialized version of DOM
*/
domContents() {
return {
head: this._head,
body: this._body,
};
}
/**
* @private
*
* Called once the Result has finished being constructed and the
* ApplicationInstance instance has finished rendering. Once `finalize()` is
* called, state is gathered from the completed ApplicationInstance instance
* and statically copied to this Result instance.
*/
_finalize() {
if (this.finalized) {
throw new Error('Results cannot be finalized more than once');
}
// Grab some metadata from the sandboxed application instance
// and copy it to this Result object.
let { applicationInstanceInstance } = this;
if (applicationInstanceInstance) {
this._finalizeMetadata(applicationInstanceInstance);
}
this._finalizeHTML();
this.finalized = true;
return this;
}
_finalizeMetadata(applicationInstanceInstance) {
if (applicationInstanceInstance._booted) {
this.url = applicationInstanceInstance.getURL();
}
let { response } = this._fastbootInfo;
if (response) {
this.headers = response.headers;
this.statusCode = response.statusCode;
}
}
_destroy() {
if (this.isDestroyed) {
return false;
}
this.isDestroyed = true;
if (this.applicationInstanceInstance !== undefined) {
this.applicationInstanceInstance.destroy();
}
if (this.applicationInstance !== undefined) {
this.applicationInstance.destroy();
}
return true;
}
_finalizeHTML() {
let htmlElement = this._doc.documentElement;
let head = this._doc.head;
let body = this._doc.body;
let { klass: htmlClass, attributes: htmlAttributes } = extractExtraAttributes(htmlElement);
this._htmlClass = htmlClass;
this._htmlAttributes = htmlAttributes;
let { klass: bodyClass, attributes: bodyAttributes } = extractExtraAttributes(body);
this._bodyClass = bodyClass;
this._bodyAttributes = bodyAttributes;
if (head) {
head = HTMLSerializer.serializeChildren(head);
}
body = HTMLSerializer.serializeChildren(body);
this._head = head;
// Adding script boundary around the body
this._body = `<script type="x/boundary" id="fastboot-body-start"></script>${body}<script type="x/boundary" id="fastboot-body-end"></script>`;
}
}
function extractExtraAttributes(element) {
let klass;
let attributes;
if (element.attributes.length > 0) {
let elementClass = element.attributes.find(attr => attr.name === 'class');
if (elementClass) {
klass = elementClass;
let otherAttrs = element.attributes.filter(attr => attr.name !== 'class');
if (otherAttrs.length > 0) {
attributes = HTMLSerializer.attributes(otherAttrs);
} else {
attributes = null;
}
} else {
attributes = HTMLSerializer.attributes(element.attributes);
klass = null;
}
} else {
klass = attributes = null;
}
return { klass, attributes };
}
function missingTag(tag) {
throw new Error(
`Fastboot was not able to find ${tag} in base HTML. It could not replace the contents.`
);
}
function addClass(html, regex, newClass) {
return html.replace(regex, function(_, tag, attributes) {
if (/class="([^"]*)"/i.test(attributes)) {
attributes = attributes.replace(/class="([^"]*)"/i, function(_, klass) {
return `class="${klass} ${newClass}"`;
});
} else {
attributes += ' class="' + newClass + '"';
}
return `<${tag}${attributes}>`;
});
}
async function insertIntoIndexHTML(
html,
htmlAttributes,
htmlClass,
head,
body,
bodyAttributes,
bodyClass
) {
if (!html) {
return Promise.resolve(html);
}
let isBodyReplaced = false;
let isHeadReplaced = false;
html = html.replace(/<!-- EMBER_CLI_FASTBOOT_(HEAD|BODY) -->/g, function(match, tag) {
if (tag === 'HEAD' && head && !isHeadReplaced) {
isHeadReplaced = true;
return head;
} else if (tag === 'BODY' && body && !isBodyReplaced) {
isBodyReplaced = true;
return body;
}
return '';
});
if (htmlClass) {
html = addClass(html, /<(html)(.*)>/i, htmlClass.value);
}
if (htmlAttributes) {
html = html.replace(/<html[^>]*/i, function(match) {
return match + ' ' + htmlAttributes;
});
}
if (bodyClass) {
html = addClass(html, /<(body)(.*)>/i, bodyClass.value);
}
if (bodyAttributes) {
html = html.replace(/<body[^>]*/i, function(match) {
return match + ' ' + bodyAttributes;
});
}
if (head && !isHeadReplaced) {
missingTag('<!--EMBER_CLI_FASTBOOT_HEAD-->');
}
if (body && !isBodyReplaced) {
missingTag('<!--EMBER_CLI_FASTBOOT_BODY-->');
}
return html;
}
module.exports = Result;