jotform / css.js

A lightweight, battle tested, fast, CSS parser in JavaScript
https://medium.com/jotform-form-builder/writing-a-css-parser-in-javascript-3ecaa1719a43
MIT License
426 stars 63 forks source link

Parser breaks on common templating syntax #29

Open Lixivial opened 4 years ago

Lixivial commented 4 years ago

We use Liquid to render our HTML for various media contexts. Part of our workflow has our design teams trying to provide common logic assignments to various parts of the HTML, including css. So they sometimes tend to use a placeholder variable in CSS.

Something like the following is a common use case:

.someClass { color: {{ some_placeholder }} }

In these cases, the parsing regex chokes on encountering unexpected {{ }} since they're not valid CSS syntax. I've looked through the code and produced the following regex adjustment and test case. This regex goes out of its way to maintain the existing behavior and not require code changes in css.js and fix the underlying problem of {{ }}.

https://regex101.com/r/t99mX4/1/

In addition, I've created a fork and am assembling a PR with more unit tests to cover this case. It doesn't cover some of the other issues filed here, like an empty @media query, but certainly could be expanded to do so with a bit of further refinement.