A Clojure library for parsing, sanitising and escaping HTML, built on jsoup.
A Clojure library for parsing, sanitising and escaping HTML, built on jsoup.
(abs-url el k)Returns the value of attribute k of el as an absolute URL, resolved
against the base URI of the document. Returns nil when the attribute is
absent, or when it is relative and the document has no base URI, so it
works with if-let, some-> and keep. jsoup returns "" in both
cases. k can be a string or a keyword.
The URL is not checked: a javascript: link is returned as it is. Use
clean or valid? on untrusted markup.
Returns the value of attribute `k` of `el` as an absolute URL, resolved against the base URI of the document. Returns nil when the attribute is absent, or when it is relative and the document has no base URI, so it works with `if-let`, `some->` and `keep`. jsoup returns "" in both cases. `k` can be a string or a keyword. The URL is not checked: a `javascript:` link is returned as it is. Use `clean` or `valid?` on untrusted markup.
(attr el k)Returns the value of attribute k of el, or nil when it is absent.
k can be a string or a keyword.
Returns the value of attribute `k` of `el`, or nil when it is absent. `k` can be a string or a keyword.
(attrs el)Returns the attributes of el as a map of keyword to string.
Returns the attributes of `el` as a map of keyword to string.
(base-uri el)Returns the base URI that el (a Document or any element) uses to
resolve relative links in abs-url, or nil when it has none. jsoup
returns "" then.
It is the :base-uri given to parse or parse-fragment, or for a File
or a Path without that option, the absolute path of the file. A <base href> tag in the document replaces it, resolved against it when it is
relative.
Returns the base URI that `el` (a Document or any element) uses to resolve relative links in `abs-url`, or nil when it has none. jsoup returns "" then. It is the `:base-uri` given to `parse` or `parse-fragment`, or for a File or a Path without that option, the absolute path of the file. A `<base href>` tag in the document replaces it, resolved against it when it is relative.
(body-html doc)Returns the inner HTML of the document body as a string.
Returns the inner HTML of the document body as a string.
(clean html)(clean html opts)Removes everything from the html string that is not allowed by a
safelist, so that untrusted markup can be embedded safely. Returns a
string.
Options:
:safelist one of :none, :simple-text, :basic (default),
:basic-with-images, :relaxed.:add-tags tags to allow in addition to the safelist, e.g. [:section].:remove-tags tags to disallow.:add-attributes map of tag to attributes to allow,
e.g. {:a [:target] :all [:class]}.:remove-attributes same shape, to disallow attributes.:base-uri base used to resolve relative links, default "". Without
it, relative links cannot be resolved and are removed.:preserve-relative-links keep relative URLs as they are instead of
making them absolute. Still requires :base-uri.:pretty-print boolean, default false.:escape-mode one of :base, :xhtml, :extended.:remove-empty boolean, default false. Also removes the elements left
with no content after cleaning: no text other than
whitespace or , and no <img>, <br> or other void
element. An inline element holding only whitespace, such
as <b> </b> between two words, is unwrapped instead, so
the space stays, unless the text next to it already has
one. A <br> at the end of a block that has other
content is removed too, since it shows nothing; a block
holding only <br>, such as <p><br></p>, keeps it. Table
cells (<td>, <th>) are kept.:collapse-whitespace boolean, default false. Collapses whitespace the
way a browser renders it: each run of spaces, tabs and
newlines becomes one space, a space that follows another
is dropped, and spaces at the edges of block elements,
around <br> and between blocks are removed. and
the text of <pre> are kept as they are.:preset options for a common source of HTML, merged under the
other options. Tags and attributes you pass are added to
the preset's, and other options replace its values.
Presets:
- :word HTML exported from Microsoft Word: same as
{:remove-tags [:span] :remove-empty true :collapse-whitespace true}.An unknown :safelist, :escape-mode or :preset throws an ex-info
listing the valid values.
Removes everything from the `html` string that is not allowed by a
safelist, so that untrusted markup can be embedded safely. Returns a
string.
Options:
- `:safelist` one of :none, :simple-text, :basic (default),
:basic-with-images, :relaxed.
- `:add-tags` tags to allow in addition to the safelist, e.g. [:section].
- `:remove-tags` tags to disallow.
- `:add-attributes` map of tag to attributes to allow,
e.g. {:a [:target] :all [:class]}.
- `:remove-attributes` same shape, to disallow attributes.
- `:base-uri` base used to resolve relative links, default "". Without
it, relative links cannot be resolved and are removed.
- `:preserve-relative-links` keep relative URLs as they are instead of
making them absolute. Still requires `:base-uri`.
- `:pretty-print` boolean, default false.
- `:escape-mode` one of :base, :xhtml, :extended.
- `:remove-empty` boolean, default false. Also removes the elements left
with no content after cleaning: no text other than
whitespace or ` `, and no <img>, <br> or other void
element. An inline element holding only whitespace, such
as `<b> </b>` between two words, is unwrapped instead, so
the space stays, unless the text next to it already has
one. A <br> at the end of a block that has other
content is removed too, since it shows nothing; a block
holding only <br>, such as <p><br></p>, keeps it. Table
cells (<td>, <th>) are kept.
- `:collapse-whitespace` boolean, default false. Collapses whitespace the
way a browser renders it: each run of spaces, tabs and
newlines becomes one space, a space that follows another
is dropped, and spaces at the edges of block elements,
around <br> and between blocks are removed. ` ` and
the text of <pre> are kept as they are.
- `:preset` options for a common source of HTML, merged under the
other options. Tags and attributes you pass are added to
the preset's, and other options replace its values.
Presets:
- `:word` HTML exported from Microsoft Word: same as
`{:remove-tags [:span] :remove-empty true
:collapse-whitespace true}`.
An unknown `:safelist`, `:escape-mode` or `:preset` throws an `ex-info`
listing the valid values.(escape-html s)(escape-html s opts)Escapes s so it is safe to embed as HTML text.
Takes the same :escape-mode and :pretty-print options as parse
(default :base).
Escapes `s` so it is safe to embed as HTML text. Takes the same `:escape-mode` and `:pretty-print` options as `parse` (default :base).
(jsoup-version)Returns the version of the jsoup library found on the classpath, as a
string. It can differ from the version montalivet was built with when
another dependency brings its own jsoup. The output of clean and
escape-html can change between jsoup versions.
Returns the version of the jsoup library found on the classpath, as a string. It can differ from the version montalivet was built with when another dependency brings its own jsoup. The output of `clean` and `escape-html` can change between jsoup versions.
(outer-html el)Returns the markup of el itself, including its own tag.
Returns the markup of `el` itself, including its own tag.
(parse input)(parse input opts)Parses input as a full HTML document and returns a jsoup Document.
input can be a String of HTML, a java.io.File, a java.nio.file.Path or
a java.io.InputStream. Any other type throws an exception. A stream is
read to its end but not closed.
Options:
:pretty-print boolean, default true.:escape-mode one of :base, :xhtml, :extended.:base-uri base used to resolve relative links. Defaults to ""
for a String or an InputStream, and to the location of
the file for a File or a Path.:charset charset of a File, Path or InputStream, as a string
("ISO-8859-1") or a java.nio.charset.Charset. When
absent, it is detected from a byte-order mark or a
<meta> tag, with UTF-8 as the fallback. Ignored for
a String.Parses `input` as a full HTML document and returns a jsoup Document.
`input` can be a String of HTML, a java.io.File, a java.nio.file.Path or
a java.io.InputStream. Any other type throws an exception. A stream is
read to its end but not closed.
Options:
- `:pretty-print` boolean, default true.
- `:escape-mode` one of :base, :xhtml, :extended.
- `:base-uri` base used to resolve relative links. Defaults to ""
for a String or an InputStream, and to the location of
the file for a File or a Path.
- `:charset` charset of a File, Path or InputStream, as a string
("ISO-8859-1") or a java.nio.charset.Charset. When
absent, it is detected from a byte-order mark or a
`<meta>` tag, with UTF-8 as the fallback. Ignored for
a String.(parse-fragment html)(parse-fragment html opts)Parses the html string as a fragment of a document body, and returns
a jsoup Document whose body holds the fragment.
Unlike parse, every element stays in the body: parse moves elements
such as <title> or <meta> to the head.
Options: :pretty-print, :escape-mode and :base-uri, as in parse.
Parses the `html` string as a fragment of a document body, and returns a jsoup Document whose body holds the fragment. Unlike `parse`, every element stays in the body: `parse` moves elements such as `<title>` or `<meta>` to the head. Options: `:pretty-print`, `:escape-mode` and `:base-uri`, as in `parse`.
(select doc query)Returns a vector of the elements of doc that match the CSS selector
query, in document order. doc can be a Document or any element, in
which case only its descendants are searched. Returns an empty vector
when nothing matches.
See https://jsoup.org/cookbook/extracting-data/selector-syntax for the selector syntax. An empty or invalid selector throws an exception.
Returns a vector of the elements of `doc` that match the CSS selector `query`, in document order. `doc` can be a Document or any element, in which case only its descendants are searched. Returns an empty vector when nothing matches. See https://jsoup.org/cookbook/extracting-data/selector-syntax for the selector syntax. An empty or invalid selector throws an exception.
(select-one doc query)Returns the first element of doc that matches the CSS selector query,
or nil when nothing matches.
Returns the first element of `doc` that matches the CSS selector `query`, or nil when nothing matches.
(text el)Returns the combined, whitespace-normalised text of el and its children.
Returns the combined, whitespace-normalised text of `el` and its children.
(unescape-html s)(unescape-html s opts)Decodes the HTML entities in s: named (&, ©), decimal
(B) and hexadecimal (A). Unknown entities are left as they
are. It reverses escape-html.
Options:
:in-attribute boolean, default false. Decodes s as an attribute
value: a named entity without its ; is then kept as it is when it is
followed by = or by a letter or digit, as in the URL a=1©=2.Decodes the HTML entities in `s`: named (`&`, `©`), decimal (`B`) and hexadecimal (`A`). Unknown entities are left as they are. It reverses `escape-html`. Options: - `:in-attribute` boolean, default false. Decodes `s` as an attribute value: a named entity without its `;` is then kept as it is when it is followed by `=` or by a letter or digit, as in the URL `a=1©=2`.
(valid? html)(valid? html opts)Returns true when the html string contains nothing that clean would
remove with the same safelist, so it can be used as it is. Use it to
reject untrusted markup instead of cleaning it.
Like clean, it checks a fragment of a body: a full document with
<html> or <head> is never valid.
Options: :safelist, :add-tags, :remove-tags, :add-attributes,
:remove-attributes and :preserve-relative-links, as in clean. There
is no base URI, so relative links are only valid with
:preserve-relative-links.
Returns true when the `html` string contains nothing that `clean` would remove with the same safelist, so it can be used as it is. Use it to reject untrusted markup instead of cleaning it. Like `clean`, it checks a fragment of a body: a full document with `<html>` or `<head>` is never valid. Options: `:safelist`, `:add-tags`, `:remove-tags`, `:add-attributes`, `:remove-attributes` and `:preserve-relative-links`, as in `clean`. There is no base URI, so relative links are only valid with `:preserve-relative-links`.
(version)Returns the version of montalivet as a string, e.g. "0.1.5".
Returns the version of montalivet as a string, e.g. "0.1.5".
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |