Skip to content

Instantly share code, notes, and snippets.

@hober
Created August 26, 2010 15:17
Show Gist options
  • Save hober/551578 to your computer and use it in GitHub Desktop.
Save hober/551578 to your computer and use it in GitHub Desktop.
var sys = require('sys');
var browser = require('jsdom/browser')
var domToHtml = require('jsdom/browser/domtohtml').domToHtml;
var DOM = browser.browserAugmentation(require('jsdom/level2/core').dom.level2.core)
var doc = new DOM.Document('html');
doc.appendChild(doc.createElement('html'));
var headEl = doc.createElement('head');
doc.documentElement.appendChild(headEl);
var metaEl = doc.createElement('meta');
headEl.appendChild(metaEl);
metaEl.setAttribute('charset', 'utf-8');
var linkEl = doc.createElement('link');
headEl.appendChild(linkEl);
linkEl.setAttribute('href', '/foo.css');
var bodyEl = doc.createElement('body');
doc.documentElement.appendChild(bodyEl);
var h1El = doc.createElement('h1');
bodyEl.appendChild(h1El);
h1El.setAttribute('class', 'foo');
h1El.id = "bar";
h1El.appendChild(doc.createTextNode("hello, world"));
sys.puts(domToHtml(doc));
/* When run, this is what I'm seeing:
<html>
<head>
<meta>
</meta>
<link>
</head>
<body>
<h1>hello, world</h1>
</body>
</html>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment