CFML wrapper for JSoup.
Working with JSoup in CFML often requires JavaCasting variables and quite cumbersome syntax. This component provides some simpler CFML methods.
JSOUP has become incredibly popular and many other Java Libraries now bundle it, including the ever popular Flexmark. To ensure we load the library we want, we have to use the OSGI system in Lucee. This is very simple, but we need to know the path to the JSOUP Jar. In many ways this is better than loading through the class path.
-
Instantiate the component as a singleton in a shared scope, e.g. application
application.coldsoup = new coldSoup(pathtoJAR);
-
Call static methods as required, e.g.
myDoc = application.coldsoup.parse(html);
-
Return type from
parse()is a Jsoup Document.These can be searched with
select(selector)where selector is a JQuery-like selector, e.g.p.class. The return value is sufficiently like a CF array to be used as one. Each element is a Jsoup Node. Modern versions of Jsoup should return an empty array if none are found. If you get null values, upgrade.
doc = coldSoup.parse(htmlDocument);
headings = doc.select("h2");
for (heading in headings) {...}html = coldSoup.clean(html="<script>bad code</script> text", whitelist="basic" );node = coldSoup.createNode("h2","my heading");For further examples, see samples.