I'm using the CSS Parser to get specific CSS rules that belong to a set HTML class. At the moment I have got a list of CSS rules in the site, however I cannot figure out how to get the rules I'm looking for.
Current Code:
InputSource inputSource = new InputSource("example.com");
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
ErrorHandler errorHandler = new CSSErrorHandler();
parser.setErrorHandler(errorHandler);
CSSStyleSheet sheet = parser.parseStyleSheet(inputSource, null, null);
CSSRuleList rules = sheet.getCssRules();
One of my options would be doing a for loop, but I'm reluctant to do this because
a. It will be slow if there is hundreds of rules in a page.
b. There appears to be no method to get the class name of a rule.
Any help would be appreciated
Add a: The CSS has already been parsed by your code, so you only have to look at the selectors which might be acceptable in terms of performance.
Add b: The
CSSStyleRuleinterface lacks a methodgetSelectors()butCSSStyleRuleImplhas it. So you could try something along:with recursive helper methods
I have tried it with input https://www.w3.org/2008/site/css/minimum-src.css and it seems to work for me.