From 44de869c8c6212041f8ccf8b921b9466d75a301d Mon Sep 17 00:00:00 2001 From: Dario Ghilardi Date: Mon, 25 Nov 2013 15:48:30 +0100 Subject: [PATCH] Fixed a multiple replacement that happens when the source pattern is contained into the replacement pattern. --- package.json | 2 +- tasks/lib/util.js | 3 ++- test/deployments_test.js | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e6b8c8f..7249ec3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-wordpress-deploy", "description": "Deploy Wordpress without pain using Grunt.", - "version": "0.0.3", + "version": "0.0.4", "homepage": "https://github.com/webrain/grunt-wordpress-deploy", "author": { "name": "Dario Ghilardi", diff --git a/tasks/lib/util.js b/tasks/lib/util.js index ea712fa..c000b6a 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -130,7 +130,8 @@ exports.init = function (grunt) { }; exports.replace_urls_in_string = function (search, replace, string) { - return string.replace(new RegExp(search, 'g'), replace); + var regexp = new RegExp('(?!' + replace + ')(' + search + ')', 'g'); + return string.replace(regexp, replace); }; /* Commands generators */ diff --git a/test/deployments_test.js b/test/deployments_test.js index 6a0c405..5bdb91d 100644 --- a/test/deployments_test.js +++ b/test/deployments_test.js @@ -4,7 +4,7 @@ var util = require('../tasks/lib/util.js').init(grunt); module.exports = { replace_urls: function(test) { - test.expect(2); + test.expect(3); var search = 'http://loremipsum'; var replace = 'http://www.loremipsum.com'; @@ -20,7 +20,17 @@ module.exports = { test.equal( util.replace_urls(search, replace, string2), '{s:19:"payment_success_url";s:45:"http://www.loremipsum.com/payment-successful/";}http://www.loremipsum.com/hb', - "Replacing a mixes string, serialized or not." + "Replacing a mixed string, serialized or not." + ); + + search = 'http://loremipsum'; + replace = 'http://loremipsum.loremipsum.com'; + + var string3 = '{s:19:"payment_success_url";s:37:"http://loremipsum/payment-successful/";}http://loremipsum.loremipsum.com/hb'; + test.equal( + util.replace_urls(search, replace, string3), + '{s:19:"payment_success_url";s:52:"http://loremipsum.loremipsum.com/payment-successful/";}http://loremipsum.loremipsum.com/hb', + "Replacing a mixed string, serialized or not, with the source url contained into the replace url." ); test.done();