From 8f819bb7606ada611b71390dd9bbb0cc772aa8af Mon Sep 17 00:00:00 2001 From: kangax Date: Thu, 11 Aug 2011 15:18:18 -0400 Subject: [PATCH] Fabric can now run under node.js --- HEADER.js | 12 ++- dist/all.js | 175 +++++++++++++++++++----------------- lib/cufon.js | 82 ++++++++--------- src/canvas.class.js | 20 ++--- src/canvas.serialization.js | 4 +- src/image.class.js | 8 +- src/object.class.js | 2 +- src/text.class.js | 4 +- src/util.js | 2 +- src/util/dom_event.js | 22 ++--- src/util/dom_misc.js | 14 +-- src/util/dom_style.js | 4 +- src/util/misc.js | 3 +- test/unit/canvas.js | 30 ------- 14 files changed, 186 insertions(+), 196 deletions(-) diff --git a/HEADER.js b/HEADER.js index 58f2a593..3d49af74 100644 --- a/HEADER.js +++ b/HEADER.js @@ -1,7 +1,17 @@ /*! Fabric.js Copyright 2008-2011, Bitsonnet (Juriy Zaytsev, Maxim Chernyak) */ -var fabric = fabric || { version: "0.4.13" }; +var fabric = fabric || { version: "0.5" }; if (typeof exports != 'undefined') { exports.fabric = fabric; +} + +if (typeof document != 'undefined' && typeof window != 'undefined') { + fabric.document = document; + fabric.window = window; +} +else { + // assume we're running under node.js when document/window are not present + fabric.document = require("jsdom").jsdom(""); + fabric.window = fabric.document.createWindow(); } \ No newline at end of file diff --git a/dist/all.js b/dist/all.js index 485c22d2..49a53d83 100644 --- a/dist/all.js +++ b/dist/all.js @@ -1,11 +1,20 @@ /*! Fabric.js Copyright 2008-2011, Bitsonnet (Juriy Zaytsev, Maxim Chernyak) */ -var fabric = fabric || { version: "0.4.13" }; +var fabric = fabric || { version: "0.5" }; if (typeof exports != 'undefined') { exports.fabric = fabric; } +if (typeof document != 'undefined' && typeof window != 'undefined') { + fabric.document = document; + fabric.window = window; +} +else { + fabric.document = require("jsdom").jsdom(""); + fabric.window = fabric.document.createWindow(); +} + /*! * Copyright (c) 2009 Simo Kinnunen. * Licensed under the MIT license. @@ -30,20 +39,20 @@ var Cufon = (function() { }; - if (document.addEventListener) { - document.addEventListener('DOMContentLoaded', perform, false); - window.addEventListener('pageshow', perform, false); // For cached Gecko pages + if (fabric.document.addEventListener) { + fabric.document.addEventListener('DOMContentLoaded', perform, false); + fabric.window.addEventListener('pageshow', perform, false); // For cached Gecko pages } - if (!window.opera && document.readyState) (function() { - readyStatus[document.readyState] ? perform() : setTimeout(arguments.callee, 10); + if (!fabric.window.opera && fabric.document.readyState) (function() { + readyStatus[fabric.document.readyState] ? perform() : setTimeout(arguments.callee, 10); })(); - if (document.readyState && document.createStyleSheet) (function() { + if (fabric.document.readyState && fabric.document.createStyleSheet) (function() { try { - document.body.doScroll('left'); + fabric.document.body.doScroll('left'); perform(); } catch (e) { @@ -51,7 +60,7 @@ var Cufon = (function() { } })(); - addEvent(window, 'load', perform); // Fallback + addEvent(fabric.window, 'load', perform); // Fallback return function(listener) { if (!arguments.length) perform(); @@ -117,7 +126,7 @@ var Cufon = (function() { for (var i = 0, l = linkElements.length; link = linkElements[i], i < l; ++i) { if (!link.disabled && link.rel.toLowerCase() == 'stylesheet') ++linkStyles; } - if (document.styleSheets.length >= styleElements.length + linkStyles) perform(); + if (fabric.document.styleSheets.length >= styleElements.length + linkStyles) perform(); else setTimeout(arguments.callee, 10); }); @@ -129,7 +138,7 @@ var Cufon = (function() { })(), supports: function(property, value) { - var checker = document.createElement('span').style; + var checker = fabric.document.createElement('span').style; if (checker[property] === undefined) return false; checker[property] = value; return checker[property] === value; @@ -355,7 +364,7 @@ var Cufon = (function() { } else if (el.attachEvent) { el.attachEvent('on' + type, function() { - return listener.call(el, window.event); + return listener.call(el, fabric.window.event); }); } } @@ -389,7 +398,7 @@ var Cufon = (function() { } function elementsByTagName(query) { - return document.getElementsByTagName(query); + return fabric.document.getElementsByTagName(query); } function merge() { @@ -404,7 +413,7 @@ var Cufon = (function() { var separate = options.separate; if (separate == 'none') return engines[options.engine].apply(null, arguments); - var fragment = document.createDocumentFragment(), processed; + var fragment = fabric.document.createDocumentFragment(), processed; var parts = text.split(separators[separate]), needsAligning = (separate == 'words'); if (needsAligning && HAS_BROKEN_REGEXP) { if (/^\s/.test(text)) parts.unshift(''); @@ -463,12 +472,12 @@ var Cufon = (function() { }, printable: true, selector: ( - window.Sizzle - || (window.jQuery && function(query) { return jQuery(query); }) // avoid noConflict issues - || (window.dojo && dojo.query) - || (window.$$ && function(query) { return $$(query); }) - || (window.$ && function(query) { return $(query); }) - || (document.querySelectorAll && function(query) { return document.querySelectorAll(query); }) + fabric.window.Sizzle + || (fabric.window.jQuery && function(query) { return jQuery(query); }) // avoid noConflict issues + || (fabric.window.dojo && dojo.query) + || (fabric.window.$$ && function(query) { return $$(query); }) + || (fabric.window.$ && function(query) { return $(query); }) + || (fabric.document.querySelectorAll && function(query) { return fabric.document.querySelectorAll(query); }) || elementsByTagName ), separate: 'words', // 'none' and 'characters' are also accepted @@ -548,17 +557,17 @@ var Cufon = (function() { Cufon.registerEngine('canvas', (function() { - var check = document.createElement('canvas'); + var check = fabric.document.createElement('canvas'); if (!check || !check.getContext || !check.getContext.apply) return; check = null; var HAS_INLINE_BLOCK = Cufon.CSS.supports('display', 'inline-block'); - var HAS_BROKEN_LINEHEIGHT = !HAS_INLINE_BLOCK && (document.compatMode == 'BackCompat' || /frameset|transitional/i.test(document.doctype.publicId)); + var HAS_BROKEN_LINEHEIGHT = !HAS_INLINE_BLOCK && (fabric.document.compatMode == 'BackCompat' || /frameset|transitional/i.test(fabric.document.doctype.publicId)); - var styleSheet = document.createElement('style'); + var styleSheet = fabric.document.createElement('style'); styleSheet.type = 'text/css'; - styleSheet.appendChild(document.createTextNode( + styleSheet.appendChild(fabric.document.createTextNode( '.cufon-canvas{text-indent:0}' + '@media screen,projection{' + '.cufon-canvas{display:inline;display:inline-block;position:relative;vertical-align:middle' + @@ -576,7 +585,7 @@ Cufon.registerEngine('canvas', (function() { '.cufon-canvas .cufon-alt{display:inline}' + '}' )); - document.getElementsByTagName('head')[0].appendChild(styleSheet); + fabric.document.getElementsByTagName('head')[0].appendChild(styleSheet); function generateFromVML(path, context) { var atX = 0, atY = 0; @@ -677,17 +686,17 @@ Cufon.registerEngine('canvas', (function() { canvas = node.firstChild; } else { - wrapper = document.createElement('span'); + wrapper = fabric.document.createElement('span'); wrapper.className = 'cufon cufon-canvas'; wrapper.alt = text; - canvas = document.createElement('canvas'); + canvas = fabric.document.createElement('canvas'); wrapper.appendChild(canvas); if (options.printable) { - var print = document.createElement('span'); + var print = fabric.document.createElement('span'); print.className = 'cufon-alt'; - print.appendChild(document.createTextNode(text)); + print.appendChild(fabric.document.createTextNode(text)); wrapper.appendChild(print); } } @@ -926,21 +935,21 @@ Cufon.registerEngine('canvas', (function() { Cufon.registerEngine('vml', (function() { - if (!document.namespaces) return; + if (!fabric.document.namespaces) return; - var canvasEl = document.createElement('canvas'); + var canvasEl = fabric.document.createElement('canvas'); if (canvasEl && canvasEl.getContext && canvasEl.getContext.apply) return; - if (document.namespaces.cvml == null) { - document.namespaces.add('cvml', 'urn:schemas-microsoft-com:vml'); + if (fabric.document.namespaces.cvml == null) { + fabric.document.namespaces.add('cvml', 'urn:schemas-microsoft-com:vml'); } - var check = document.createElement('cvml:shape'); + var check = fabric.document.createElement('cvml:shape'); check.style.behavior = 'url(#default#VML)'; if (!check.coordsize) return; // VML isn't supported check = null; - document.write('