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

Fix regex in resolve-value.js to allow nested CSS functions #97

Merged
merged 17 commits into from Nov 25, 2019
Merged

Conversation

juliovedovatto
Copy link
Contributor

This fix will make work, when using nested CSS functions in the same declaration.

See #96

This fix will make work, when using nested CSS functions in the same declaration.

See #96
package.json Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look at this!

Needs some new test cases and pass existing tests

@MadLittleMods MadLittleMods changed the title fixed regex in resolve-value.js Fix regex in resolve-value.js to allow nested CSS functions Apr 17, 2019
@juliovedovatto
Copy link
Contributor Author

First, thank you @MadLittleMods for the opportunity to let me help with this small fix 😄

My Scenario:

Previously, I just did a hotfix, without testing it properly, because deadline was short. Due IE11 browser, we had to convert every var() to its own original value. We used https://material.io grid layout and it contains A LOT of CSS variables.

My new merge proposal:

I decided to change my approach, and improved the RegExp sentence. I added an extra instruction to help match nested functions with var() declarations. With a fallback using the original regex block.

In addition, I had to change your approach on how replace results. I added a while block (not sure if is a good idea), to replace until no var() is found.

To finish, I just updated nested-inside-other-func test, with two situations: one with a nested function with a declared var() and another with a undefined var(). Using the orginal RegExp it fails, using my proposal, it passes 🎉

Please check if my logic makes sense to you and if you can imagine any extra scenario to test.

Cheers from Brazil 🇧🇷

Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

A lot of change in the diff because of the new indentation. If there were any changes inside, please point them out with some comments

lib/resolve-value.js Outdated Show resolved Hide resolved
test/fixtures/nested-inside-other-func.css Show resolved Hide resolved
@juliovedovatto
Copy link
Contributor Author

juliovedovatto commented Apr 26, 2019

@MadLittleMods I made more code changes, just to adapt your logic in some special scenarios.

I was facing issues with RegExp to match few nested inside css functions.

I spent a good time trying to create a good regex to help, but due the fact JS RegExp engine is limited, I had to change my approach.

The match-recursive npm lib was added, to help match several var() declaration in the same line. Additionally, I changed a few chunks of the code to fit my logic into your code. Also, I changed the original regex in RE_VAR_FUNC variable, just to help fill variablesUsedInValue array.

I added more related tests and worked ok.

Could you take a look and make some tests? I believe this is a good candidate to merge 🤓

Cheers!

package.json preinstall instruction added, to avoid npm not installing required package dependencies.
@juliovedovatto
Copy link
Contributor Author

@MadLittleMods I did code changes again, please review. I think we are in the right track :)

Now, it will use a while loop and balanced-match npm lib to match var() declarations and resolve them. This should cover nested CSS functions.

In addition, I added one more test for css nested functions. It should be all covered now.

Cheers!

Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

Thanks for the changes and extra tests for our confidence. Some new review points to address

package.json Outdated Show resolved Hide resolved
lib/resolve-value.js Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
test/fixtures/nested-inside-other-func.css Show resolved Hide resolved
@juliovedovatto
Copy link
Contributor Author

@MadLittleMods Another code review round done. Please check.

@juliovedovatto
Copy link
Contributor Author

@MadLittleMods any updates?

Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

Sorry for the delay. Here is the latest

lib/resolve-value.js Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
@colinhowells
Copy link

Thanks everyone for your hard work on this – I've been watching this bit of the HiQ framework

--radio-border-width: var(--hiq-radio-border-width, 1px) solid var(--hiq-radio-border-color, transparent);
--radio-border-color: var(--hiq-radio-border-color, transparent);
border: var(--radio-border-width) solid var(--radio-border-color);

get compiled to:

border: 1px) solid var(--hiq-radio-border-color, transparent solid transparent;

and had no clue why. If it was defeated by regex, that'd make sense ...

@juliovedovatto
Copy link
Contributor Author

juliovedovatto commented Jun 2, 2019

@MadLittleMods Done with the requested changes. Could you take a look? :)

@colinhowells You are right, it was defeated by regex. The intend of my proposal is to fix when some complex var() usage, specially when nested declarations appear.

lib/resolve-value.js Outdated Show resolved Hide resolved
lib/resolve-value.js Outdated Show resolved Hide resolved
@@ -27,26 +29,61 @@ function toString(value) {
var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
var debugIndent = _debugIsInternal ? '\t' : '';

var matchingVarDecl = undefined;
var RE_VAR_FUNC_G = new RegExp(RE_VAR_FUNC.source, 'g');
Copy link
Owner

Choose a reason for hiding this comment

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

We can get rid of RE_VAR_FUNC_G and RE_VAR_FUNC now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RE_VAR_FUNC_G was removed.

But, It seems we can't remove RE_VAR_FUNC, since it is being called in index.js.

var doesRuleUseVariables = rule.nodes.some(function(node) {
if(node.type === 'decl') {
var decl = node;
// If it uses variables
// and is not a variable declarations that we may be preserving from earlier
if(resolveValue.RE_VAR_FUNC.test(decl.value) && !RE_VAR_PROP.test(decl.prop)) {
return true;
}
}
return false;
});

lib/resolve-value.js Outdated Show resolved Hide resolved
Copy link
Owner

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

Looking good, just a few remaining points 👍

@juliovedovatto
Copy link
Contributor Author

@MadLittleMods I did some of your requests, could you review again?

Also, check my comment #97 (diff)

@juliovedovatto
Copy link
Contributor Author

@MadLittleMods any news?

@juliovedovatto
Copy link
Contributor Author

@MadLittleMods sorry to bump this PR, but could you take a look in the latest changes sent?

lib/resolve-value.js Outdated Show resolved Hide resolved
@MadLittleMods
Copy link
Owner

I've left this for a while. Just feeling the effects of going back and fourth with things not actually changing or being quite finished.

I only spotted one thing and the tests are passing so we can merge after that 😥

@juliovedovatto
Copy link
Contributor Author

No worries @MadLittleMods

I just did the request change. Feel free to to approve ;)

@MadLittleMods MadLittleMods merged commit 046839f into MadLittleMods:master Nov 25, 2019
MadLittleMods added a commit that referenced this pull request Nov 25, 2019
@MadLittleMods
Copy link
Owner

Thanks for the quick update @juliovedovatto ❤️

This is now part of postcss-css-variables@0.14.0 🎉, https://github.com/MadLittleMods/postcss-css-variables/blob/master/CHANGELOG.md#v0140---2019-11-24

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

Successfully merging this pull request may close these issues.

None yet

3 participants