Skip to content

Undocumented breaking change in v8.0+ #2289

@sadiqkhoja

Description

@sadiqkhoja

I was using v3.9 for a long time but recently decided to upgrade to the latest version (10.0). My code started to break down because I was passing Buffer to the parser.write. Following is the small reproducible code:

const { Parser } = require('htmlparser2');

const parser = new Parser({
  onopentag(name, attributes) {
    console.log(`Opening tag: ${name}`);
  },
  ontext(text) {
    console.log(`Text: ${text}`);
  },
  onclosetag(name) {
    console.log(`Closing tag: ${name}`);
  },
  onerror(err) {
    console.error('Parser error:', err.message);
  }
});

const htmlBuffer = Buffer.from('<div>Hello World</div>');

try {
  console.log('Attempting to write Buffer to parser...');
  parser.write(htmlBuffer);
  parser.end();
} catch (error) {
  console.error('Caught error:', error.message);
}

Above code returns this.buffer.charCodeAt is not a function for all versions about 8.0. For any version below 8.0, it returns ```
Opening tag: div
Text: Hello World
Closing tag: div


I don't if the change in behaviour is intentional or not. However I feel either release notes should be updated or type of the argument of the `write()` should be documented in the readme file.

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