Skip to content

Bug: Invalid replacement of similarly named images #6

Closed
@berimor

Description

@berimor

If there are references to similarly named image files (path to one file is a subpath to another one), one of both occurrences is replaced incorrectly.

Example:
.my-class-1 {
background-image: url("/folder1/my-image.png");
}
.my-class-2 {
background-image: url("/folder2/folder1/my-image.png");
}

Replacement result:

.my-class-1 {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAA...");
}
.my-class-2 {
background-image: url("/folder2data:image/png;base64,iVBORw0KGgoAAAA...");
}

This happens because the line:
source = source.replace(new RegExp(escapedRawUrl, 'g'), dataUri);
looks for any occurrences of the first imare url ignoring whether this is a full url (enclosed in quotes) or only a sub-url of another image.

Proposition: change the line to only replace full urls as follows:
source = source.replace(new RegExp('url(['"]\s_' + escapedRawUrl + '\s_['"])', 'g'), 'url("' + dataUri + '")');

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions