Skip to content

Commit ff465bd

Browse files
authored
Merge pull request #128 from dtetto/escape-for-string-replace
Escape '$' as '$$$' due to `String.replace()` special patterns 💰
2 parents 50c9bef + c0812be commit ff465bd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/result.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,19 @@ 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, '$$$');
121+
}
122+
115123
function insertIntoIndexHTML(html, head, body) {
116-
html = html.replace("<!-- EMBER_CLI_FASTBOOT_BODY -->", body);
124+
html = html.replace("<!-- EMBER_CLI_FASTBOOT_BODY -->", escapeForStringReplace(body));
117125

118126
if (head) {
119-
html = html.replace("<!-- EMBER_CLI_FASTBOOT_HEAD -->", head);
127+
html = html.replace("<!-- EMBER_CLI_FASTBOOT_HEAD -->", escapeForStringReplace(head));
120128
}
121129

122130
return html;

test/result-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,23 @@ describe('Result', function() {
8181
});
8282
});
8383
});
84+
85+
describe('when the document has special-case content', function () {
86+
var BODY = '<h1>A special response document: $$</h1>';
87+
88+
beforeEach(function () {
89+
doc.body.appendChild(doc.createRawHTMLSection(BODY));
90+
91+
result._fastbootInfo.response.statusCode = 418;
92+
result._finalize();
93+
});
94+
95+
it('it should handle \'$$\' correctly (due to `String.replace()` gotcha)', function () {
96+
return result.html()
97+
.then(function (result) {
98+
expect(result).to.include(BODY);
99+
});
100+
});
101+
});
84102
});
85103
});

0 commit comments

Comments
 (0)