| name | bx-jsoup |
| description | Use this skill when parsing, querying, or sanitizing HTML in BoxLang with the bx-jsoup module: htmlParse(), htmlClean(), CSS selectors, XSS protection, HTML to JSON/XML conversion, and safe HTML allowlisting. |
bx-jsoup: HTML Parsing & Sanitization
Installation
install-bx-module bx-jsoup
box install bx-jsoup
BIFs
| BIF | Description |
|---|
htmlParse( html ) | Parse HTML into a BoxDocument object for querying |
htmlClean( html, [safelist], [baseUri] ) | Sanitize HTML, removing unsafe tags/attributes |
HTML Parsing with htmlParse()
doc = htmlParse( "<html><head><title>My Page</title></head><body><h1>Hello</h1></body></html>" )
title = doc.title()
bodyText = doc.body().text()
innerHtml = doc.html()
fullHtml = doc.outerHtml()
doc = htmlParse( "<ul><li class='item active'>One</li><li class='item'>Two</li></ul>" )
items = doc.select( ".item" )
active = doc.select( ".item.active" )
heading = doc.select( "h1, h2" )
element = doc.getElementById( "main-content" )
paragraphs = doc.getElementsByTag( "p" )
links = doc.getElementsByAttribute( "href" )
plainText = doc.text()
BoxDocument Enhanced Methods
doc = htmlParse( htmlContent )
jsonStr = doc.toJSON()
jsonPretty = doc.toJSON( true )
xmlStr = doc.toXML()
xmlPretty = doc.toXML( true, 2 )
HTML Sanitization with htmlClean()
htmlClean() removes tags and attributes not on the allowlist — protecting against XSS:
cleanHtml = htmlClean( userInput, "basic" )
plainText = htmlClean( "<script>alert(1)</script><p>Hello</p>", "none" )
safe = htmlClean( "<script>xss()</script><b>Bold</b><p>Text</p>", "basic" )
safe = htmlClean( '<a href="/page">Link</a>', "basic", "https://example.com" )
Common Patterns
function sanitizeRichText( html ) {
return htmlClean( html, "basicWithImages" )
}
function extractLinks( html ) {
var doc = htmlParse( html )
var links = doc.select( "a[href]" )
return links.map( el => el.attr( "href" ) )
}
function scrapePrices( html ) {
var doc = htmlParse( html )
var prices = doc.select( ".price" )
return prices.map( el => el.text() )
}
Common Pitfalls
- ❌ Never output user-supplied HTML without running it through
htmlClean() first
- ✅ Use
"basic" or "basicWithImages" for rich text editor output — "relaxed" is rarely safe for user input
- ✅
htmlParse() is for trusted HTML you want to query; htmlClean() is for untrusted HTML you want to render
- ❌
doc.select() uses CSS selectors, not XPath — "#id", ".class", "tag", "[attr]" syntax