Skip to content

taylorwood/hickory-css-selector

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

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

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!

Releases

No releases published

Packages

No packages published