2

是否可以使用实时选择器检索原始 HTML(及其怪癖和格式)?

(def data "<div class=\"foo\"><p>some text <br> some more text</p></div>") 
(apply str 
    (enlive/emit* (enlive/select (enlive/html-snippet data) 
                                 [:.foo :> enlive/any-node])))

=> "<p>some text <br /> some more text</p>"

在此示例中,enlive 已将<br>标签转换为自闭合标签,这与原始输入片段不同。

我怀疑 enlive 正在将其转换为类似打嗝的标签列表,因此不幸的是原始信息丢失了。

4

2 回答 2

3

您的怀疑是正确的,enlive 在努力提供对 HTML 的一致抽象时会消耗这些信息。我不认为这是它旨在提供的功能。

于 2015-02-04T17:05:58.443 回答
1

尽管这可能只是切线相关,但如果您使用“附加”,您可以保留否则会被 net.cgrand.enlive-html/html-resource 丢弃的信息(例如评论)

https://github.com/cgrand/enlive/wiki/Table-and-Layout-Tutorial%2C-Part-3%3A-Simple-Transformations

<div id="wrapper">
   <!--body-->
</div>

jcrit.server=> (pprint 
             (transform layout [:#wrapper] 
                        (append page-content)))
({:tag :html,
     {:tag :div,
      :attrs {:id "wrapper"},
      :content
      ("\n       "
       {:type :comment, :data "body"}   ; <<== Still there.
       "\n    "
       {:tag :p, :content ("Hi, mom!")})}
     "\n")}
   "\n\n")})
于 2015-02-06T00:09:36.907 回答