shady-css-parser icon indicating copy to clipboard operation
shady-css-parser copied to clipboard

Comments and whitespace in a complicated selector are not seen by stringifier

Open dfreedm opened this issue 9 years ago • 3 comments

Given a sheet

.foo,
.bar,
/* comment */
.baz {
  border: 2px solid black;
}

and a stringifier

class Stringifer extends shadyCssParser.Stringifier {
  comment() {
    return '';
  }
}

Output is expected to be

.foo,.bar,.baz{border:2px solid black;}

but is instead

.foo,
.bar,
/* comment */
.baz {border:2px solid black;}

dfreedm avatar May 10 '16 22:05 dfreedm

This is slightly tricky to fix in the parser without adding a significant number of costly conditional checks, but it's actually quite trivial and cheap to fix with a specialized Tokenizer. tokenizeComment could be specialized to always return a zero-length token that starts and ends at the detected offset of the comment token.

In order to support this, the Parser would have to accept a specialized Tokenizer implementation. I'm going to add this for the near term, which should allow you to address this case. If there is a more elegant solution or preferred ergonomics to be had, I'm open to considering it.

cdata avatar May 15 '16 18:05 cdata

An example of the specialized tokenizeComment method:

tokenizeComment(...args) {
  let token = super.tokenizeComment(...args);
  // Force token to be zero length
  token.start = token.end;
  return token;
}

cdata avatar May 15 '16 18:05 cdata

Okay, now that I think about it more, this would not be a sufficient solution. We only look at the start and end tokens when slicing chunks of the string, so the ranges in any interstitial comment tokens won't matter.

cdata avatar May 15 '16 18:05 cdata