gpt4 book ai didi

css - Apache Batik 中的查询选择器

转载 作者:行者123 更新时间:2023-11-28 11:43:34 26 4
gpt4 key购买 nike

我想在 Apache Batik DOM 中查询与 CSS 选择器匹配的元素。

Batik 是否提供了以下任何浏览器 DOM 方法的替代方法?

最佳答案

好的,这是我设法组合的解决方案。它是用 Clojure 而不是 Java 编写的,但重要的部分是:

  1. 实例化 org.apache.batik.css.engine.sac.CSSConditionFactory
  2. 实例化 org.apache.batik.css.parser.Parser
  3. 调用 Parser.parseSelectors
  4. 调用 org.apache.batik.dom.traversal.TraversalSupport.createNodeIterator
  5. 在您的 NodeFilter 中,遍历已解析的 SelectorList,调用 ExtendedSelector.match
  6. 有条件地跳过从迭代器返回的第一个节点(它始终是遍历根)
(def ^:private condition-factory
(CSSConditionFactory. nil "class" nil "id"))

(defn- parse-selector [selector]
(let [parser (Parser.)]
(doto parser
(.setSelectorFactory CSSSelectorFactory/INSTANCE)
(.setConditionFactory condition-factory))
(.parseSelectors parser selector)))

(defn- matches?
([selector element] (matches? selector element ""))
([selector element pseudo]
(let [length (.getLength selector)]
(loop [i 0]
(if (< i length)
(if (.. selector (item i) (match element pseudo))
true
(recur (inc i)))
false)))))

(defn selection-seq [root selector]
(let [selector (parse-selector selector)
iterator (.createNodeIterator (TraversalSupport.)
(.getOwnerDocument root)
root
NodeFilter/SHOW_ELEMENT
(reify NodeFilter
(acceptNode [_ element]
(if (matches? selector element)
NodeFilter/FILTER_ACCEPT
NodeFilter/FILTER_REJECT)))
false)
node-seq ((fn step []
(lazy-seq
(when-let [node (.nextNode iterator)]
(cons node (step))))))]
;; Iterator always returns the reference node, so match it.
(when-let [node (first node-seq)]
(if (matches? selector (first node-seq))
node-seq
(next node-seq)))))

关于css - Apache Batik 中的查询选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14407817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com