Constructing Page from document fragment

47 views
Skip to first unread message

Dale Harvey

unread,
Feb 4, 2015, 8:38:20 AM2/4/15
to js...@googlegroups.com
Having a bit of a problem with what I am hoping is something simple, trying to insert a partial html fragment into a dom

var jsdom = require('jsdom').jsdom;
 
var html = jsdom('<html><div id="content"></div></html>');
var content = jsdom('<h1>Yay</h1>');
 
html
.getElementById('content').appendChild(content);
 
// I want:
// <html><div id="content"><h1>Yay</h1></div></html>
 
// I get:
 
DOMException: Wrong document
    at core
.Node.insertBefore (/Users/daleharvey/src/arandomurl.com/node_modules/jsdom/lib/jsdom/level1/core.js:579:13)
    at core
.Node.insertBefore (/Users/daleharvey/src/arandomurl.com/node_modules/jsdom/lib/jsdom/level2/events.js:326:32)
    at core
.Node.appendChild (/Users/daleharvey/src/arandomurl.com/node_modules/jsdom/lib/jsdom/level1/core.js:761:17)
    at repl
:1:32
    at
REPLServer.self.eval (repl.js:110:21)
    at repl
.js:249:20
    at
REPLServer.self.eval (repl.js:122:7)
    at
Interface.<anonymous> (repl.js:239:12)
    at
Interface.EventEmitter.emit (events.js:95:17)
    at
Interface._onLine (readline.js:202:10)

Anyone got any pointers for this?

Cheers
Dale

Domenic Denicola

unread,
Feb 20, 2015, 1:59:01 PM2/20/15
to js...@googlegroups.com
You can't append a document node to a <div>. If you want to insert a <h1>, you'll need to extract that h1, e.g. by doing `content.querySelector("h1")` and then inserting that node. Even then, inserting nodes across documents is pretty sketchy and not guaranteed to work.
Reply all
Reply to author
Forward
0 new messages