Skip to content

endIndex is not consistent in Tokenizer's onattribend #1538

@DimaIT

Description

@DimaIT

When using Tokenizer's onattribend callback it's endIndex may indicate different position, depending on the structure of the attribute.
Worst case scenario is NoValue attribute, then endIndex is not related to the attribute at all.

Code to reproduce:
const { Tokenizer } = require("htmlparser2");

function parse(html) {
    console.log('\n' + html);
    const tokenizer = new Tokenizer({}, {
        onopentagname() {},
        onopentagend() {},
        onattribname() {},
        onattribdata() {},
        onattribend(quote, endIndex) {
            console.log(' '.repeat(endIndex) + '^');
            console.log('Attribute endIndex:', endIndex);
        },
        onend() {},
        onattribentity() {},
        oncdata() {},
        onclosetag() {},
        oncomment() {},
        ondeclaration() {},
        onprocessinginstruction() {},
        onselfclosingtag() {},
        ontext() {},
        ontextentity() {},
    });
    tokenizer.write(html);
    tokenizer.end();
}

parse('<a aaaaa   >');
parse('<a aaaaa >');
parse('<a a=aaa >');
parse('<a a="a" >');

Output:

<a aaaaa   >
           ^
Attribute endIndex: 11

<a aaaaa >
         ^
Attribute endIndex: 9

<a a=aaa >
        ^
Attribute endIndex: 8

<a a="a" >
       ^
Attribute endIndex: 7

Ideally I would expect one value for all cases above (probably 7).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions