下面列出了org.w3c.dom.stylesheets.MediaList#org.w3c.dom.css.CSSRuleList 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void init() {
if (style != null) {
String styleContent = style.getValue().getText();
if (styleContent != null && !styleContent.isEmpty()) {
InputSource source = new InputSource(new StringReader(styleContent));
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
parser.setErrorHandler(new ParserErrorHandler());
try {
styleSheet = parser.parseStyleSheet(source, null, null);
cssFormat = new CSSFormat().setRgbAsHex(true);
CSSRuleList rules = styleSheet.getCssRules();
for (int i = 0; i < rules.getLength(); i++) {
final CSSRule rule = rules.item(i);
if (rule instanceof CSSStyleRuleImpl) {
styleRuleMap.put(((CSSStyleRuleImpl) rule).getSelectorText(), (CSSStyleRuleImpl) rule);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static String processStyles(String tag, String clazz, String style, CSSRuleList rules, int stype) {
for (int i = 0; rules != null && i < rules.getLength(); i++) {
CSSRule rule = rules.item(i);
switch (rule.getType()) {
case CSSRule.STYLE_RULE:
CSSStyleRuleImpl srule = (CSSStyleRuleImpl) rule;
for (int j = 0; j < srule.getSelectors().getLength(); j++) {
Selector selector = srule.getSelectors().item(j);
if (selector.getSelectorType() != stype)
continue;
switch (selector.getSelectorType()) {
case Selector.SAC_ELEMENT_NODE_SELECTOR:
ElementSelectorImpl eselector = (ElementSelectorImpl) selector;
if (tag == null
? eselector.getLocalName() == null
: tag.equals(eselector.getLocalName()))
style = mergeStyles(style, srule.getStyle().getCssText());
break;
case Selector.SAC_CONDITIONAL_SELECTOR:
ConditionalSelectorImpl cselector = (ConditionalSelectorImpl) selector;
if (cselector.getCondition().getConditionType() == SAC_CLASS_CONDITION) {
ClassConditionImpl ccondition = (ClassConditionImpl) cselector.getCondition();
if (clazz.equals(ccondition.getValue()))
style = mergeStyles(style, srule.getStyle().getCssText());
}
break;
}
}
break;
case CSSRule.MEDIA_RULE:
CSSMediaRuleImpl mrule = (CSSMediaRuleImpl) rule;
if (isScreenMedia(mrule.getMedia()))
style = processStyles(tag, clazz, style, mrule.getCssRules(), stype);
break;
}
}
return style;
}
private void initCssRules() {
if (cssRules_ == null) {
cssRules_ = new com.gargoylesoftware.htmlunit.javascript.host.css.CSSRuleList(this);
cssRulesIndexFix_ = new ArrayList<>();
refreshCssRules();
}
}
/**
* Retrieves the collection of rules defined in this style sheet.
* @return the collection of rules defined in this style sheet
*/
@JsxGetter({IE, CHROME})
public com.gargoylesoftware.htmlunit.javascript.host.css.CSSRuleList getRules() {
return getCssRules();
}
/**
* Returns the collection of rules defined in this style sheet.
* @return the collection of rules defined in this style sheet
*/
@JsxGetter
public com.gargoylesoftware.htmlunit.javascript.host.css.CSSRuleList getCssRules() {
initCssRules();
return cssRules_;
}
public CSSRuleList getCssRules( )
{
return null;
}