Skip to content

Commit c5175b4

Browse files
authored
Merge pull request #151 from bekzod/refactor-insert-html
refactored `insertIntoIndexHTML`
2 parents f41f387 + d238901 commit c5175b4

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

src/result.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,25 @@ function missingTag(tag) {
127127

128128
function insertIntoIndexHTML(html, head, body) {
129129
if (!html) { return Promise.resolve(html); }
130+
let isBodyReplaced = false;
131+
let isHeadReplaced = false;
130132

131-
if (body) {
132-
let isBodyReplaced = false;
133-
html = html.replace("<!-- EMBER_CLI_FASTBOOT_BODY -->", function() {
133+
html = html.replace(/<\!-- EMBER_CLI_FASTBOOT_(HEAD|BODY) -->/g, function(match, tag) {
134+
if (tag === 'HEAD' && head && !isHeadReplaced) {
135+
isHeadReplaced = true;
136+
return head;
137+
} else if (tag === 'BODY' && body && !isBodyReplaced) {
134138
isBodyReplaced = true;
135139
return body;
136-
});
137-
138-
if (!isBodyReplaced) {
139-
return missingTag('<!--EMBER_CLI_FASTBOTT_BODY-->');
140140
}
141-
}
141+
return '';
142+
});
142143

143-
if (head) {
144-
let isHeadReplaced = false;
145-
html = html.replace("<!-- EMBER_CLI_FASTBOOT_HEAD -->", function() {
146-
isHeadReplaced = true;
147-
return head;
148-
});
149-
150-
if (!isHeadReplaced) {
151-
return missingTag('<!--EMBER_CLI_FASTBOTT_HEAD-->');
152-
}
144+
if (head && !isHeadReplaced) {
145+
return missingTag('<!--EMBER_CLI_FASTBOOT_HEAD-->');
146+
}
147+
if (body && !isBodyReplaced) {
148+
return missingTag('<!--EMBER_CLI_FASTBOOT_BODY-->');
153149
}
154150

155151
return Promise.resolve(html);

test/result-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Result', function() {
3939
return result.html()
4040
.catch(function (e) {
4141
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.");
42+
expect(e.message).to.equal("Fastboot was not able to find <!--EMBER_CLI_FASTBOOT_BODY--> in base HTML. It could not replace the contents.");
4343
done();
4444
});
4545
});
@@ -55,7 +55,7 @@ describe('Result', function() {
5555
return result.html()
5656
.catch(function (e) {
5757
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.");
58+
expect(e.message).to.equal("Fastboot was not able to find <!--EMBER_CLI_FASTBOOT_HEAD--> in base HTML. It could not replace the contents.");
5959
done();
6060
});
6161
});

0 commit comments

Comments
 (0)