isaacs / sax-js

A sax style parser for JS
Other
1.09k stars 325 forks source link

parser's tag does not reflect actually visited element in ontext #261

Open jpilgrim opened 10 months ago

jpilgrim commented 10 months ago

According to the documentation, the parser object property "tag" reflects "The current tag being dealt with." This does not work in all cases. See the following test:

import sax from "sax";

test("parser.tag.name should be to current element in ontext", () => {
    const parser = sax.parser(true);
    let currentElement: string[] = []

    parser.onopentag =  (node) => { currentElement.push(node.name); };
    parser.onclosetag =  (node) => { currentElement.pop(); };
    parser.ontext = (text) => {
        expect(parser.tag.name + " - " + text).toBe(currentElement[currentElement.length-1]+ " - " + text);
    }
    parser.write('<some><sub>Text<b>bold</b></sub></some>').close();
});

This (Jest) test fails:

Error: expect(received).toBe(expected) // Object.is equality

Expected: "sub - Text"
Received: "b - Text"

IMHO this is a bug. It seems as if the parser already detects the opening tag after the text, which also means the end of the text. But this following tag must not be the tag when ontext is called. It is weird that the parser's tag contains a new tag before onopentag (or onopentagstart) has been called. It actually means that ontext is called after the parser has found a new tag -- in other words it is called too late (from a client perspective)!