Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stringMap: Unexpected replacement when search value includes dot #24

Closed
ocean90 opened this issue Feb 27, 2015 · 1 comment
Closed

stringMap: Unexpected replacement when search value includes dot #24

ocean90 opened this issue Feb 27, 2015 · 1 comment
Labels

Comments

@ocean90
Copy link
Contributor

ocean90 commented Feb 27, 2015

I have this map:

{
    name: 'import-rtl-stylesheet',
    search: [ '.css' ],
    replace: [ '-rtl.css' ],
    options: {
        scope: 'url',
        ignoreCase: false
    }
}

The map should replace @import url(common.css); with @import url(common-rtl.css);. That works great.
But the map also changes
@import url("//fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,600,700");
to
@import url("//fonts.googleapis.com.css?family=Open+Sans:400italic,700italic,400,600,700");
(Note the slash which is replaced with a dot.)

Since search and replace is passed to RegExp I have escaped the dot (search: [ '\.css' ]) but stil getting the same output.

Any idea what the issue is?

@MohammadYounes
Copy link
Owner

Your search term will match any character followed by "css"

"a.b/css".match(/(.css)/);` // will match "/css"
"a.css".match(/(.css)/);` // will match ".css"

Using the examples you provided, escaping the dot (search:'\\.css') will work with @import url("//fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,600,700"); because there will be no match. But will fail in @import url(common.css);, since the match .css will be replaced by the search term "\.css" producing @import url(common\.css);

This is because the StringMap is bi-directional, it matches both search and replace then checks if the match is equal to search term, if yes its replaced with replace term, if not it will assume the match was for the replace term and replace it with search term.

Not sure if the explanation was clear enough! But I should have escaped the stringMap terms internally from the start.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants