w3c / css-validator

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

NullPointerException when using undeclared prefixes #391

Closed AdrianSorop closed 1 year ago

AdrianSorop commented 1 year ago

Here's a quick sample to reproduce the issue:

String css = 
        "prefix|div {\n" + 
        "    display:block;\n" + 
        "}";

    Reader reader = new StringReader(css);
    ApplContext applContext = new ApplContext("en");
    applContext.setCssVersion(CssVersion.CSS3.toString());
    applContext.setProfile(CssProfile.NONE.toString());
    applContext.setMedium("all");
    applContext.setWarningLevel(2);

    StyleSheetParser sheetParser = new StyleSheetParser(applContext);
    sheetParser.parseStyleSheet(applContext, reader, new URL("file:fake.css"));

The following error appears in console:

java.lang.NullPointerException
    at org.w3c.css.util.ApplContext.isNamespaceDefined(ApplContext.java:596)
    at org.w3c.css.parser.analyzer.CssParser.type_selector(CssParser.java:5042)
    at org.w3c.css.parser.analyzer.CssParser.compound_selector(CssParser.java:4754)
    at org.w3c.css.parser.analyzer.CssParser.complex_selector(CssParser.java:4648)
    at org.w3c.css.parser.analyzer.CssParser.selector_list(CssParser.java:4055)
    at org.w3c.css.parser.analyzer.CssParser.ruleSet(CssParser.java:4127)
    at org.w3c.css.parser.analyzer.CssParser.afterImportDeclaration(CssParser.java:819)
    at org.w3c.css.parser.analyzer.CssParser.parserUnit(CssParser.java:611)
    at org.w3c.css.parser.CssFouffa.parseStyle(CssFouffa.java:379)
    at org.w3c.css.css.StyleSheetParser.parseStyleElement(StyleSheetParser.java:329)
    at org.w3c.css.css.StyleSheetParser.parseStyleSheet(StyleSheetParser.java:428)

Reproduces on latest release and on main branch. The org.w3c.css.util.ApplContext.namespaces map is null.

The quick fix can be something like: HashMap<String,String> nsdefs = Optional.ofNullable(namespaces).map(t -> t.get(url)).orElse(null);

ylafon commented 1 year ago

Hi, this is relative to #390 and indeed a bug, looking into this

AdrianSorop commented 1 year ago

Hi, I've fixed it on my side but forgot to add an issue for the project. Thanks for taking time to fix it.