fb55/htmlparser2

`endIndex` is not consistent in Tokenizer's `onattribend`

DimaIT opened this issue · 1 comments

DimaIT commented

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).

fb55 commented

Ohh seems like a shortcoming of the last release. I agree that all indices should be the same in your examples.

It will take me a bit to look into this, so help is appreciated!