### Node.js xmldom Setup Source: https://github.com/clj-commons/hickory/blob/master/README.md Configures the global `js/DOMParser` object in a Node.js environment using the `xmldom` library. This setup is required for Hickory to parse markup on Node.js when `xmldom` is used as the DOM implementation. ```javascript (set! js/DOMParser (.-DOMParser (cljs.nodejs/require "xmldom"))) ``` -------------------------------- ### Node.js jsdom Setup Source: https://github.com/clj-commons/hickory/blob/master/README.md Configures the global `js/document` object in a Node.js environment using the `jsdom` library. This is necessary for Hickory to parse markup on Node.js when `jsdom` is used as the DOM implementation. ```javascript (set! js/document (.jsdom (cljs.nodejs/require "jsdom"))) ``` -------------------------------- ### Clojure Example: Selecting Nested Data with Descendant and Combinators Source: https://github.com/clj-commons/hickory/blob/master/README.md Demonstrates how to use Hickory selectors and combinators in Clojure to navigate and extract specific data from an HTML structure. This example selects a driver's name from a table using a combination of descendant, class, tag, and positional selectors. ```clojure user=> (-> (s/select (s/descendant (s/class "subModule") (s/class "standings") (s/and (s/tag :tr) s/first-child) (s/and (s/tag :td) (s/nth-child 2)) (s/tag :a)) site-htree) first :content first string/trim) "Sebastian Vettel" ``` -------------------------------- ### Clojure Example: Selecting Formula 1 Race Dates Source: https://github.com/clj-commons/hickory/blob/master/README.md Demonstrates how to use Hickory selectors to parse HTML content from a URL and extract specific data, such as race dates. It involves fetching content using clj-http, parsing it with hickory.core, and then applying selectors from hickory.select to pinpoint the desired information. ```clojure user=> (use 'hickory.core) nil user=> (require '[hickory.select :as s]) nil user=> (require '[clj-http.client :as client]) nil user=> (require '[clojure.string :as string]) nil user=> (def site-htree (-> (client/get "http://formula1.com/default.html") :body parse as-hickory)) #'user/site-htree user=> (-> (s/select (s/child (s/class "subCalender") ; sic (s/tag :div) (s/id :raceDates) s/first-child (s/tag :b)) site-htree) first :content first string/trim) "10, 11, 12 May 2013" ``` -------------------------------- ### HTML Tree Manipulation with Zippers Source: https://github.com/clj-commons/hickory/blob/master/README.md Illustrates using Hickory's zipper integration with `hiccup-zip` and `clojure.zip` functions to navigate, modify, and retrieve parts of an HTML tree. It shows replacing an element and getting the modified root. ```clojure user=> (use 'hickory.zip) nil user=> (require '[clojure.zip :as zip]) nil user=> (require '[hickory.render :refer [hickory-to-html]]) nil user=> (-> (hiccup-zip (as-hiccup (parse "bar
"))) zip/node) ([:html {} [:head {}] [:body {} [:a {:href "foo"} "bar" [:br {}]]]]) user=> (-> (hiccup-zip (as-hiccup (parse "bar
"))) zip/next zip/node) [:html {} [:head {}] [:body {} [:a {:href "foo"} "bar" [:br {}]]]] user=> (-> (hiccup-zip (as-hiccup (parse "bar
"))) zip/next zip/next zip/node) [:head {}] user=> (-> (hiccup-zip (as-hiccup (parse "bar
"))) zip/next zip/next (zip/replace [:head {:id "a"}]) zip/node) [:head {:id "a"}] user=> (-> (hiccup-zip (as-hiccup (parse "bar
"))) zip/next zip/next (zip/replace [:head {:id "a"}]) zip/root) ([:html {} [:head {:id "a"}] [:body {} [:a {:href "foo"} "bar" [:br {}]]]]) user=> (-> (hickory-zip (as-hiccup (parse "bar
"))) zip/next zip/next (zip/replace {:type :element :tag :head :attrs {:id "a"} :content nil}) zip/root) {:type :document, :content [{:type :element, :attrs nil, :tag :html, :content [{:content nil, :type :element, :attrs {:id "a"}, :tag :head} {:type :element, :attrs nil, :tag :body, :content [{:type :element, :attrs {:href "foo"}, :tag :a, :content ["bar" {:type :element, :attrs nil, :tag :br, :content nil}]}]}]}]} user=> (hickory-to-html *1) "bar
" ``` -------------------------------- ### Parsing HTML and Converting to Hiccup/Hickory Source: https://github.com/clj-commons/hickory/blob/master/README.md Demonstrates parsing an HTML document and converting it to both Hiccup and Hickory formats using `parse`, `as-hiccup`, and `as-hickory`. It also shows parsing an HTML fragment and processing the resulting list of nodes. ```clojure user=> (use 'hickory.core) nil user=> (def parsed-doc (parse "foo")) #'user/parsed-doc user=> (as-hiccup parsed-doc) ([:html {} [:head {}] [:body {} [:a {:href "foo"} "foo"]]]) user=> (as-hickory parsed-doc) {:type :document, :content [{:type :element, :attrs nil, :tag :html, :content [{:type :element, :attrs nil, :tag :head, :content nil} {:type :element, :attrs nil, :tag :body, :content [{:type :element, :attrs {:href "foo"}, :tag :a, :content ["foo"]}]}]}]} user=> (def parsed-frag (parse-fragment "foo bar")) #'user/parsed-frag user=> (as-hiccup parsed-frag) IllegalArgumentException No implementation of method: :as-hiccup of protocol: #'hickory.core/HiccupRepresentable found for class: clojure.lang.PersistentVector clojure.core/-cache-protocol-fn (core_deftype.clj:495) user=> (map as-hiccup parsed-frag) ([:a {:href "foo"} "foo"] " " [:a {:href "bar"} "bar"]) user=> (map as-hickory parsed-frag) ({:type :element, :attrs {:href "foo"}, :tag :a, :content ["foo"]} " " {:type :element, :attrs {:href "bar"}, :tag :a, :content ["bar"]}) ``` -------------------------------- ### Hickory Selector Combinators Overview Source: https://github.com/clj-commons/hickory/blob/master/README.md Lists and briefly describes various selector combinators available in the Hickory library. These combinators are used to build complex selection logic by combining simpler selectors. ```APIDOC Selector Combinators: - `and`: Takes any number of selectors, returns a selector that only selects nodes for which all argument selectors are true. - `or`: Takes any number of selectors, returns a selector that only selects nodes for which at least one argument selector is true. - `not`: Takes a single selector, returns a selector that only selects nodes that its argument selector does not. - `el-not`: Takes a single selector, returns a selector that only selects element nodes that its argument selector does not. - `child`: Takes any number of selectors, returns true when the zipper location is at the end of a chain of direct child relationships specified by the selectors. - `descendant`: Takes any number of selectors, returns true when the zipper location is at the end of a chain of descendant relationships specified by the selectors. ``` -------------------------------- ### Hickory Selectors API Reference Source: https://github.com/clj-commons/hickory/blob/master/README.md Provides detailed descriptions of various selector functions available in the hickory.select namespace. These functions act as predicates on zipper locations of hickory-format data, enabling powerful querying capabilities. ```APIDOC Hickory Selectors API: Selectors are functions that take a zipper loc from a hickory HTML tree and return the loc if the selector applies, or nil otherwise. They are often combined using combinators. node-type: Selects nodes based on their :type field. - Signature: (node-type type-keyword-or-string) - Example: (node-type :comment) tag: Selects nodes based on their :tag field. - Signature: (tag tag-keyword-or-string) - Example: (tag :div) attr: Selects nodes based on attributes in the :attrs map. - Signature: (attr attribute-key [value-predicate]) - The optional value-predicate is a function that takes the attribute value and returns true if it matches. - Example: (attr :id #(.startsWith % "foo")) id: Selects nodes by their :id attribute (case-insensitive). - Signature: (id id-string) - Example: (id :raceDates) class: Selects nodes that have a specific class in their :class attribute. - Signature: (class class-string) - Example: (class :foo) any: Selects any element node, similar to CSS's '*'. - Signature: any - Example: s/any element: Equivalent to 'any', explicitly selects element nodes. - Signature: element - Example: s/element root: Selects the root node of the tree. - Signature: root - Example: s/root n-moves-until: Selects a node if its distance from a boundary matches a pattern. - Signature: (n-moves-until n c move-fn boundary-fn) - n, c: Define the counting pattern (nk+c). - move-fn: Zipper movement function. - boundary-fn: Function to check for the boundary. - Example: (s/n-moves-until 2 0 s/down s/right) nth-of-type: Selects nodes that are the nth child of a given tag type. - Signature: (nth-of-type tag-or-type n [c | :odd | :even]) - n, c: Define the position (nk+c). - :odd/:even: Keywords for selecting odd/even positioned children. - Example: (s/nth-of-type :div 1 0) ; Selects the first div child - Example: (s/nth-of-type :p :odd) ; Selects odd-positioned p children nth-last-of-type: Similar to nth-of-type but counts backwards from the last sibling. - Signature: (nth-last-of-type tag-or-type n [c | :odd | :even]) - Example: (s/nth-last-of-type :li 2) ; Selects the second to last li child ``` -------------------------------- ### Add Hickory Dependency Source: https://github.com/clj-commons/hickory/blob/master/README.md Specifies the dependency for the Hickory library in a Clojure project. This snippet shows the format for adding the library to a `project.clj` file or an equivalent build tool configuration. ```clojure [org.clj-commons/hickory "0.7.3"] ``` -------------------------------- ### Hickory Parsing Functions Source: https://github.com/clj-commons/hickory/blob/master/API.md Functions for parsing HTML into Hickory DOM structures. `parse` handles entire documents, while `parse-fragment` handles HTML fragments. `parse-dom-with-write` uses `document.write` for parsing. ```clojure (parse html-string) ;; Parse an entire HTML document into a DOM structure that can be used as input to as-hiccup or as-hickory. ``` ```clojure (parse-dom-with-domparser html-string) ;; Parse an HTML document (or fragment) as a DOM using DOMParser. ``` ```clojure (parse-dom-with-write html-string) ;; Parse an HTML document (or fragment) as a DOM using document.implementation.createHTMLDocument and document.write. ``` ```clojure (parse-fragment html-fragment-string) ;; Parse an HTML fragment (some group of tags that might be at home somewhere in the tag hierarchy under ) into a list of DOM elements that can each be passed as input to as-hiccup or as-hickory. ``` -------------------------------- ### Hickory Core Conversion Functions Source: https://github.com/clj-commons/hickory/blob/master/API.md Functions for converting Hickory nodes to different data formats. `as-hiccup` converts to Hiccup format, while `as-hickory` converts to Hickory format. Nodes must implement the respective protocols. ```clojure (as-hiccup this) ;; Converts the node given into a hiccup-format data structure. ;; The node must have an implementation of the HiccupRepresentable protocol; ;; nodes created by parse or parse-fragment already do. ``` ```clojure (as-hickory this) ;; Converts the node given into a hickory-format data structure. ;; The node must have an implementation of the HickoryRepresentable protocol; ;; nodes created by parse or parse-fragment already do. ``` -------------------------------- ### Hickory Parsing and Conversion API Source: https://github.com/clj-commons/hickory/blob/master/README.md Hickory offers functions to parse HTML strings into different data formats and convert between them. The `parse` and `parse-fragment` functions have flexible return types, allowing conversion to Hiccup or Hickory formats using `as-hiccup` or `as-hickory`. Note that conversions may not always be exact or roundtripable and might trigger a full HTML reparse. ```APIDOC Core Parsing and Conversion Functions: - parse: Parses an HTML string. The return value can be directed to Hiccup or Hickory format. - Signature: `parse(html-string, options)` - Options: Specify output format (e.g., `:as-hiccup`, `:as-hickory`). - parse-fragment: Parses an HTML fragment. - Signature: `parse-fragment(html-fragment, options)` - as-hiccup: Converts parsed data to Hiccup format. - as-hickory: Converts parsed data to Hickory format. Dependencies: Core Hickory library. Usage Example: ```clojure (require '[hickory.core :as h]) (let [html "

