Parser options

Ger Hobbelt edited this page Aug 24, 2018 · 20 revisions


var Parser = new htmlparser.Parser(<obj> handler,[<obj> options]);


while (...) {

Note: It is recommended to always pass the decodeEntities option (as described below) to the constructor; see #105 for a potential issue.

Note: When streaming non ASCII data, make sure to use a StringDecoder in order to prevent certain non ASCII characters from being chopped apart.


Names for the keys of the handler object. Only functions are valid values (the parser will break otherwise).

  • onopentag(<str> name, <obj> attributes)
  • onopentagname(<str> name)
  • onattribute(<str> name, <str> value)
  • ontext(<str> text)
  • onclosetag(<str> name)
  • onprocessinginstruction(<str> name, <str> data)
  • oncomment(<str> data)
  • oncommentend()
  • oncdatastart()
  • oncdataend()
  • onerror(<err> error)
  • onreset()
  • onend()


write (alias: parseChunk)

Parses a chunk of data and calls the corresponding callbacks.

end (alias: done)

Parses the end of the buffer and clears the stack, calls onend.


Resets buffer & stack, calls onreset.


Resets the parser, parses the data & calls end.

Option: xmlMode

Indicates whether special tags (<script> and <style>) should get special treatment and if "empty" tags (eg. <br>) can have children. If false, the content of special tags will be text only.

For feeds and other XML content (documents that don't consist of HTML), set this to true. Default: false.

Option: decodeEntities

If set to true, entities within the document will be decoded. Defaults to false.

Option: lowerCaseTags

If set to true, all tags will be lowercased. If xmlMode is disabled, this defaults to true.

Option: lowerCaseAttributeNames

If set to true, all attribute names will be lowercased. This has noticeable impact on speed, so it defaults to false.

Option: recognizeCDATA

If set to true, CDATA sections will be recognized as text even if the xmlMode option is not enabled. NOTE: If xmlMode is set to true then CDATA sections will always be recognized as text.

Option: recognizeSelfClosing

If set to true, self-closing tags will trigger the onclosetag event even if xmlMode is not set to true. NOTE: If xmlMode is set to true then self-closing tags will always be recognized.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.