• Stars
    star
    311
  • Rank 134,035 (Top 3 %)
  • Language
    TypeScript
  • License
    BSD 2-Clause "Sim...
  • Created about 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Handler for htmlparser2, to get a DOM

domhandler Node.js CI

The DOM handler creates a tree containing all nodes of a page. The tree can be manipulated using the domutils or cheerio libraries and rendered using dom-serializer .

Usage

const handler = new DomHandler([ <func> callback(err, dom), ] [ <obj> options ]);
// const parser = new Parser(handler[, options]);

Available options are described below.

Example

const { Parser } = require("htmlparser2");
const { DomHandler } = require("domhandler");
const rawHtml =
    "Xyz <script language= javascript>var foo = '<<bar>>';</script><!--<!-- Waah! -- -->";
const handler = new DomHandler((error, dom) => {
    if (error) {
        // Handle error
    } else {
        // Parsing completed, do something
        console.log(dom);
    }
});
const parser = new Parser(handler);
parser.write(rawHtml);
parser.end();

Output:

[
    {
        data: "Xyz ",
        type: "text",
    },
    {
        type: "script",
        name: "script",
        attribs: {
            language: "javascript",
        },
        children: [
            {
                data: "var foo = '<bar>';<",
                type: "text",
            },
        ],
    },
    {
        data: "<!-- Waah! -- ",
        type: "comment",
    },
];

Option: withStartIndices

Add a startIndex property to nodes. When the parser is used in a non-streaming fashion, startIndex is an integer indicating the position of the start of the node in the document. The default value is false.

Option: withEndIndices

Add an endIndex property to nodes. When the parser is used in a non-streaming fashion, endIndex is an integer indicating the position of the end of the node in the document. The default value is false.


License: BSD-2-Clause

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

domhandler for enterprise

Available as part of the Tidelift Subscription

The maintainers of domhandler and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

More Repositories

1

htmlparser2

The fast & forgiving HTML and XML parser
TypeScript
4,057
star
2

css-select

a CSS selector compiler & engine
TypeScript
548
star
3

entities

Encode & decode HTML & XML entities with ease & speed
TypeScript
293
star
4

readabilitySAX

a fast and platform independent readability port (JS)
HTML
237
star
5

css-what

a CSS selector parser
TypeScript
217
star
6

domutils

Utilities for working with htmlparser2's DOM
TypeScript
180
star
7

bitfield

A bitfield implementation using buffers, compliant with the BitTorrent spec.
TypeScript
80
star
8

nth-check

Parses and compiles CSS nth-checks to highly optimized functions.
TypeScript
52
star
9

cornet

transform streaming html using css selectors
JavaScript
44
star
10

domelementtype

all the types of nodes in htmlparser2's dom
TypeScript
27
star
11

high5

html 5 tokenizer
JavaScript
24
star
12

boolbase

two functions: one that returns true, one that returns false
JavaScript
14
star
13

inline

inline all images, stylesheets and scripts of a webpage
JavaScript
11
star
14

binopsy

Reimplementation of binary-parser supporting serialization and streaming
JavaScript
10
star
15

SimpleQueue

A simple FIFO queue
TypeScript
6
star
16

node-minreq

minimalistic request library for node
JavaScript
6
star
17

webshelf

my node knockout 2012 project
JavaScript
2
star
18

minschema

a (html form) schema builder & validator
JavaScript
1
star
19

node-fsi-dropbox

DEPRECATED
JavaScript
1
star
20

encoding-sniffer

HTML encoding sniffer, with stream support
TypeScript
1
star
21

fb55

Config files for my GitHub profile.
1
star
22

fb55.github.io

HTML
1
star
23

YQL-Tables-for-Google-Data-API

Tables to authenticate at and use the Google Data API
JavaScript
1
star
24

ReadableFeeds

runs readabilitySAX on feeds
JavaScript
1
star
25

level-insert

insert documents into a db with autoincrementing keys
JavaScript
1
star
26

funexp

UNFINISHED a functional regular expression library
JavaScript
1
star