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

Calc with internal parentheses being deleted. #5

Open
Hazzard13 opened this issue Apr 19, 2021 · 2 comments
Open

Calc with internal parentheses being deleted. #5

Hazzard13 opened this issue Apr 19, 2021 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@Hazzard13
Copy link

Pardon the unusually high specificity of this bug, but this has been a serious blocker for me, and has cost me most of a day.

Basically, from my research I've found that any line of CSS including parentheses inside a calc statement will be removed. To test, all I'm doing with my CSS currently is parsing and printing it (Using the current v2.2.0 version, ofc):

stylesheet = cssutils.parseString(css_contents)
print(stylesheet.cssText.decode("utf-8"))

In my findings, even lengthy calcs like this will be left alone: font-size: calc(15px * 100vw - 600px / 1320);, but the moment any brackets are introduced it will fail, like this line: font-size: calc((100vw - 600px) * 10);.

Functions like this are supposedly meant to be handled according to the documents linked in the README, look at Example 29 for particulars. Regarding that same example, I've also tried nested calcs in the same fashion, to no avail: font-size: calc(calc(100vw - 600px) * 10);.

Basically what I'm asking, is if there's any way for this bug to be fixed or worked around? Ideally, I'd love something I can do today, to either skip validating these lines or to find a way to get them to pass without changing the meaning.

@Hazzard13
Copy link
Author

I've taken a good crack at this myself, but been unable to make too much headway, due to simply not being familiar with the codebase. Regardless, here's the fruit of my labour and what I assume needs to be done here.

def _productiondefinition(self):
"""Return defintion used for parsing."""
types = self._prods # rename!
def toSeq(t, tokens):
"Do not normalize function name!"
return t[0], t[1]
funcProds = Sequence(
Prod(name='calc', match=lambda t, v: t == types.FUNCTION, toSeq=toSeq),
Sequence(
Choice(
Prod(
name='nested function',
match=lambda t, v: t == self._prods.FUNCTION,
toSeq=lambda t, tokens: (
CSSFunction._functionName,
CSSFunction(cssutils.helper.pushtoken(t, tokens)),
),
),
Prod(
name='part',
match=lambda t, v: v != ')',
toSeq=lambda t, tokens: (t[0], t[1]),
),
),
minmax=lambda: (0, None),
),
PreDef.funcEnd(stop=True),
)
return funcProds

def _setCssText(self, cssText):
self._checkReadonly()
types = self._prods # rename!
_operator = Choice(
Prod(
name='Operator */',
match=lambda t, v: v in '*/',
toSeq=lambda t, tokens: (t[0], t[1]),
),
Sequence(
PreDef.S(),
Choice(
Sequence(
Prod(
name='Operator */',
match=lambda t, v: v in '*/',
toSeq=lambda t, tokens: (t[0], t[1]),
),
PreDef.S(optional=True),
),
Sequence(
Prod(
name='Operator +-',
match=lambda t, v: v in '+-',
toSeq=lambda t, tokens: (t[0], t[1]),
),
PreDef.S(),
),
PreDef.funcEnd(stop=True, mayEnd=True),
),
),
)
_operant = lambda: Choice(_DimensionProd(self), _CSSVariableProd(self)) # noqa
prods = Sequence(
Prod(
name='CALC',
match=lambda t, v: t == types.FUNCTION and normalize(v) == 'calc(',
),
PreDef.S(optional=True),
_operant(),
Sequence(_operator, _operant(), minmax=lambda: (0, None)),
PreDef.funcEnd(stop=True),
)
# store: name of variable
ok, seq, store, unused = ProdParser().parse(
cssText, 'CSSCalc', prods, checkS=True
)
self.wellformed = ok
if ok:
self._setSeq(seq)
cssText = property(
lambda self: cssutils.ser.do_css_CSSCalc(self),
_setCssText,
doc="String representation of calc function.",
)
type = property(lambda self: Value.CALC, doc="Type is fixed to Value.CALC.")

Basically, somehow CalcValue and CSSCalc needs to recursively refer to themselves, with an Operator of either "(" or "calc(". The actual handling of calc as it currently exists is perfectly adequate at handling all other cases, at least in my testing and experience. Additionally, this particular check for a closing bracket probably needs to be removed or reworked:

match=lambda t, v: v != ')',

I've been unable to get anything to build that functions substantially different, but hopefully someone on the team can pick this up and move it further. If that's you, thanks so much for your time and effort in this library.

@jaraco
Copy link
Owner

jaraco commented May 24, 2021

I've adopted maintenance of this package, but unfortunately, I don't know the codebase much at all. I appreciate the work you've done and welcome you or others to do more.

@jaraco jaraco added the help wanted Extra attention is needed label May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants