Created
February 16, 2011 19:33
-
-
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.
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
/** | |
* jsdom + htmlparser + jQuery @ Node v0.4.0 | |
* use npm to install jsdom & htmlparser: | |
* $ npm install [email protected] | |
* $ 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("[email protected]") | |
,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')); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks solid