Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Hickory CSS Selectors

Hickory provides a namespace of selectors with functionality similar to CSS selectors, for selecting particular elements from a document tree.

This proof-of-concept provides a CSS-to-Hickory selector translator that can turn a CSS selector like this:

div#main > ul.styled a[href]

into a Hickory selector like this:

(s/descendant
  (s/child
    (s/and (s/tag :div) (s/id :main))
    (s/and (s/tag :ul) (s/class :styled)))
  (s/and (s/tag :a) (s/attr :href)))

CSS selectors can be used as a concise syntax for extracting structured data from HTML documents.

Usage

Add a dependency:

{:deps {hickory-css-selector
        {:git/url "https://github.com/taylorwood/hickory-css-selector"}}}

Convert some HTML into Hickory, then select elements from it using CSS selector syntax:

(require '(hickory [core :as h] [select :as s]))
(def doc (h/as-hickory (h/parse (slurp "https://clojure.org"))))

(use 'hickory-css-selectors)
(s/select (parse-css-selector "a[href~=reference]") doc)
=>
[{:type :element,
  :attrs
  {:href "/reference/documentation", :class "w-nav-link clj-nav-link"},
  :tag :a,
  :content ["Reference‍"]}]

Tests

$ clojure -A:test

Contributions

I welcome them!

Many thanks to @vitobasso for adding sibling (~, +), :has, and :contains support!

About

CSS-to-Hickory selector translator

Topics

Resources

License

Releases

No releases published

Packages

No packages published
You can’t perform that action at this time.