Skip to content

Feature request: source locations for elements and attributes #5

@JoshuaWise

Description

@JoshuaWise

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions