Implement DOMParser emulation based on jsdom.

This commit is contained in:
Jürg Lehni 2013-05-09 00:22:20 -07:00
parent ecbde1af14
commit ee3fa3b00f

View file

@ -27,9 +27,9 @@ var options = {
version: 'dev' version: 'dev'
}; };
// Create a window and document using jsdom, e.g. for exportSVG() // Create a document and a window using jsdom, e.g. for exportSVG()
var win = jsdom.createWindow(), var doc = jsdom.jsdom("<html><body></body></html>"),
doc = win.document = jsdom.jsdom("<html><body></body></html>"); win = doc.createWindow();
// Define XMLSerializer. // Define XMLSerializer.
// TODO: Put this into a simple node module, with dependency on jsdom // TODO: Put this into a simple node module, with dependency on jsdom
@ -45,6 +45,15 @@ XMLSerializer.prototype.serializeToString = function(node) {
}); });
}; };
function DOMParser() {
}
DOMParser.prototype.parseFromString = function(string, contenType) {
var div = doc.createElement('div');
div.innerHTML = string;
return div.firstChild;
};
// Create the context within which we will run the source files: // Create the context within which we will run the source files:
var dirname = path.resolve(__dirname, '..'); var dirname = path.resolve(__dirname, '..');
var context = vm.createContext({ var context = vm.createContext({
@ -67,6 +76,8 @@ var context = vm.createContext({
Canvas: Canvas, Canvas: Canvas,
// Expose global browser variables: // Expose global browser variables:
HTMLCanvasElement: Canvas, HTMLCanvasElement: Canvas,
XMLSerializer: XMLSerializer,
DOMParser: DOMParser,
Image: Canvas.Image, Image: Canvas.Image,
window: win, window: win,
document: doc, document: doc,
@ -96,7 +107,8 @@ Base.isPlainObject = function(obj) {
// Expose the Canvas, XMLSerializer to paper scopes: // Expose the Canvas, XMLSerializer to paper scopes:
Base.each({ Base.each({
Canvas: Canvas, Canvas: Canvas,
XMLSerializer: XMLSerializer XMLSerializer: XMLSerializer,
DOMParser: DOMParser
}, function(value, key) { }, function(value, key) {
this[key] = value; this[key] = value;
}, context.PaperScope.prototype); }, context.PaperScope.prototype);