diff --git a/tasks/lib/util.js b/tasks/lib/util.js index b0d2887..04f89f2 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -100,9 +100,10 @@ exports.init = function (grunt) { exports.replace_urls_in_serialized = function(search, replace, string) { var length_delta = search.length - replace.length; + var searchRegExp = new RegExp(search, 'g'); // Replace for serialized data - var matches, length, delimiter, old_serialized_data, target_string, new_url; + var matches, length, delimiter, old_serialized_data, target_string, new_url, occurences; var regexp = /s:(\d+):([\\]*['"])(.*?)\2;/g; while (matches = regexp.exec(string)) { @@ -111,12 +112,13 @@ exports.init = function (grunt) { // If the string contains the url make the substitution if (target_string.indexOf(search) !== -1) { + occurences = target_string.match(searchRegExp).length; length = matches[1]; delimiter = matches[2]; // Replace the url - new_url = target_string.replace(search, replace); - length -= length_delta; + new_url = target_string.replace(searchRegExp, replace); + length -= length_delta * occurences; // Compose the new serialized data var new_serialized_data = 's:' + length + ':' + delimiter + new_url + delimiter + ';'; diff --git a/test/deployments_test.js b/test/deployments_test.js index 6bc6820..7307556 100644 --- a/test/deployments_test.js +++ b/test/deployments_test.js @@ -63,6 +63,13 @@ module.exports = { "Replace multiple urls into serialized data." ); + var string4 = '{s:19:"payment_success_url";s:74:"http://loremipsum/payment-successful/ and http://loremipsum/error-message/";s:16:"payment_fail_url";s:33:"http://loremipsum/payment-failed/";s:13:"currency_unit"}'; + test.equal( + util.replace_urls_in_serialized(search, replace, string4), + '{s:19:"payment_success_url";s:90:"http://www.loremipsum.com/payment-successful/ and http://www.loremipsum.com/error-message/";s:16:"payment_fail_url";s:41:"http://www.loremipsum.com/payment-failed/";s:13:"currency_unit"}', + "Replace multiple urls in a single serialized object into serialized data." + ); + test.done(); },