Skip to content

Commit

Permalink
Fixed a multiple replacement that happens when the source pattern is …
Browse files Browse the repository at this point in the history
…contained into the replacement pattern.
  • Loading branch information
darioghilardi committed Nov 25, 2013
1 parent ef49497 commit 44de869
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion tasks/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
14 changes: 12 additions & 2 deletions test/deployments_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down

0 comments on commit 44de869

Please sign in to comment.