2 A forgiving HTML/XML/RSS parser written in JS for both the browser and NodeJS (yes, despite the name it works just fine in any modern browser). The parser can handle streams (chunked data) and supports custom handlers for writing custom DOMs/output.
10 ###Run tests under node:
13 ###Run tests in browser:
14 View runtests.html in any browser
19 var htmlparser = require("htmlparser");
20 var rawHtml = "Xyz <script language= javascript>var foo = '<<bar>>';< / script><!--<!-- Waah! -- -->";
21 var handler = new htmlparser.DefaultHandler(function (error, dom) {
23 [...do something for errors...]
25 [...parsing done, do something...]
27 var parser = new htmlparser.Parser(handler);
28 parser.parseComplete(rawHtml);
29 sys.puts(sys.inspect(handler.dom, false, null));
35 var handler = new Tautologistics.NodeHtmlParser.DefaultHandler(function (error, dom) {
37 [...do something for errors...]
39 [...parsing done, do something...]
41 var parser = new Tautologistics.NodeHtmlParser.Parser(handler);
42 parser.parseComplete(document.body.innerHTML);
43 alert(JSON.stringify(handler.dom, null, 2));
49 [ { raw: 'Xyz ', data: 'Xyz ', type: 'text' }
50 , { raw: 'script language= javascript'
51 , data: 'script language= javascript'
54 , attribs: { language: 'javascript' }
56 [ { raw: 'var foo = \'<bar>\';<'
57 , data: 'var foo = \'<bar>\';<'
62 , { raw: '<!-- Waah! -- '
63 , data: '<!-- Waah! -- '
74 parser.parseChunk(chunk);
79 ##Parsing RSS/Atom Feeds
82 new htmlparser.RssHandler(function (error, dom) {
87 ##DefaultHandler Options
92 var handler = new htmlparser.DefaultHandler(
93 function (error) { ... }
94 , { verbose: false, ignoreWhitespace: true }
98 ###Option: ignoreWhitespace
99 Indicates whether the DOM should exclude text nodes that consists solely of whitespace. The default value is "false".
119 [ { raw: 'br', data: 'br', type: 'tag', name: 'br' }
120 , { raw: 'this is the text\n'
121 , data: 'this is the text\n'
124 , { raw: 'font', data: 'font', type: 'tag', name: 'font' }
148 [ { raw: '\n\t', data: '\n\t', type: 'text' }
149 , { raw: 'br', data: 'br', type: 'tag', name: 'br' }
150 , { raw: 'this is the text\n'
151 , data: 'this is the text\n'
154 , { raw: 'font', data: 'font', type: 'tag', name: 'font' }
161 Indicates whether to include extra information on each node in the DOM. This information consists of the "raw" attribute (original, unparsed text found between "<" and ">") and the "data" attribute on "tag", "script", and "comment" nodes. The default value is "true".
167 <a href="test.html">xxx</a>
173 [ { raw: 'a href="test.html"'
174 , data: 'a href="test.html"'
177 , attribs: { href: 'test.html' }
178 , children: [ { raw: 'xxx', data: 'xxx', type: 'text' } ]
187 <a href="test.html">xxx</a>
195 , attribs: { href: 'test.html' }
196 , children: [ { data: 'xxx', type: 'text' } ]
201 ###Option: enforceEmptyTags
202 Indicates whether the DOM should prevent children on tags marked as empty in the HTML spec. Typically this should be set to "true" HTML parsing and "false" for XML parsing. The default value is "true".
214 [ { raw: 'link', data: 'link', type: 'tag', name: 'link' }
215 , { raw: 'text', data: 'text', type: 'text' }
233 , children: [ { raw: 'text', data: 'text', type: 'text' } ]
240 ###TBD (see utils_example.js for now)
244 Looking for CSS selectors to search the DOM? Try Node-SoupSelect, a port of SoupSelect to NodeJS: http://github.com/harryf/node-soupselect
246 There's also a port of hpricot to NodeJS that uses HtmlParser for HTML parsing: http://github.com/silentrob/Apricot