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
+
1
12
Inject = {
2
13
// stores in a script type=application/ejson tag, accessed with Injected.obj('id')
3
14
obj : function ( id , data , res ) {
@@ -74,7 +85,7 @@ Inject = {
74
85
+ "</script>\n" ;
75
86
}
76
87
77
- return html . replace ( '<head>' , '<head>\n' + injectHtml ) ;
88
+ return html . replace ( '<head>' , '<head>\n' + escapeReplaceString ( injectHtml ) ) ;
78
89
} ,
79
90
80
91
_injectMeta : function ( html , res ) {
@@ -89,7 +100,7 @@ Inject = {
89
100
+ "' content='" + meta . replace ( "'" , ''' ) + "'>\n" , res ;
90
101
}
91
102
92
- return html . replace ( '<head>' , '<head>\n' + injectHtml ) ;
103
+ return html . replace ( '<head>' , '<head>\n' + escapeReplaceString ( injectHtml ) ) ;
93
104
} ,
94
105
95
106
_injectHeads : function ( html , res ) {
@@ -103,7 +114,7 @@ Inject = {
103
114
injectHtml += head + '\n' ;
104
115
}
105
116
106
- return html . replace ( '<head>' , '<head>\n' + injectHtml ) ;
117
+ return html . replace ( '<head>' , '<head>\n' + escapeReplaceString ( injectHtml ) ) ;
107
118
} ,
108
119
109
120
_injectBodies : function ( html , res ) {
@@ -117,7 +128,7 @@ Inject = {
117
128
injectHtml += body + '\n' ;
118
129
}
119
130
120
- return html . replace ( '<body>' , '<body>\n' + injectHtml ) ;
131
+ return html . replace ( '<body>' , '<body>\n' + escapeReplaceString ( injectHtml ) ) ;
121
132
} ,
122
133
123
134
// ensure object exists and store there
0 commit comments