【问题标题】:Get list of CSS rules that apply to a specific HTML class in Java获取适用于 Java 中特定 HTML 类的 CSS 规则列表
【发布时间】:2017-11-01 03:36:18
【问题描述】:

我正在使用CSS Parser 来获取属于一组 HTML 类的特定 CSS 规则。目前我在网站上有一个 CSS 规则列表,但是我不知道如何获得我正在寻找的规则。

当前代码:

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();

我的一个选择是执行 for 循环,但我不愿意这样做,因为
一个。如果一个页面中有数百条规则,它会很慢。
乙。似乎没有获取规则类名的方法。

任何帮助将不胜感激

【问题讨论】:

    标签: java css-parsing


    【解决方案1】:

    添加:CSS 已经被您的代码解析,因此您只需要查看在性能方面可能可以接受的选择器。

    添加 b:CSSStyleRule 接口缺少方法 getSelectors()CSSStyleRuleImpl 有。所以你可以尝试一下:

    scanRules(rules, name -> name.contains("e"), 
        (names, rule) -> System.out.println(
                new TreeSet<>(names) + " --> " + rule.getCssText()));
    

    使用递归辅助方法

    // scan CSS rules including rules contained in media rules 
    void scanRules(CSSRuleList rules, Predicate<String> classNameTest,
            BiConsumer<Set<String>, CSSStyleRule> ruleAction) {
        for (int ri = 0; ri < rules.getLength(); ri++) {
            CSSRule rule = rules.item(ri);
            if (rule.getType() == CSSRule.MEDIA_RULE) {
                CSSMediaRule mr = (CSSMediaRule) rule;
                scanRules(mr.getCssRules(), classNameTest, ruleAction);
            } else if (rule.getType() == CSSRule.STYLE_RULE) {
                CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule;
                SelectorList selectors = styleRule.getSelectors();
                // if (!styleRule.getSelectorText().contains(".name"))
                //  continue; // selector text test might cause speed up...
                for (int si = 0; si < selectors.getLength(); si++) {
                    Selector selector = selectors.item(si);
                    Set<String> classNames = classNamesInSelectorMatching(selector, classNameTest);
                    if (!classNames.isEmpty()) 
                        ruleAction.accept(classNames, styleRule);
                }
            }
        }
    }
    
    // find matching class names in given (potentially complex) selector
    Set<String> classNamesInSelectorMatching(Selector selector,
            Predicate<String> nameMatches) {
        switch (selector.getSelectorType()) {
            case Selector.SAC_CHILD_SELECTOR:
            case Selector.SAC_DESCENDANT_SELECTOR:
            case Selector.SAC_DIRECT_ADJACENT_SELECTOR: {
                DescendantSelector ds = (DescendantSelector) selector;
                Set<String> set = new HashSet<>();
                set.addAll(classNamesInSelectorMatching(ds.getAncestorSelector(), nameMatches));
                set.addAll(classNamesInSelectorMatching(ds.getSimpleSelector(), nameMatches));
                return set;
            }
            case Selector.SAC_NEGATIVE_SELECTOR: {
                NegativeSelector ns = (NegativeSelector) selector;
                return classNamesInSelectorMatching(ns.getSimpleSelector(), nameMatches);
            }
            case Selector.SAC_CONDITIONAL_SELECTOR: {
                ConditionalSelector ns = (ConditionalSelector) selector;
                return classNamesInConditionMatching(ns.getCondition(), nameMatches);
            }
            default:
                return Collections.emptySet();
        }
    }
    
    // find matching class names in given (potentially complex) condition
    Set<String> classNamesInConditionMatching(Condition condition,
            Predicate<String> nameMatches) {
        switch (condition.getConditionType()) {
            case Condition.SAC_CLASS_CONDITION: {
                AttributeCondition ac = (AttributeCondition) condition;
                if (nameMatches.test(ac.getValue()))
                    return Collections.singleton(ac.getValue());
                else
                    return Collections.emptySet();
            }
            case Condition.SAC_AND_CONDITION:
            case Condition.SAC_OR_CONDITION: {
                CombinatorCondition cc = (CombinatorCondition) condition;
                Set<String> set = new HashSet<>();
                set.addAll(classNamesInConditionMatching(cc.getFirstCondition(), nameMatches));
                set.addAll(classNamesInConditionMatching(cc.getSecondCondition(), nameMatches));
                return set;
            }
            case Condition.SAC_NEGATIVE_CONDITION: {
                NegativeCondition nc = (NegativeCondition) condition;
                return classNamesInConditionMatching(nc.getCondition(), nameMatches);
            }
            default:
                return Collections.emptySet();
        }
    }
    

    我已经尝试过输入 https://www.w3.org/2008/site/css/minimum-src.css,它似乎对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      相关资源
      最近更新 更多