From ee3fa3b00f411ddfb2e86e6f34ccbab7c5facd93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 9 May 2013 00:22:20 -0700 Subject: [PATCH] Implement DOMParser emulation based on jsdom. --- src/node/index.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/node/index.js b/src/node/index.js index 758b0e7d..e0efc3e3 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -27,9 +27,9 @@ var options = { version: 'dev' }; -// Create a window and document using jsdom, e.g. for exportSVG() -var win = jsdom.createWindow(), - doc = win.document = jsdom.jsdom(""); +// Create a document and a window using jsdom, e.g. for exportSVG() +var doc = jsdom.jsdom(""), + win = doc.createWindow(); // Define XMLSerializer. // 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: var dirname = path.resolve(__dirname, '..'); var context = vm.createContext({ @@ -67,6 +76,8 @@ var context = vm.createContext({ Canvas: Canvas, // Expose global browser variables: HTMLCanvasElement: Canvas, + XMLSerializer: XMLSerializer, + DOMParser: DOMParser, Image: Canvas.Image, window: win, document: doc, @@ -96,7 +107,8 @@ Base.isPlainObject = function(obj) { // Expose the Canvas, XMLSerializer to paper scopes: Base.each({ Canvas: Canvas, - XMLSerializer: XMLSerializer + XMLSerializer: XMLSerializer, + DOMParser: DOMParser }, function(value, key) { this[key] = value; }, context.PaperScope.prototype);