2

我正在尝试使用CSSParser解析 css 例如,我有 css:

 final String css = "@media screen and (color) { h1 { color: red } }";

我想得到这部分:

{ h1 { color: red } }

但是当我使用 mediaList 进行操作时

    final CSSStyleSheet sheet = parse(css);
    final CSSRule cssRule = sheet.getCssRules().item(0);
    final MediaList mediaList = ((CSSMediaRuleImpl) cssRule).getMedia();

我只得到

输出:屏幕和(颜色)

也许有人面临这个问题?

4

2 回答 2

0

我不是假装知道这是怎么回事,而是一个

({.*})

正则表达式可能会有所帮助。

于 2018-05-14T10:46:45.357 回答
0

如果有人需要回答:

@Test
public void testCSSParser2() throws IOException {
    String css = "@media only screen and (max-width: 600px) {body { background-color: lightblue } h1 { color: red } }";
    InputSource source = new InputSource(new StringReader(css));
    CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
    CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);
    CSSRuleList rules = sheet.getCssRules();
    for (int x = 0; x < rules.getLength(); x++) {
        final CSSMediaRule mediaRule = ((CSSMediaRule) rules.item(x));
        CSSRuleList mediaRules = mediaRule.getCssRules();
        for (int y = 0; y < mediaRules.getLength(); y++) {
            final CSSRule rule = mediaRules.item(y);
            System.out.println(rule.getCssText());

        }
    }

}
于 2018-05-16T10:19:14.100 回答