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

Conversation

sontek
Copy link
Contributor

@sontek sontek commented Mar 15, 2017

Fixes #191 / adds test

@sontek sontek force-pushed the fix_embeded_parens_matching_recursively branch from 797199d to 07ea718 Compare March 15, 2017 21:43
@sontek
Copy link
Contributor Author

sontek commented Mar 15, 2017

@ekulabuhov @michael-ciniawsky @tquetano-r7 Please review

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

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

@sontek Thx for very much quickly jumping in on this 😛

@michael-ciniawsky
Copy link
Member

@bebraw @ekulabuhov ping 😛

fixUrls.js Outdated
@@ -29,8 +29,9 @@ module.exports = function (css) {
var currentDir = baseUrl + location.pathname.replace(/\/[^\/]*$/, "/");

// convert each url(...)
var fixedCss = css.replace(/url *\( *(.+?) *\)/g, function(fullMatch, origUrl) {
var fixedCss = css.replace(/url\(((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/g, 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.

pro-sauce regex :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. It would be better for the next coder if you split that up a bit (even in a comment). Scary to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be acceptable to bring in a dependency instead? This regex is only necessary because standard regex in the browser doesn't support recursive matching.

https://www.npmjs.com/package/xregexp could do this in a cleaner manner.

I'll try to explain it in a comment but its basically just a nasty way to recursively match brackets

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be fine to me (simpler code is better, also less to maintain this way). Better wait what others think.

Copy link
Member

Choose a reason for hiding this comment

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

That lib itself has no dependencies, is small and looks to be well maintained. Normally I'd say just use the regex but he has a point about browser regex being a bit gimp.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, let's go with a dep then. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment on there that might clear it up so we don't need the dep, what do you think?


/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

@ekulabuhov
Copy link
Member

Great work 👍 I think it's good to go.

@ekulabuhov
Copy link
Member

Noticed previous comments:
@sontek If you think that xregexp could make this simpler, then go with it. The breakdown you did is useful, but the intent is kinda of lost there.

@sontek
Copy link
Contributor Author

sontek commented Mar 16, 2017

#193 is to visualize how it'd look with xregexp

@sontek
Copy link
Contributor Author

sontek commented Mar 16, 2017

The code is simpler but in general I don't mind this PR, and it always sucks to add an extra dep

@michael-ciniawsky
Copy link
Member

I would also favour to not include an extra dependency, a comment should be enough, maybe include a link to a regexp visualisation service and good to go imho. (But haven't a strong opinion upon that) #192 #193 are both fine

@sontek
Copy link
Contributor Author

sontek commented Mar 16, 2017

I vote #192 to avoid the dep, we have this ticket + the comment in the PR to document the regex. Once you squint at it a bit its not that bad :)

@michael-ciniawsky michael-ciniawsky merged commit 71e0908 into webpack-contrib:master Mar 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants