cloudflare / cloudflare/html-rewriter-wasm

Feature request: source locations for elements and attributes

Open
#5 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
219
Forks
20
PR merge metrics
No merged PRs in 30d

Description

Hi, first of all, thank you for this fantastic library.

I am using it to transform HTML attributes. During this transformation, it's possible for errors to occur. I'd like to display the source location of the attribute that caused the error. However I don't see any way to get the source location of an element or attribute.

I am using the element handlers like this:

```js
const rewriter = new HTMLRewriter(callback);
rewriter.on('*[src]', {
async element(element) {
try {
element.setAttribute('src', await modify(element.getAttribute('src')));
} catch (err) {
// No way of getting the source location of the element or the "src" attribute
const lineNumber = ???;
const columnNumber = ???;
console.error(`A problem occurred on line ${lineNumber}, column ${columnNumber}`);
}
},
});
```

Could this feature be added?

Edit: A simple offset would also work, since I could use it to calculate the line and column numbers myself.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.