Skip to content

Instantly share code, notes, and snippets.

@DTrejo
Created November 6, 2010 07:59
Show Gist options
  • Save DTrejo/665269 to your computer and use it in GitHub Desktop.
Save DTrejo/665269 to your computer and use it in GitHub Desktop.
making sure that node-htmlparser doesn't throw errors when reading html with a doctype
var rawHtml = '<!DOCTYPE HTML><html lang="en"><head><title>LEtss goo</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="en-us"><link rel="icon" type="image/png" href="/favicon.png"><link rel="stylesheet" type="text/css" href="css/style.css"><link rel="stylesheet" type="text/css" href="css/jquery.gritter.css"><link rel="stylesheet" type="text/css" href="css/jScrollPane.css"><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="js/lib/jquery.mousewheel.js"></script><script type="text/javascript" src="js/lib/jquery.em.js"></script><script type="text/javascript" src="js/lib/jScrollPane.js"></script><script type="text/javascript" src="js/lib/jquery.autolink.js"></script><script type="text/javascript" src="js/lib/jquery-css-transform.js"></script><script type="text/javascript" src="js/lib/jquery-animate-css-rotate-scale.js"></script><script type="text/javascript" src="js/lib/jquery.gritter.min.js"></script><script type="text/javascript" src="socket.io/socket.io.js"></script><script type="text/javascript" src="js/session.js"></script><script type="text/javascript">var id_session = "1337"; // chatroom namevar param = "1338"; // room to redirect to, if they tried to hit a url and failed.var id_php = "1339"; // username$(document).ready(function() {$("#nojs").hide();$("#dolla").text("\t"+$);});</script></head><body><p id="nojs">Javascript is broken!</p><pre> Yo Content is showin up! Youza killa.</pre><p>Also, jquery works:</p><pre id="dolla"></pre><!-- Your designers can design all static like, without a server even! --><p>Hello, <span class="name">Joe Smith</span> wrote this.</p><p>But yo developers will still see all the goodness without any extra work. Now to add pure into the mix?</p><p>Static version: <a href="index.html">localhost:PORT/index.html</a></p><p>"server-side jquery modified" version: <a href="/">localhost:PORT/</a></p></body></html>';
var htmlparser = require("htmlparser");
var sys = require('sys');
var handler = new htmlparser.DefaultHandler(function (error, dom) {
if (error) console.log(error);
else console.log('done!');
});
var parser = new htmlparser.Parser(handler);
parser.parseComplete(rawHtml);
sys.puts(sys.inspect(handler.dom, false, null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment