Skip to content
Search and replace in HTML5 strings.
Clojure
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/html5_walker
test/html5_walker
.gitignore
README.md
project.clj
tests.edn

README.md

html5-walker

A thin Clojure wrapper around jfiveparse, this lets you find and replace in HTML5 strings.

Install

  • add [html5-walker "2019-10-01"] to :dependencies in your project.clj

or

  • add html5-walker {:mvn/version "2019-10-01"} to :deps in your deps.edn

Usage

html5-walker exposes these functions:

find-nodes

Signature: (find-nodes html-string path)

It returns a sequence of Nodes matching the path.

A path is a vector of keywords of element names, with optional hiccup-esque class names. Like this:

  • [:a] matches all anchor tags.
  • [:form :input] matches all input tags nested inside a form.
  • [:div.foo] matches all div tags with "foo" in its class name.

So running:

(find-nodes "<ul><li>1</li><li>2</li></ul>" [:ul :li])

would return a sequence with two li nodes in it. See the javadoc for more information about these nodes.

replace-in-fragment

Signature: (replace-in-fragment html-string path->fn)

This returns a new html-string with any changes performed by the functions in the path->fn map applied.

So running:

(replace-in-fragment
  "<ul><li>1</li><li>2</li></ul>"
  {[:ul :li] (fn [node] (.setInnerHTML node (str (.getInnerHTML node) "!!!")))})

would return:

"<ul><li>1!!!</li><li>2!!!</li></ul>"

replace-in-document

Just like replace-in-fragment, except it works on an entire html document. This means that html, head and body tags are expected to be there. They will be added if missing.

Note that replace-in-fragment will actually remove these tags when found.

More usage

Take a look at the tests if you'd like more examples.

License

Copyright © Magnar Sveen, since 2019

Distributed under the Eclipse Public License, the same as Clojure.

You can’t perform that action at this time.