2

Is it possible to retrieve the original HTML (with its quirks and formatting) using enlive selectors?

(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>"

In this example, enlive has transformed the <br> tag into a self-closing tag, unlike the original input snippet.

I suspect that enlive is transforming it into a hiccup-like list of tags, such that the original information is unfortunately lost.

George Armhold
  • 30,824
  • 50
  • 153
  • 232

2 Answers2

3

Your suspicion is correct, enlive consumes this information in it's efforts to provide a consistent abstraction over the HTML. I don't think this was a feature it was designed to offer.

George Armhold
  • 30,824
  • 50
  • 153
  • 232
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
1

Although this is perhaps only tangentially related, if you use "append" you can preserve information (such as comments) that would otherwise be thrown out by 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")})
sova
  • 5,468
  • 10
  • 40
  • 48