isaacs / sax-js

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

Notify if onclosetag is sent for a non-existing closing tag #262

Open NPavie opened 9 months ago

NPavie commented 9 months ago

While doing a custom formatter with the lib (which I found really great by the way), i noticed something that caused me some trouble :

While formatting an XML file, I want the file content to remain as close to source as possible, including possible syntax errors that the user would be in charge to correct. When testing a wrongly formatted file with the parser in non-strict mode, I realised that the parser was raising onclosetag event when closing tags were not existing, without notifying me in some way that the closing tag in question was "virtual" as in "does not exists in document". As a result, i was wrongfully creating closing tag at the wrong place while formatting.

The simplest option to avoid this I think would be for the onclosetag event to be emitted with a "virtual" boolean value indicating that the onclosetag was sent while no closing tag was present in the document.

(With some slight modification, i think it can be done without breaking existing code base. I have prepared something for it in a modified version of the sax-js for the formatted i was working on, i'll be making a PR for fix proposal when I have finished creating some tests for it)