如何在@media 中获取选择器?
How to get selector inside @media?
我正在尝试使用 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();
我只得到
输出:屏幕和(颜色)
也许有人遇到过这个问题?
我不是假装知道这是怎么回事,但是
一个
({.*})
正则表达式可能有帮助。
如果有人需要回答:
@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());
}
}
}
我正在尝试使用 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();
我只得到
输出:屏幕和(颜色)
也许有人遇到过这个问题?
我不是假装知道这是怎么回事,但是 一个
({.*})
正则表达式可能有帮助。
如果有人需要回答:
@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());
}
}
}