w3c / css-validator

W3C CSS Validation Service
https://jigsaw.w3.org/css-validator/
Other
204 stars 105 forks source link

UnescapeFilterReader replaces \r with \n causing invalid localization of errors #393

Closed AdrianSorop closed 1 year ago

AdrianSorop commented 1 year ago

I validated a file that had \r\n as end line separator. The localization of errors was off. I managed to reduce the problem to the org.w3c.css.util.UnescapeFilterReader. It seems like the reader replaces \r with \n

Here's the quick unit test for the issue:

public void testUnescapeFilterReader() throws Exception {

    String content = 
        "x {\r\n"
            + "  content:leader(a,b,c);\r\n"
            + "}";

    UnescapeFilterReader unescapeFilterReader = new UnescapeFilterReader(new StringReader(content));

    StringBuilder sb = new StringBuilder();
    int l = 0;
    char c[] = new char[1024];
    while ((l = unescapeFilterReader.read(c)) > 0) {
      sb.append(new String(c, 0, l));
    }
    unescapeFilterReader.close();

    assertEquals(
        "x {\n"
            + "  content:leader(a,b,c);\n"
            + "}", 
            sb.toString());
  }
ylafon commented 1 year ago

Input is processed per https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#input-preprocessing But yes, it leads to some mismatch when you apply the line/col numbers to the original stream, which is mainly an issue for partially minimised CSS.