Skip to content

Instantly share code, notes, and snippets.

@clarkdave
Created May 6, 2011 21:24
Show Gist options
  • Save clarkdave/959811 to your computer and use it in GitHub Desktop.
Save clarkdave/959811 to your computer and use it in GitHub Desktop.

Revisions

  1. Dave Clark created this gist May 6, 2011.
    26 changes: 26 additions & 0 deletions parseAndModifyHtml.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /**
    * npm install jsdom
    * npm install jquery
    */

    var html = "<!doctype html><html><body><h1>Hello world!</h1></body></html>";

    /* parse the html and create a dom window */
    var window = require('jsdom').jsdom(html, null, {
    // standard options: disable loading other assets
    // or executing script tags
    FetchExternalResources: false,
    ProcessExternalResources: false,
    MutationEvents: false,
    QuerySelector: false
    }).createWindow();

    /* apply jquery to the window */
    var $ = require('jquery').create(window);

    /* modify html using jquery */
    $('h1').text('World hello!');
    $('body').append('<p>Lorem ipsum...</p>');

    /* output the modified html with doctype */
    console.log( window.document.doctype + window.document.innerHTML );