I had to write a DOM TreeWalker today for a personal project that was written in Oak, and it's surprising how pleasant it was to write. I'd say it was the same or better than using the same DOM API from JavaScript.
// headingsList takes some container <div> and returns a list of all headings
// within that section of the page, in order
fn headingsList(div) {
walker := document.createTreeWalker(
div
NodeFilter.SHOW_ELEMENT
{
acceptNode: fn(el) if el.tagName {
'H1', 'H2', 'H3', 'H4', 'H5', 'H6' -> NodeFilter.FILTER_ACCEPT
_ -> NodeFilter.FILTER_SKIP
}
}
)
headings := []
with loop() fn(_, break) if el := walker.nextNode() {
? -> break(headings)
_ -> headings << {
level: int(el.tagName.1)
text: el.textContent
}
}
}
This is mostly due to two things, I think:
- The recently finalized
std.loop
API is really nice for generic unbounded loops. - This semi-recent change to
oak build --web
codegen made JS methods very nice to call from Oak.