Search code examples
csskotlinhtmlunit

HtmlUnit Doesn't Recognize Valid CSS


So, I just started using HtmlUnit (literally just now), and I've already run into an error. I have some simple code:

fun getPage() { val page: Page = WebClient().getPage("http://htmlunit.sourceforge.net") }

However, it throws an extremely long error:

WARNING: CSS error: 'http://htmlunit.sourceforge.net/css/apache-maven-fluido-1.5.min.css' [9:90176] Error in declaration. '*' is not allowed as first char of a property.
Jan 24, 2018 4:14:58 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'http://htmlunit.sourceforge.net/css/apache-maven-fluido-1.5.min.css' [9:91151] Error in declaration. '*' is not allowed as first char of a property.
Jan 24, 2018 4:14:59 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Jan 24, 2018 4:15:00 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
//continues the above sequence a lot, and then ends with:

SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[http://htmlunit.sourceforge.net/js/apache-maven-fluido-1.5.min.js] line=[25] lineSource=[null] lineOffset=[0]

I looked at the link provided by the error, and it seems to contain a lot of asterisks inside of their CSS. 194, to be exact (including asterisks used in the commented area). I also experimented with loading other web pages, and it appears to be that it has issues understanding that the CSS of web-pages is valid.

Am I doing something wrong, or should I just tell it to ignore CSS? (I'm not sure if that's possible, but I would prefer it.)


Solution

  • At first regarding the css warning

    • it is a warning
    • see star hack for more info about this
    • if you still think this is valid css please open an CSSParser issue and i will have a look

    Second: An invalid or illegal selector was specified....

    This is really common if you are testing pages with HtmlUnit. The background is that many javascript libs (like sizzle used by jQuery) doing some checks to figure out what features are supported by your browser. And the output you see is the result of one of this checks. This one tries to figure out, if the javascript select method is able to work with a special kind of css-selectors. There is a try-catch around this selector usage in the javascript code and if the selector fails (in fact this happens in many browsers) the features is not available and the lib falls back to some other way to do the selection. The reason for seeing this in the error log is part of HtmlUnit history. This lib was initially designed for web testing and based on this the lib logs every javascript exception at the moment the exception is thrown (even if the exception is catched/handled later on). So you can more or less ignore this; the HtmlUnit does not stop working because of this.