Skip to content

Commit 257cc5e

Browse files
authored
Merge pull request #149 from bekzod/insert-comments-fix
throw when insert comments are not replaced
2 parents b13e7bf + fe7f545 commit 257cc5e

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

src/result.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const HTMLSerializer = new SimpleDOM.HTMLSerializer(SimpleDOM.voidMap);
99
* method.
1010
*/
1111
class Result {
12-
constructor(options) {
13-
this.instanceBooted = false;
14-
this.instanceDestroyed = false;
12+
constructor(options) {
13+
this.instanceBooted = false;
14+
this.instanceDestroyed = false;
1515
this._doc = options.doc;
1616
this._html = options.html;
1717
this._fastbootInfo = options.fastbootInfo;
@@ -43,7 +43,7 @@ class Result {
4343
}
4444
}
4545

46-
return Promise.resolve(insertIntoIndexHTML(this._html, this._head, this._body));
46+
return insertIntoIndexHTML(this._html, this._head, this._body);
4747
}
4848

4949
/**
@@ -84,10 +84,10 @@ class Result {
8484
return this;
8585
}
8686

87-
_finalizeMetadata(instance) {
88-
if (this.instanceBooted) {
89-
this.url = instance.getURL();
90-
}
87+
_finalizeMetadata(instance) {
88+
if (this.instanceBooted) {
89+
this.url = instance.getURL();
90+
}
9191

9292
let response = this._fastbootInfo.response;
9393

@@ -112,22 +112,38 @@ class Result {
112112
}
113113
}
114114

115-
/**
116-
* `String.replace()` converts '$$' to '$', so we must escape each '$' as '$$';
117-
* but because we’re using `String.replace()` to do it, we must use '$$$'!
118-
*/
119-
function escapeForStringReplace(string) {
120-
return string.replace(/\$/g, '$$$');
115+
function missingTag(tag) {
116+
return Promise.reject(new Error(`Fastboot was not able to find ${tag} in base HTML. It could not replace the contents.`));
121117
}
122118

123119
function insertIntoIndexHTML(html, head, body) {
124-
html = html.replace("<!-- EMBER_CLI_FASTBOOT_BODY -->", escapeForStringReplace(body));
120+
if (!html) { return Promise.resolve(html); }
121+
122+
if (body) {
123+
let isBodyReplaced = false;
124+
html = html.replace("<!-- EMBER_CLI_FASTBOOT_BODY -->", function() {
125+
isBodyReplaced = true;
126+
return body;
127+
});
128+
129+
if (!isBodyReplaced) {
130+
return missingTag('<!--EMBER_CLI_FASTBOTT_BODY-->');
131+
}
132+
}
125133

126134
if (head) {
127-
html = html.replace("<!-- EMBER_CLI_FASTBOOT_HEAD -->", escapeForStringReplace(head));
135+
let isHeadReplaced = false;
136+
html = html.replace("<!-- EMBER_CLI_FASTBOOT_HEAD -->", function() {
137+
isHeadReplaced = true;
138+
return head;
139+
});
140+
141+
if (!isHeadReplaced) {
142+
return missingTag('<!--EMBER_CLI_FASTBOTT_HEAD-->');
143+
}
128144
}
129145

130-
return html;
146+
return Promise.resolve(html);
131147
}
132148

133149
module.exports = Result;

test/result-test.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@ describe('Result', function() {
2929

3030
describe('html()', function () {
3131

32+
describe('reject when body insert comment missing', function () {
33+
beforeEach(function () {
34+
result._html = '<head></head><body></body>';
35+
result._body = '<h1>news</h2>';
36+
});
37+
38+
it('rejects when body insert comment missing', function (done) {
39+
return result.html()
40+
.catch(function (e) {
41+
expect(e).to.be.an('error');
42+
expect(e.message).to.equal("Fastboot was not able to find <!--EMBER_CLI_FASTBOTT_BODY--> in base HTML. It could not replace the contents.");
43+
done();
44+
});
45+
});
46+
});
47+
48+
describe('reject when head insert comment missing', function () {
49+
beforeEach(function () {
50+
result._html = '<head></head><body><!-- EMBER_CLI_FASTBOOT_BODY --></body>';
51+
result._head = '<title>news</title>';
52+
});
53+
54+
it('rejects when head insert comment missing', function (done) {
55+
return result.html()
56+
.catch(function (e) {
57+
expect(e).to.be.an('error');
58+
expect(e.message).to.equal("Fastboot was not able to find <!--EMBER_CLI_FASTBOTT_HEAD--> in base HTML. It could not replace the contents.");
59+
done();
60+
});
61+
});
62+
});
63+
3264
describe('when the response status code is 204', function () {
3365
beforeEach(function () {
3466
result._fastbootInfo.response.statusCode = 204;
@@ -37,9 +69,9 @@ describe('Result', function() {
3769

3870
it('should return an empty message body', function () {
3971
return result.html()
40-
.then(function (result) {
41-
expect(result).to.equal('');
42-
});
72+
.then(function (result) {
73+
expect(result).to.equal('');
74+
});
4375
});
4476
});
4577

0 commit comments

Comments
 (0)