Hello

" parsed-data (h/parse html {:as-hiccup true})] (println parsed-data)) ;; Output might be: [:div [:p "Hello"]] ``` ``` -------------------------------- ### Hickory Selectors and Zipper Functions Source: https://github.com/clj-commons/hickory/blob/master/README.md Hickory provides a rich set of selectors for navigating and querying HTML structures, along with zipper functions for tree manipulation. Key selectors include `has-child`, `has-descendant`, `element-child`, `find-in-text`, `precede-adjacent`, `follow-adjacent`, `precede`, and `follow`. A notable addition is the 'up-pred' zipper function. ```APIDOC hickory.select Namespace Functions: - has-child: Selects nodes that have a direct child matching a given selector. - has-descendant: Selects nodes that have a descendant matching a given selector. Note: This can be slow as it performs a full subtree search. - element-child: Selects element nodes that are direct children of another element node. - find-in-text: Selects nodes whose text content matches a pattern. - precede-adjacent: Selects nodes that immediately precede a target node. - follow-adjacent: Selects nodes that immediately follow a target node. - precede: Selects nodes that precede a target node in the document. - follow: Selects nodes that follow a target node in the document. Zipper Functions: - up-pred: A zipper function that moves up the tree based on a predicate. Dependencies: Requires the core Hickory library. ``` -------------------------------- ### hickory.core/parse-dom-with-write Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Parses an HTML document or fragment as a DOM using `document.implementation.createHTMLDocument` and `document.write`. This method can be useful for handling fragments or specific document creation scenarios. ```clojure (parse-dom-with-write s) ``` -------------------------------- ### hickory.core/parse Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Parses an entire HTML document into a DOM structure. The resulting structure can be used as input to `as-hiccup` or `as-hickory` for further processing. ```klipse (-> (parse "foo

