Escaping an Issue with Grunt “string-replace”

Since I started using Grunt, I have found more and more cases where Grunt tasks turn out to be very useful and a huge timesaver. That is, timesaver once you got whatever you’re trying to do up and running. In the most recent case, I ran into an issue w…


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream

Since I started using Grunt, I have found more and more cases where Grunt tasks turn out to be very useful and a huge timesaver. That is, timesaver once you got whatever you’re trying to do up and running. In the most recent case, I ran into an issue with using Grunt string-replace to replace background-image URLs in my CSS file with a different path to prepare them for use with Rails" asset pipeline.

I needed to search for : url(img/imagename) to then be replaced with : url(<%= asset_path 'img/imagename' %>).

As the first replacement pattern I used ':$1url(<%= asset_path \'$2$3\' %>)’. This failed with an error message. Since I was using the < & > characters in the pattern, I figured these characters might need to be escaped.

I tried ':$1url(\<%= asset_path \'$2$3\' %\>)’ and various variations, all without any luck. Eventually I found a hint on Stack Overflow, that this might not work this way at all and needs to be processed with two passes.

After checking back with the author of string-replace, this problem seems to be related to Lo-Dash's template security.

The final working code looks like this now and works pretty much as well as it would with running it in one pass, I believe.

  'string-replace': {
    inline: {
      files: {
        'dist/css/app.css.erb': ['css/app.css'],
      },
      options: {
        replacements: [{
          pattern: /:(\s*)url\((img)(.+?)\)/ig,
          replacement: ':$1url(\<%= asset_path \'$2$3\' CLOSETAG)'
        },
        {
          pattern: /CLOSETAG/g,
          replacement: '%>'
        }]
      }
    }
  }


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream


Print Share Comment Cite Upload Translate Updates
APA

foobartel.com :: Stream | Sciencx (2014-11-01T23:00:00+00:00) Escaping an Issue with Grunt “string-replace”. Retrieved from https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/

MLA
" » Escaping an Issue with Grunt “string-replace”." foobartel.com :: Stream | Sciencx - Saturday November 1, 2014, https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/
HARVARD
foobartel.com :: Stream | Sciencx Saturday November 1, 2014 » Escaping an Issue with Grunt “string-replace”., viewed ,<https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/>
VANCOUVER
foobartel.com :: Stream | Sciencx - » Escaping an Issue with Grunt “string-replace”. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/
CHICAGO
" » Escaping an Issue with Grunt “string-replace”." foobartel.com :: Stream | Sciencx - Accessed . https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/
IEEE
" » Escaping an Issue with Grunt “string-replace”." foobartel.com :: Stream | Sciencx [Online]. Available: https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/. [Accessed: ]
rf:citation
» Escaping an Issue with Grunt “string-replace” | foobartel.com :: Stream | Sciencx | https://www.scien.cx/2014/11/01/escaping-an-issue-with-grunt-string-replace-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.