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

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

Open
jpilgrim opened this issue Nov 10, 2023 · 0 comments
Open

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

jpilgrim opened this issue Nov 10, 2023 · 0 comments

Comments

@jpilgrim
Copy link

jpilgrim commented Nov 10, 2023

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

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