Hello

") as-hiccup) ``` ```klipse (-> (parse "foo

Hello

") as-hicory) ``` -------------------------------- ### Hickory Basic Selectors Overview Source: https://github.com/clj-commons/hickory/blob/master/README.md Describes basic selector functions in Hickory that target elements based on their position relative to siblings. These include nth-child, nth-last-child, first-child, and last-child. ```APIDOC Basic Selectors: - `nth-child`: Selects an argument if it is the (nk+c)'th child of its parent. Supports `:odd` and `:even` keywords for n and c. - `nth-last-child`: Similar to `nth-child` but counts backwards from the last sibling. - `first-child`: Equivalent to `(nth-child 1)`. Takes no arguments. - `last-child`: Equivalent to `(nth-last-child 1)`. Takes no arguments. ``` -------------------------------- ### Hickory to HTML Generation Source: https://github.com/clj-commons/hickory/blob/master/README.md The `hickory-to-html` function is responsible for converting Hickory's internal data structure back into an HTML string. This function has undergone fixes to ensure proper HTML escaping of tag attributes and text nodes, including correct whitespace preservation for script and style tags. ```APIDOC hickory-to-html Function: - Converts a Hickory data structure into an HTML string. - Handles HTML escaping for tag attributes and text nodes. - Preserves whitespace correctly within script/style tags. Dependencies: Core Hickory library. Error Handling: - Provides more helpful error messages when conversion fails. Example: ```clojure (require '[hickory.core :as h]) (require '[hickory.render :as hr]) (let [hiccup-data [:div "Hello " [:b "World!"]]] (hr/hickory-to-html hiccup-data)) ;; Output: "
Hello World!
" ``` ``` -------------------------------- ### hickory.core/parse-dom-with-domparser Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Parses an HTML string into a DOM structure using the browser's built-in DOMParser API. This method is typically efficient for parsing full documents. ```clojure (parse-dom-with-domparser s) ``` -------------------------------- ### hickory.core/parse-fragment Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Parses an HTML fragment into a list of DOM elements. Each element in the list can be independently processed by `as-hiccup` or `as-hickory`. ```clojure (parse-fragment s) ``` -------------------------------- ### hickory.core/format-doctype Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Formats doctype information into a string representation. This is the inverse operation of `extract-doctype`. ```clojure (format-doctype dt) ``` -------------------------------- ### Hickory DOM Node Structure Source: https://github.com/clj-commons/hickory/blob/master/API.md Defines the structure of DOM node maps when represented using the Hickory format. This includes keys for node type, tag, attributes, and content. ```APIDOC HickoryRepresentable: Objects that can be represented as HTML DOM node maps, similar to clojure.xml, implement this protocol to make the conversion. Each DOM node will be a map or string (for Text/CDATASections). Nodes that are maps have the appropriate subset of the keys: :type - [:comment, :document, :document-type, :element] :tag - node's tag, check :type to see if applicable :attrs - node's attributes as a map, check :type to see if applicable :content - node's child nodes, in a vector, check :type to see if applicable ``` -------------------------------- ### hickory.core/extract-doctype Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Extracts the doctype information from an HTML string. This function is useful for parsing specific parts of an HTML document. ```clojure (extract-doctype s) ``` -------------------------------- ### hickory.core/node-type Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Determines the type of a DOM node. This utility function helps in classifying different elements within the parsed HTML structure. ```clojure (node-type type) ``` -------------------------------- ### hickory.core/remove-el Function Source: https://github.com/clj-commons/hickory/blob/master/API.md Removes a specific element from a DOM structure. This function operates on elements represented in the hickory format. ```clojure (remove-el el) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.