Skip to content

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

@jpilgrim

Description

@jpilgrim

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions