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

Suggestion: modular CSS regex checks #1252

Open
ayala-io opened this issue Feb 23, 2020 · 3 comments
Open

Suggestion: modular CSS regex checks #1252

ayala-io opened this issue Feb 23, 2020 · 3 comments

Comments

@ayala-io
Copy link
Contributor

While working on PR #1159, I found myself constantly refining how I checked for the CSS <number> value and often having to replace the same pattern in multiple places of the larger regex pattern for the isHSL() check. I realized I should have assembled the regex pattern in a modular way but then I thought this can be done at a more global level to support other CSS string validations (like isRgbColor()).

As a possible "feature request" or development direction, maybe it would be good to make a collection of common regex patterns that we can use to build more complex patterns. This is particularly applicable to CSS checks for isHSL() and isRgbColor() and other CSS-based checks which use the same <number>, <percentage>, and other value formats defined in the specification (see MDN documentation).

Now I'm not an expert in JS, so I'm not sure what would be the best way to achieve a modular regex pattern. A quick Google search led me to this: https://stackoverflow.com/questions/185510/how-can-i-concatenate-regex-literals-in-javascript which I suppose could be one way to do it.

@profnandaa
Copy link
Member

There's something that @hamzahejja did here, I don't know if it goes in line with what you are talking about? And how can we use it in the cases you mentioned?

@ayala-io
Copy link
Contributor Author

ayala-io commented Feb 25, 2020

Thank you for pointing that out. I'll keep that in mind moving forward. I think that would serve as part of what I was suggesting to finally "glue" together the final regex pattern composed of re-usable, modular patterns.

The re-usable patterns would be a set of globally available regex patterns (or I guess you can say sub-patterns) concerning a CSS <number> or <percentage> or <integer>, or any other CSS types/properties (there are some validators like isInt() as a candidate for code reuse but I'm not sure if it would validate a CSS-based <integer>). Then when coding validators such as isHSL(), isRgbColor(), and potentially other new CSS validators defined in validator.js, one can use the available sub-patterns and combine them or augment them via @hamzahejja's function.

This of course depends on the need for more CSS validators, which could be discussed here.

@profnandaa
Copy link
Member

profnandaa commented Feb 25, 2020 via email

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

No branches or pull requests

2 participants