Browse Source

Avoid problems with string.prototype.replace and 610

Gaël Métais 10 years ago
parent
commit
10cdd9cda4

+ 8 - 0
app/lib/strReplace.js

@@ -0,0 +1,8 @@
+/**
+ * Alternative to the standard String.prototype.replace function
+ * Avoids problems with $$, $1, $2, ...
+ */
+
+module.exports = function(str, searched, replacement) {
+    return str.split(searched).join(replacement);
+};

+ 4 - 3
app/node_controllers/indexController.js

@@ -2,8 +2,9 @@
  * Yellow Lab Tools home page controller
  */
 
-var async   = require('async');
-var fs      = require ('fs');
+var async           = require('async');
+var fs              = require ('fs');
+var strReplace      = require('../lib/strReplace');
 
 var indexController = function(req, res, googleAnalyticsId) {
     'use strict';
@@ -16,7 +17,7 @@ var indexController = function(req, res, googleAnalyticsId) {
 
     }, function(err, results) {
         var html = results.htmlTemplate;
-        html = html.replace('%%GA_ID%%', googleAnalyticsId);
+        html = strReplace(html, '%%GA_ID%%', googleAnalyticsId);
 
         res.setHeader('Content-Type', 'text/html');
         res.send(html);

+ 6 - 5
app/node_controllers/launchTestController.js

@@ -2,8 +2,9 @@
  * Controller for the test launching page (the waiting page, after the user submited a test on the index page)
  */
 
-var async   = require('async');
-var fs      = require ('fs');
+var async           = require('async');
+var fs              = require ('fs');
+var strReplace      = require('../lib/strReplace');
 
 var launchTestController = function(req, res, testQueue, googleAnalyticsId) {
     'use strict';
@@ -32,9 +33,9 @@ var launchTestController = function(req, res, testQueue, googleAnalyticsId) {
 
         function sendResponse(html, callback) {
 
-            html = html.replace('%%TEST_URL%%', url);
-            html = html.replace('%%TEST_ID%%', testId);
-            html = html.replace('%%GA_ID%%', googleAnalyticsId);
+            html = strReplace(html, '%%TEST_URL%%', url);
+            html = strReplace(html, '%%TEST_ID%%', testId);
+            html = strReplace(html, '%%GA_ID%%', googleAnalyticsId);
 
             res.setHeader('Content-Type', 'text/html');
             res.send(html);

+ 6 - 5
app/node_controllers/resultsController.js

@@ -2,8 +2,9 @@
  * The page that dispays the results
  */
 
-var async   = require('async');
-var fs      = require ('fs');
+var async           = require('async');
+var fs              = require('fs');
+var strReplace      = require('../lib/strReplace');
 
 var resultsController = function(req, res, googleAnalyticsId) {
     'use strict';
@@ -39,9 +40,9 @@ var resultsController = function(req, res, googleAnalyticsId) {
         phantomasResults = phantomasResults.replace(/<\/script>/g, '\\u003c/script>');
 
         var html = results.htmlTemplate;
-        html = html.replace('%%METADATA%%', results.phantomasMetadata);
-        html = html.replace('%%RESULTS%%', phantomasResults);
-        html = html.replace('%%GA_ID%%', googleAnalyticsId);
+        html = strReplace(html, '%%METADATA%%', results.phantomasMetadata);
+        html = strReplace(html, '%%RESULTS%%', phantomasResults);
+        html = strReplace(html, '%%GA_ID%%', googleAnalyticsId);
 
         res.setHeader('Content-Type', 'text/html');
         res.send(html);