Skip to content

Commit da541c8

Browse files
committed
v1.0.1; escape use of $ in string.replace (closes #11)
1 parent da629cb commit da541c8

File tree

5 files changed

+90
-14
lines changed

5 files changed

+90
-14
lines changed

History.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# vNEXT
22

3+
# v1.0.1
4+
5+
* Escape use of $ in string.replace in all internal functions (fixes #11)
6+
7+
# v1.0.0
8+
9+
* Re-release in Meteor 0.9.0 package system, with better semver
10+
* Note, package will be renamed to meteorhacks:inject-initial once MDG
11+
finishes up with rename and redirect support.
12+
313
# v0.0.19
414

515
* Critical work (fixes and tests) from arunoda. Thank you!

lib/inject-core.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ http.OutgoingMessage.prototype.write = function(chunk, encoding) {
1919
}
2020
}
2121

22-
2322
this.iInjected = true;
2423
}
2524

lib/inject-server.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
function escapeReplaceString(str) {
2+
/*
3+
* When using string.replace(str, newSubStr), the dollar sign ("$") is
4+
* considered a special character in newSubStr, and needs to be escaped
5+
* as "$$". We have to do this twice, for escaping the newSubStr in
6+
* this function, and for the resulting string which is passed back.
7+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
8+
*/
9+
return str.replace(/\$/g, '$$$$');
10+
}
11+
112
Inject = {
213
// stores in a script type=application/ejson tag, accessed with Injected.obj('id')
314
obj: function(id, data, res) {
@@ -74,7 +85,7 @@ Inject = {
7485
+ "</script>\n";
7586
}
7687

77-
return html.replace('<head>', '<head>\n' + injectHtml);
88+
return html.replace('<head>', '<head>\n' + escapeReplaceString(injectHtml));
7889
},
7990

8091
_injectMeta: function(html, res) {
@@ -89,7 +100,7 @@ Inject = {
89100
+ "' content='" + meta.replace("'", '&apos;') + "'>\n", res;
90101
}
91102

92-
return html.replace('<head>', '<head>\n' + injectHtml);
103+
return html.replace('<head>', '<head>\n' + escapeReplaceString(injectHtml));
93104
},
94105

95106
_injectHeads: function(html, res) {
@@ -103,7 +114,7 @@ Inject = {
103114
injectHtml += head + '\n';
104115
}
105116

106-
return html.replace('<head>', '<head>\n' + injectHtml);
117+
return html.replace('<head>', '<head>\n' + escapeReplaceString(injectHtml));
107118
},
108119

109120
_injectBodies: function(html, res) {
@@ -117,7 +128,7 @@ Inject = {
117128
injectHtml += body + '\n';
118129
}
119130

120-
return html.replace('<body>', '<body>\n' + injectHtml);
131+
return html.replace('<body>', '<body>\n' + escapeReplaceString(injectHtml));
121132
},
122133

123134
// ensure object exists and store there

package.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package.describe({
22
summary: "Allow injection of arbitrary data to initial Meteor HTML page",
3-
version: "1.0.0",
4-
git: "https://github.com/gadicc/meteor-inject-initial.git",
5-
name: "meteorhacks:inject-initial"
3+
version: "1.0.1",
4+
git: "https://github.com/meteorhacks/meteor-inject-initial.git",
5+
name: "gadicohen:inject-initial"
66
});
77

88
Npm.depends({

test/inject-internal-api.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ Tinytest.add(
44
function func() {};
55
Inject.rawHeads = {
66
"id1": "one",
7-
"id2": "two"
7+
"id2": "two$$"
88
};
99

1010
var res = {
1111
Inject: {
1212
rawHeads: {
1313
"id3": "three",
14-
"id4": "four"
14+
"id4": "four$$"
1515
}
1616
}
1717
};
1818

1919
var newHtml = Inject._injectHeads("<head></head>", res);
20-
test.equal(newHtml, "<head>\none\ntwo\nthree\nfour\n</head>")
20+
test.equal(newHtml, "<head>\none\ntwo$$\nthree\nfour$$\n</head>")
2121
}
2222
);
2323

@@ -27,19 +27,75 @@ Tinytest.add(
2727
function func() {};
2828
Inject.rawBodies = {
2929
"id1": "one",
30-
"id2": "two"
30+
"id2": "two$$"
3131
};
3232

3333
var res = {
3434
Inject: {
3535
rawBodies: {
3636
"id3": "three",
37-
"id4": "four"
37+
"id4": "four$$"
3838
}
3939
}
4040
};
4141

4242
var newHtml = Inject._injectBodies("<body></body>", res);
43-
test.equal(newHtml, "<body>\none\ntwo\nthree\nfour\n</body>")
43+
test.equal(newHtml, "<body>\none\ntwo$$\nthree\nfour$$\n</body>")
44+
}
45+
);
46+
47+
Tinytest.add(
48+
'Inject Internal Apis - _injectObjects',
49+
function (test) {
50+
function func() {};
51+
Inject.objList = {
52+
"id1": { value: 1 },
53+
"id2": { value: "two$$" }
54+
};
55+
56+
var res = {
57+
Inject: {
58+
objList: {
59+
"id3": { value: "three$$" },
60+
"id4": { value: 4 }
61+
}
62+
}
63+
};
64+
65+
var newHtml = Inject._injectObjects("<head></head>", res);
66+
test.equal(newHtml, "<head>\n"
67+
+ " <script id='id1' type='application/ejson'>{\"value\":1}</script>\n"
68+
+ " <script id='id2' type='application/ejson'>{\"value\":\"two$$\"}</script>\n"
69+
+ " <script id='id3' type='application/ejson'>{\"value\":\"three$$\"}</script>\n"
70+
+ " <script id='id4' type='application/ejson'>{\"value\":4}</script>\n"
71+
+ "</head>");
72+
}
73+
);
74+
75+
Tinytest.add(
76+
'Inject Internal Apis - _injectMeta',
77+
function (test) {
78+
function func() {};
79+
Inject.metaList = {
80+
"id1": "1",
81+
"id2": "two$$"
82+
};
83+
84+
var res = {
85+
Inject: {
86+
metaList: {
87+
"id3": "three",
88+
"id4": "four$$"
89+
}
90+
}
91+
};
92+
93+
var newHtml = Inject._injectMeta("<head></head>", res);
94+
test.equal(newHtml, "<head>\n"
95+
+ " <meta id='id1' content='1'>\n"
96+
+ " <meta id='id2' content='two$$'>\n"
97+
+ " <meta id='id3' content='three'>\n"
98+
+ " <meta id='id4' content='four$$'>\n"
99+
+ "</head>");
44100
}
45101
);

0 commit comments

Comments
 (0)