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

Match parens recursively on URLs to not fix embeded calls #192

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion fixUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,35 @@ module.exports = function (css) {
var currentDir = baseUrl + location.pathname.replace(/\/[^\/]*$/, "/");

// convert each url(...)
var fixedCss = css.replace(/url *\( *(.+?) *\)/g, function(fullMatch, origUrl) {
/*
This regular expression is just a way to recursively match brackets within
a string.

/url\s*\( = Match on the word "url" with any whitespace after it and then a parens
( = Start a capturing group
(?: = Start a non-capturing group
[^)(] = Match anything that isn't a parentheses
| = OR
\( = Match a start parentheses
(?: = Start another non-capturing groups
[^)(]+ = Match anything that isn't a parentheses
| = OR
\( = Match a start parentheses
[^)(]* = Match anything that isn't a parentheses
\) = Match a end parentheses
) = End Group
*\) = Match anything and then a close parens
) = Close non-capturing group
* = Match anything
) = Close capturing group
\) = Match a close parens

/gi = Get all matches, not the first. Be case insensitive.
*/
var fixedCss = css.replace(/url\s*\(((?:[^)(]|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gi, function(fullMatch, origUrl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a pretty awesome site!

Copy link
Member

@joshwiens joshwiens Mar 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's how I survive dealing with any regex beyond the basics, second bookmark on my chrome bar.

Also super useful - https://regex101.com/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣 http://regexr.com / http://regexr.com/3fhgn here. Glad not to be the only one

// strip quotes (if they exist)
var unquotedOrigUrl = origUrl
.trim()
.replace(/^"(.*)"$/, function(o, $1){ return $1; })
.replace(/^'(.*)'$/, function(o, $1){ return $1; });

Expand Down
19 changes: 17 additions & 2 deletions test/fixUrlsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("fix urls tests", function() {
var resultCss = fixUrls(origCss, specialUrl || defaultUrl);
expectedCss = expectedCss || origCss;

assert.equal(resultCss, expectedCss);
assert.equal(expectedCss, resultCss);
};

// no change
Expand Down Expand Up @@ -100,11 +100,18 @@ describe("fix urls tests", function() {
// relative urls
it("Relative url", function() {
assertUrl(
"body { background-image:url(bg.jpg); }",
"body { background-image:url (bg.jpg); }",
"body { background-image:url(\"https://x.y.z/a/bg.jpg\"); }"
);
});

it("Relative url case sensitivity", function() {
assertUrl(
"body { background-image:URL (bg.jpg); }",
"body { background-image:url(\"https://x.y.z/a/bg.jpg\"); }"
);
});

it("Relative url with path", function() {
assertUrl(
"body { background-image:url(c/d/bg.jpg); }",
Expand Down Expand Up @@ -180,4 +187,12 @@ describe("fix urls tests", function() {
"http://x.y.z"
);
});

it("Doesn't break inline SVG", function() {
const svg = "url('data:image/svg+xml;charset=utf-8,<svg><feFlood flood-color=\"rgba(0,0,0,0.5)\" /></svg>')";

assertUrl(
"body: { background: " + svg + " }"
);
});
});