Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add custom attribute to <desc> element #86

Open
sixers opened this issue Apr 9, 2021 · 0 comments
Open

Can't add custom attribute to <desc> element #86

sixers opened this issue Apr 9, 2021 · 0 comments

Comments

@sixers
Copy link

sixers commented Apr 9, 2021

AFAIU, this parser is what HTMLRewriter in CloudFlare workers uses under the hood.
I have a simple worker which transforms the HTML received as request body and adds a new attribute to each node. It seems that there is a bug, and the attribute change is not applied to <desc> element.

Example:

class EnumerationElementHandler {
    constructor() {
        this.counter = 0;
    }
    element(element) {
        const c = this.counter++;
        element.setAttribute('data-custom', c.toString())
    }
}

const rewriter = new HTMLRewriter().on("*", new EnumerationElementHandler())

export default {
    async fetch(request) {
        return rewriter.transform(new Response(request.body, {status: 200}));
    },
};

Now, I send a POST request with the following body:

<div><svg viewBox="0 0 460 271.2" width="70px"
    height="41px" aria-labelledby="title desc">
    <title>
    <span>X</span></title>
    <desc><span>Y</span>
    </desc>
    <path id="flare" fill="#fff"
    d="M370.9,150.2l-40.7-24.7c-0.6-0.1-4.4,0.3-6.4-0.7c-1.4-0.7-2.5-1.9-3.2-4c-3.2,0-175.5,0-175.5,0    v91.8h225.8V150.2z"></path>
    
</svg></div>

Output:

<div data-custom="0"><svg viewBox="0 0 460 271.2" width="70px" height="41px" aria-labelledby="title desc" data-custom="1">
    <title data-custom="2">
    <span data-custom="3">X</span></title>
    <desc><span data-custom="4">Y</span>
    </desc>
    <path id="flare" fill="#fff" d="M370.9,150.2l-40.7-24.7c-0.6-0.1-4.4,0.3-6.4-0.7c-1.4-0.7-2.5-1.9-3.2-4c-3.2,0-175.5,0-175.5,0    v91.8h225.8V150.2z" data-custom="5"></path>
    
</svg></div>

As you can see, data-custom attribute is added to every node but <desc>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant