Skip to content

Instantly share code, notes, and snippets.

@Maff-
Created February 16, 2011 19:33
Show Gist options
  • Save Maff-/829988 to your computer and use it in GitHub Desktop.
Save Maff-/829988 to your computer and use it in GitHub Desktop.
Demo setup of jsdom + htmlparser + jquery on Node (v0.4.0). Not sure if this is the right way to do it, please fork/comment if you have suggestions.
/**
* jsdom + htmlparser + jQuery @ Node v0.4.0
* use npm to install jsdom & htmlparser:
* $ npm install jsdom@0.1.23
* $ npm install htmlparser
*
* Note: not sure what versions work or not
*
* https://github.com/tmpvar/jsdom
* https://github.com/tautologistics/node-htmlparser
*/
var jsdom = require("jsdom@0.1.23")
,htmlparser = require("htmlparser")
;
// some example chunks of data
var htmlChunks = [
'<h2>H2 Title</h2><div class="foo"><a href="http://google.com/"><img src="http://www.google.com/images/logos/ps_logo2.png" /></a><p>Some text is here</p></div>',
'<h1>Misformed header</div><a href="http://google.com/"><img src="http://www.google.com/images/logos/ps_logo.png" title="Google logo" alt="Google logo" /></a><p class="foo">Some text is here</p>',
'<p>Some text is here</p><div class="foo"><a href="http://google.com/" title="Link to google.com">Google.com</a></div>'
];
//htmlChunks = ['<html><head><title>test</title></head><body><p>foobar</p></body></html>'];
htmlChunks.forEach(function (htmlChunk) {
// wrap the htmlChunk with html/head/body tags, shouldn't this be done by jsdom or parser somewhere?
// also not sure if node-htmlparser is of better use than the default sax based parser
var window = jsdom.jsdom('<html><head></head><body>'+htmlChunk+'</body></html>', null, {parser: htmlparser}).createWindow();
//var window = jsdom.jsdom('<html><head></head><body>'+htmlChunk+'</body></html>').createWindow();
// should suplly local path to jquery to prevent reloading jquery again and again
jsdom.jQueryify(window, 'http://code.jquery.com/jquery-1.5.min.js' , function() {
var $ = window.$;
var imgs = $('a > img');
imgs.each(function() {
console.log($(this).attr('src'));
});
});
});
@tmpvar
Copy link

tmpvar commented Feb 16, 2011

looks solid

@Maff-
Copy link
Author

Maff- commented Feb 16, 2011

Thanks, good to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment