diff --git a/app/lib/strReplace.js b/app/lib/strReplace.js new file mode 100644 index 0000000..4b36abb --- /dev/null +++ b/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); +}; \ No newline at end of file diff --git a/app/node_controllers/indexController.js b/app/node_controllers/indexController.js index a7ebc3d..b5c488c 100644 --- a/app/node_controllers/indexController.js +++ b/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); diff --git a/app/node_controllers/launchTestController.js b/app/node_controllers/launchTestController.js index 472b0a0..5ea78d8 100644 --- a/app/node_controllers/launchTestController.js +++ b/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); diff --git a/app/node_controllers/resultsController.js b/app/node_controllers/resultsController.js index 3bb4495..4da4c1c 100644 --- a/app/node_controllers/resultsController.js +++ b/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);