Merged
Conversation
Document all parser events, options, and common workflows (searching, modifying, serializing the DOM) Closes #1765
There was a problem hiding this comment.
Pull request overview
Expands the README documentation to make htmlparser2 easier to use without reading source code, focusing on parser callbacks/options and common DOM workflows (parse → query → modify → serialize), addressing #1765.
Changes:
- Added a comprehensive table of parser events (callbacks) supported by
Parser. - Added a parser options table and expanded
parseDocumentdocumentation, including combined parser + domhandler options. - Added practical DOM workflow examples (searching with
DomUtils/css-select, modifying nodes, and serializing back to HTML) and expandedparseFeeddocumentation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+109
to
+116
| | Option | Type | Default | Description | | ||
| | ------------------------ | --------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | `xmlMode` | `boolean` | `false` | Treat the document as XML. This affects entity decoding, self-closing tags, CDATA handling, and more. Set this to `true` for XML, RSS, Atom and RDF feeds. | | ||
| | `decodeEntities` | `boolean` | `true` | Decode HTML entities (e.g. `&` -> `&`). | | ||
| | `lowerCaseTags` | `boolean` | `!xmlMode` | Lowercase tag names. | | ||
| | `lowerCaseAttributeNames`| `boolean` | `!xmlMode` | Lowercase attribute names. | | ||
| | `recognizeSelfClosing` | `boolean` | `xmlMode` | Recognize self-closing tags (e.g. `<br/>`). Always enabled in `xmlMode`. | | ||
| | `recognizeCDATA` | `boolean` | `xmlMode` | Recognize CDATA sections as text. Always enabled in `xmlMode`. | |
Comment on lines
+151
to
+161
| `parseDocument` accepts an optional second argument with both parser and [DOM handler options](https://github.com/fb55/domhandler): | ||
|
|
||
| ```js | ||
| const dom = htmlparser2.parseDocument(data, { | ||
| // Parser options | ||
| xmlMode: true, | ||
|
|
||
| // domhandler options | ||
| withStartIndices: true, // Add `startIndex` to each node | ||
| withEndIndices: true, // Add `endIndex` to each node | ||
| }); |
| const feed = htmlparser2.parseFeed(content); | ||
| ``` | ||
|
|
||
| This returns an object with `type`, `title`, `link`, `description`, `updated`, `author`, and `items` (an array of feed entries), or `null` if the document isn't a recognized feed format. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Document all parser events, options, and common workflows (searching, modifying, serializing the DOM)
Closes #1765
The wiki is now disabled