diff options
-rw-r--r-- | .config/karma.conf.js | 2 | ||||
-rw-r--r-- | .config/karma.quick.js | 2 | ||||
-rw-r--r-- | dist/svg.js | 104 | ||||
-rw-r--r-- | src/animation/Animator.js | 12 | ||||
-rw-r--r-- | src/animation/Timeline.js | 7 | ||||
-rw-r--r-- | src/elements/Bare.js | 6 | ||||
-rw-r--r-- | src/elements/Doc.js | 6 | ||||
-rw-r--r-- | src/elements/Dom.js | 10 | ||||
-rw-r--r-- | src/elements/Image.js | 6 | ||||
-rw-r--r-- | src/elements/Text.js | 6 | ||||
-rw-r--r-- | src/elemnts-svg.js | 68 | ||||
-rw-r--r-- | src/modules/core/event.js | 8 | ||||
-rw-r--r-- | src/modules/core/parser.js | 6 | ||||
-rw-r--r-- | src/modules/core/selector.js | 6 | ||||
-rw-r--r-- | src/modules/core/textable.js | 6 | ||||
-rw-r--r-- | src/types/Box.js | 14 | ||||
-rw-r--r-- | src/utils/adopter.js | 12 | ||||
-rw-r--r-- | src/utils/window.js | 13 |
18 files changed, 94 insertions, 200 deletions
diff --git a/.config/karma.conf.js b/.config/karma.conf.js index 7421808..61fe206 100644 --- a/.config/karma.conf.js +++ b/.config/karma.conf.js @@ -32,7 +32,7 @@ module.exports = function(config) { served: true }, 'dist/svg.js', - 'spec/spec/**/*.js' + 'spec/spec/*.js' ], proxies: { diff --git a/.config/karma.quick.js b/.config/karma.quick.js index 4574707..8d6dc2e 100644 --- a/.config/karma.quick.js +++ b/.config/karma.quick.js @@ -27,7 +27,7 @@ module.exports = function(config) { served: true }, 'dist/svg.js', - 'spec/spec/**/*.js' + 'spec/spec/*.js' ], diff --git a/dist/svg.js b/dist/svg.js index 2243698..8cd3575 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -6,7 +6,7 @@ * @copyright Wout Fierens <wout@mick-wout.com> * @license MIT * -* BUILT: Sun Nov 11 2018 17:18:46 GMT+0100 (GMT+01:00) +* BUILT: Mon Nov 12 2018 09:31:46 GMT+0100 (GMT+01:00) */; var SVG = (function () { 'use strict'; @@ -321,26 +321,26 @@ var SVG = (function () { var svgjs = 'http://svgjs.com/svgjs'; var globals = { - window: window, - document: document + window: typeof window === 'undefined' ? null : window, + document: typeof document === 'undefined' ? null : document }; - function registerWindow(w) { - globals.window = w; - globals.document = w.document; + function registerWindow() { + var win = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; + var doc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; + globals.window = win; + globals.document = doc; } var Base = function Base() { _classCallCheck(this, Base); }; - var window$1 = globals.window, - document$1 = globals.document; var elements = {}; var root = Symbol('root'); // Method for element creation function makeNode(name) { // create element - return document$1.createElementNS(ns, name); + return globals.document.createElementNS(ns, name); } function makeInstance(element) { if (element instanceof Base) return element; @@ -354,7 +354,7 @@ var SVG = (function () { } if (typeof element === 'string' && element.charAt(0) !== '<') { - return adopt(document$1.querySelector(element)); + return adopt(globals.document.querySelector(element)); } var node = makeNode('svg'); @@ -365,7 +365,7 @@ var SVG = (function () { return element; } function nodeOrNew(name, node) { - return node instanceof window$1.Node ? node : makeNode(name); + return node instanceof globals.window.Node ? node : makeNode(name); } // Adopt existing svg elements function adopt(node) { @@ -374,7 +374,7 @@ var SVG = (function () { if (node.instance instanceof Base) return node.instance; - if (!(node instanceof window$1.SVGElement)) { + if (!(node instanceof globals.window.SVGElement)) { return new elements.HtmlNode(node); } // initialize variables @@ -847,7 +847,6 @@ var SVG = (function () { memory: memory }); - var window$2 = globals.window; var listenerId = 0; function getEvents(node) { @@ -953,10 +952,10 @@ var SVG = (function () { function dispatch(node, event, data) { var n = getEventTarget(node); // Dispatch event - if (event instanceof window$2.Event) { + if (event instanceof globals.window.Event) { n.dispatchEvent(event); } else { - event = new window$2.CustomEvent(event, { + event = new globals.window.CustomEvent(event, { detail: data, cancelable: true }); @@ -1024,6 +1023,8 @@ var SVG = (function () { this.g = g; this.b = b; } + + return this; } // Default to hex conversion }, { @@ -1239,6 +1240,7 @@ var SVG = (function () { init: function init(arr) { this.length = 0; this.push.apply(this, _toConsumableArray(this.parse(arr))); + return this; }, toArray: function toArray() { return Array.prototype.concat.apply([], this); @@ -1311,6 +1313,8 @@ var SVG = (function () { this.unit = value.unit; } } + + return this; } }, { key: "toString", @@ -1457,9 +1461,6 @@ var SVG = (function () { return this; } - var window$3 = globals.window, - document$2 = globals.document; - var Dom = /*#__PURE__*/ function (_EventTarget) { @@ -1624,7 +1625,7 @@ var SVG = (function () { parent = adopt(parent.node.parentNode); if (!type) return parent; // loop trough ancestors if type is given - while (parent && parent.node instanceof window$3.SVGElement) { + while (parent && parent.node instanceof globals.window.SVGElement) { // FIXME: That shouldnt be neccessary if (typeof type === 'string' ? parent.matches(type) : parent instanceof type) return parent; parent = adopt(parent.node.parentNode); @@ -1746,8 +1747,8 @@ var SVG = (function () { outerHTML = outerHTML == null ? false : outerHTML; // Create temporary holder - well = document$2.createElementNS(ns, 'svg'); - fragment = document$2.createDocumentFragment(); // Dump raw svg + well = globals.document.createElementNS(ns, 'svg'); + fragment = globals.document.createDocumentFragment(); // Dump raw svg well.innerHTML = svgOrFn; // Transplant nodes into the fragment @@ -1998,8 +1999,6 @@ var SVG = (function () { }(Container); register(Defs); - var window$4 = globals.window; - var Doc$1 = /*#__PURE__*/ function (_Container) { @@ -2020,7 +2019,7 @@ var SVG = (function () { _createClass(Doc, [{ key: "isRoot", value: function isRoot() { - return !this.node.parentNode || !(this.node.parentNode instanceof window$4.SVGElement) || this.node.parentNode.nodeName === '#document'; + return !this.node.parentNode || !(this.node.parentNode instanceof globals.window.SVGElement) || this.node.parentNode.nodeName === '#document'; } // Check if this is a root svg // If not, call docs from this element @@ -2081,7 +2080,6 @@ var SVG = (function () { }); register(Doc$1, 'Doc', true); - var document$3 = globals.document; function parser() { // Reuse cached element if possible if (!parser.nodes) { @@ -2095,7 +2093,7 @@ var SVG = (function () { } if (!parser.nodes.svg.node.parentNode) { - var b = document$3.body || document$3.documentElement; + var b = globals.document.body || globals.document.documentElement; parser.nodes.svg.addTo(b); } @@ -2134,6 +2132,7 @@ var SVG = (function () { }; this.x = source.x == null ? base.x : source.x; this.y = source.y == null ? base.y : source.y; + return this; } // Clone point }, { @@ -2209,6 +2208,7 @@ var SVG = (function () { this.d = source.d != null ? source.d : base.d; this.e = source.e != null ? source.e : base.e; this.f = source.f != null ? source.f : base.f; + return this; } // Clones this matrix }, { @@ -3151,12 +3151,11 @@ var SVG = (function () { return Queue; }(); - var window$5 = globals.window; var Animator = { nextDraw: null, frames: new Queue(), timeouts: new Queue(), - timer: window$5.performance || window$5.Date, + timer: globals.window.performance || globals.window.Date, transforms: [], frame: function frame(fn) { // Store the node @@ -3165,7 +3164,7 @@ var SVG = (function () { }); // Request an animation frame if we don't have one if (Animator.nextDraw === null) { - Animator.nextDraw = window$5.requestAnimationFrame(Animator._draw); + Animator.nextDraw = globals.window.requestAnimationFrame(Animator._draw); } // Return the node so we can remove it easily @@ -3185,7 +3184,7 @@ var SVG = (function () { }); // Request another animation frame if we need one if (Animator.nextDraw === null) { - Animator.nextDraw = window$5.requestAnimationFrame(Animator._draw); + Animator.nextDraw = globals.window.requestAnimationFrame(Animator._draw); } return node; @@ -3226,26 +3225,23 @@ var SVG = (function () { el(); }); // If we have remaining timeouts or frames, draw until we don't anymore - Animator.nextDraw = Animator.timeouts.first() || Animator.frames.first() ? window$5.requestAnimationFrame(Animator._draw) : null; + Animator.nextDraw = Animator.timeouts.first() || Animator.frames.first() ? globals.window.requestAnimationFrame(Animator._draw) : null; } }; - var window$6 = globals.window, - document$4 = globals.document; - function isNulledBox(box) { return !box.w && !box.h && !box.x && !box.y; } function domContains(node) { - return (document$4.documentElement.contains || function (node) { + return (globals.document.documentElement.contains || function (node) { // This is IE - it does not support contains() for top-level SVGs while (node.parentNode) { node = node.parentNode; } - return node === document$4; - }).call(document$4.documentElement, node); + return node === document; + }).call(globals.document.documentElement, node); } var Box = @@ -3304,8 +3300,8 @@ var SVG = (function () { key: "addOffset", value: function addOffset() { // offset by window scroll position, because getBoundingClientRect changes when window is scrolled - this.x += window$6.pageXOffset; - this.y += window$6.pageYOffset; + this.x += globals.window.pageXOffset; + this.y += globals.window.pageYOffset; return this; } }, { @@ -3343,7 +3339,7 @@ var SVG = (function () { box = cb(clone.node); clone.remove(); } catch (e) { - console.warn('Getting a bounding box of this element is not possible'); + throw new Error('Getting a bounding box of element "' + this.node.nodeName + '" is not possible'); } } @@ -3809,6 +3805,7 @@ var SVG = (function () { value: function init(val) { val = Array.isArray(val) ? val[0] : val; this.value = val; + return this; } }, { key: "valueOf", @@ -3850,6 +3847,7 @@ var SVG = (function () { } Object.assign(this, TransformBag.defaults, obj); + return this; } }, { key: "toArray", @@ -3896,6 +3894,7 @@ var SVG = (function () { this.values = entries.reduce(function (last, curr) { return last.concat(curr); }, []); + return this; } }, { key: "valueOf", @@ -3935,9 +3934,7 @@ var SVG = (function () { }); } - var window$7 = globals.window, - document$5 = globals.document; - var time = window$7.performance || Date; + var time = globals.window.performance || Date; var makeSchedule = function makeSchedule(runnerInfo) { var start = runnerInfo.start; @@ -3962,7 +3959,7 @@ var SVG = (function () { return time.now(); }; - this._dispatcher = document$5.createElement('div'); // Store the timing variables + this._dispatcher = globals.document.createElement('div'); // Store the timing variables this._startTime = 0; this._speed = 1.0; // Play control variables control how the animation proceeds @@ -5507,9 +5504,8 @@ var SVG = (function () { }(Element); register(Stop); - var document$6 = globals.document; function baseFind(query, parent) { - return map((parent || document$6).querySelectorAll(query), function (node) { + return map((parent || globals.document).querySelectorAll(query), function (node) { return adopt(node); }); } // Scoped find method @@ -5682,8 +5678,6 @@ var SVG = (function () { }); register(Pattern); - var window$8 = globals.window; - var Image = /*#__PURE__*/ function (_Shape) { @@ -5700,7 +5694,7 @@ var SVG = (function () { key: "load", value: function load(url, callback) { if (!url) return this; - var img = new window$8.Image(); + var img = new globals.window.Image(); on(img, 'load', function (e) { var p = this.parent(Pattern); // ensure image size @@ -6250,8 +6244,6 @@ var SVG = (function () { }); register(Rect); - var document$7 = globals.document; // Create plain text node - function plain(text) { // clear if build mode is disabled if (this._build === false) { @@ -6259,7 +6251,7 @@ var SVG = (function () { } // create text node - this.node.appendChild(document$7.createTextNode(text)); + this.node.appendChild(globals.document.createTextNode(text)); return this; } // Get length of text element @@ -6272,8 +6264,6 @@ var SVG = (function () { length: length }); - var window$9 = globals.window; - var Text = /*#__PURE__*/ function (_Shape) { @@ -6409,7 +6399,7 @@ var SVG = (function () { var blankLineOffset = 0; var leading = this.dom.leading; this.each(function () { - var fontSize = window$9.getComputedStyle(this.node).getPropertyValue('font-size'); + var fontSize = globals.window.getComputedStyle(this.node).getPropertyValue('font-size'); var dy = leading * new SVGNumber(fontSize); if (this.dom.newLined) { @@ -6527,8 +6517,6 @@ var SVG = (function () { }); register(Tspan); - var document$8 = globals.document; - var Bare = /*#__PURE__*/ function (_Container) { @@ -6549,7 +6537,7 @@ var SVG = (function () { } // create text node - this.node.appendChild(document$8.createTextNode(text)); + this.node.appendChild(globals.document.createTextNode(text)); return this; } }]); diff --git a/src/animation/Animator.js b/src/animation/Animator.js index 4e0b112..9b460f5 100644 --- a/src/animation/Animator.js +++ b/src/animation/Animator.js @@ -1,13 +1,11 @@ -import globals from '../utils/window.js' +import { globals } from '../utils/window.js' import Queue from './Queue.js' -const { window } = globals - const Animator = { nextDraw: null, frames: new Queue(), timeouts: new Queue(), - timer: window.performance || window.Date, + timer: globals.window.performance || globals.window.Date, transforms: [], frame (fn) { @@ -16,7 +14,7 @@ const Animator = { // Request an animation frame if we don't have one if (Animator.nextDraw === null) { - Animator.nextDraw = window.requestAnimationFrame(Animator._draw) + Animator.nextDraw = globals.window.requestAnimationFrame(Animator._draw) } // Return the node so we can remove it easily @@ -38,7 +36,7 @@ const Animator = { // Request another animation frame if we need one if (Animator.nextDraw === null) { - Animator.nextDraw = window.requestAnimationFrame(Animator._draw) + Animator.nextDraw = globals.window.requestAnimationFrame(Animator._draw) } return node @@ -80,7 +78,7 @@ const Animator = { // If we have remaining timeouts or frames, draw until we don't anymore Animator.nextDraw = Animator.timeouts.first() || Animator.frames.first() - ? window.requestAnimationFrame(Animator._draw) + ? globals.window.requestAnimationFrame(Animator._draw) : null } } diff --git a/src/animation/Timeline.js b/src/animation/Timeline.js index ff30a0d..f1d540a 100644 --- a/src/animation/Timeline.js +++ b/src/animation/Timeline.js @@ -1,9 +1,8 @@ import { registerMethods } from '../utils/methods.js' import Animator from './Animator.js' -import globals from '../utils/window.js' +import { globals } from '../utils/window.js' -const { window, document } = globals -var time = window.performance || Date +var time = globals.window.performance || Date var makeSchedule = function (runnerInfo) { var start = runnerInfo.start @@ -19,7 +18,7 @@ export default class Timeline { return time.now() } - this._dispatcher = document.createElement('div') + this._dispatcher = globals.document.createElement('div') // Store the timing variables this._startTime = 0 diff --git a/src/elements/Bare.js b/src/elements/Bare.js index 7162f3a..a057634 100644 --- a/src/elements/Bare.js +++ b/src/elements/Bare.js @@ -1,9 +1,7 @@ import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js' import { registerMethods } from '../utils/methods.js' import Container from './Container.js' -import globals from '../utils/window.js' - -const { document } = globals +import { globals } from '../utils/window.js' export default class Bare extends Container { constructor (node, attrs) { @@ -17,7 +15,7 @@ export default class Bare extends Container { } // create text node - this.node.appendChild(document.createTextNode(text)) + this.node.appendChild(globals.document.createTextNode(text)) return this } diff --git a/src/elements/Doc.js b/src/elements/Doc.js index 952f1d3..d56fae3 100644 --- a/src/elements/Doc.js +++ b/src/elements/Doc.js @@ -8,9 +8,7 @@ import { ns, svgjs, xlink, xmlns } from '../modules/core/namespaces.js' import { registerMethods } from '../utils/methods.js' import Container from './Container.js' import Defs from './Defs.js' -import globals from '../utils/window.js' - -const { window } = globals +import { globals } from '../utils/window.js' export default class Doc extends Container { constructor (node) { @@ -20,7 +18,7 @@ export default class Doc extends Container { isRoot () { return !this.node.parentNode || - !(this.node.parentNode instanceof window.SVGElement) || + !(this.node.parentNode instanceof globals.window.SVGElement) || this.node.parentNode.nodeName === '#document' } diff --git a/src/elements/Dom.js b/src/elements/Dom.js index 8fa053c..192b9bd 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -8,12 +8,10 @@ import { } from '../utils/adopter.js' import { map } from '../utils/utils.js' import { ns } from '../modules/core/namespaces.js' -import globals from '../utils/window.js' +import { globals } from '../utils/window.js' import EventTarget from '../types/EventTarget.js' import attr from '../modules/core/attr.js' -const { window, document } = globals - export default class Dom extends EventTarget { constructor (node, attrs) { super(node) @@ -156,7 +154,7 @@ export default class Dom extends EventTarget { if (!type) return parent // loop trough ancestors if type is given - while (parent && parent.node instanceof window.SVGElement) { // FIXME: That shouldnt be neccessary + while (parent && parent.node instanceof globals.window.SVGElement) { // FIXME: That shouldnt be neccessary if (typeof type === 'string' ? parent.matches(type) : parent instanceof type) return parent parent = adopt(parent.node.parentNode) } @@ -279,8 +277,8 @@ export default class Dom extends EventTarget { outerHTML = outerHTML == null ? false : outerHTML // Create temporary holder - well = document.createElementNS(ns, 'svg') - fragment = document.createDocumentFragment() + well = globals.document.createElementNS(ns, 'svg') + fragment = globals.document.createDocumentFragment() // Dump raw svg well.innerHTML = svgOrFn diff --git a/src/elements/Image.js b/src/elements/Image.js index 6a01b8f..7f00980 100644 --- a/src/elements/Image.js +++ b/src/elements/Image.js @@ -6,9 +6,7 @@ import { registerMethods } from '../utils/methods.js' import { xlink } from '../modules/core/namespaces.js' import Pattern from './Pattern.js' import Shape from './Shape.js' -import globals from '../utils/window.js' - -const { window } = globals +import { globals } from '../utils/window.js' export default class Image extends Shape { constructor (node) { @@ -19,7 +17,7 @@ export default class Image extends Shape { load (url, callback) { if (!url) return this - var img = new window.Image() + var img = new globals.window.Image() on(img, 'load', function (e) { var p = this.parent(Pattern) diff --git a/src/elements/Text.js b/src/elements/Text.js index 41be916..db9c2ee 100644 --- a/src/elements/Text.js +++ b/src/elements/Text.js @@ -9,11 +9,9 @@ import { attrs } from '../modules/core/defaults.js' import { registerMethods } from '../utils/methods.js' import SVGNumber from '../types/SVGNumber.js' import Shape from './Shape.js' -import globals from '../utils/window.js' +import { globals } from '../utils/window.js' import * as textable from '../modules/core/textable.js' -const { window } = globals - export default class Text extends Shape { // Initialize node constructor (node) { @@ -134,7 +132,7 @@ export default class Text extends Shape { var leading = this.dom.leading this.each(function () { - var fontSize = window.getComputedStyle(this.node) + var fontSize = globals.window.getComputedStyle(this.node) .getPropertyValue('font-size') var dy = leading * new SVGNumber(fontSize) diff --git a/src/elemnts-svg.js b/src/elemnts-svg.js deleted file mode 100644 index b5b2542..0000000 --- a/src/elemnts-svg.js +++ /dev/null @@ -1,68 +0,0 @@ -import { ns } from './namespaces.js' - -/* eslint no-unused-vars: "off" */ -var a = { - // Import raw svg - svg (svg, fn = false) { - var well, len, fragment - - // act as getter if no svg string is given - if (svg == null || svg === true || typeof svg === 'function') { - // write svgjs data to the dom - this.writeDataToDom() - let current = this - - // An export modifier was passed - if (typeof svg === 'function') { - // Juggle arguments - [fn, svg] = [svg, fn] - - // If the user wants outerHTML we need to process this node, too - if (!svg) { - current = fn(current) - - // The user does not want this node? Well, then he gets nothing - if (current === false) return '' - } - - // Deep loop through all children and apply modifier - current.each(function () { - let result = fn(this) - - // If modifier returns false, discard node - if (result === false) { - this.remove() - - // If modifier returns new node, use it - } else if (result !== this) { - this.replace(result) - } - }, true) - } - - // Return outer or inner content - return svg - ? current.node.innerHTML - : current.node.outerHTML - } - - // Act as setter if we got a string - - // Create temporary holder - well = document.createElementNS(ns, 'svg') - fragment = document.createDocumentFragment() - - // Dump raw svg - well.innerHTML = svg - - // Transplant nodes into the fragment - for (len = well.children.length; len--;) { - fragment.appendChild(well.firstElementChild) - } - - // Add the whole fragment at once - this.node.appendChild(fragment) - - return this - } -} diff --git a/src/modules/core/event.js b/src/modules/core/event.js index 351fe3f..a52a744 100644 --- a/src/modules/core/event.js +++ b/src/modules/core/event.js @@ -1,8 +1,6 @@ import { delimiter } from './regex.js' import { makeInstance } from '../../utils/adopter.js' -import globals from '../../utils/window.js' - -const { window } = globals +import { globals } from '../../utils/window.js' let listenerId = 0 @@ -112,10 +110,10 @@ export function dispatch (node, event, data) { var n = getEventTarget(node) // Dispatch event - if (event instanceof window.Event) { + if (event instanceof globals.window.Event) { n.dispatchEvent(event) } else { - event = new window.CustomEvent(event, { detail: data, cancelable: true }) + event = new globals.window.CustomEvent(event, { detail: data, cancelable: true }) n.dispatchEvent(event) } return event diff --git a/src/modules/core/parser.js b/src/modules/core/parser.js index a490576..ccbbc54 100644 --- a/src/modules/core/parser.js +++ b/src/modules/core/parser.js @@ -1,7 +1,5 @@ import Doc from '../../elements/Doc.js' -import globals from '../../utils/window.js' - -const { document } = globals +import { globals } from '../../utils/window.js' export default function parser () { // Reuse cached element if possible @@ -21,7 +19,7 @@ export default function parser () { } if (!parser.nodes.svg.node.parentNode) { - let b = document.body || document.documentElement + let b = globals.document.body || globals.document.documentElement parser.nodes.svg.addTo(b) } diff --git a/src/modules/core/selector.js b/src/modules/core/selector.js index 52a7ad1..f2a7c58 100644 --- a/src/modules/core/selector.js +++ b/src/modules/core/selector.js @@ -1,12 +1,10 @@ import { adopt } from '../../utils/adopter.js' import { map } from '../../utils/utils.js' import { registerMethods } from '../../utils/methods.js' -import globals from '../../utils/window.js' - -const { document } = globals +import { globals } from '../../utils/window.js' export default function baseFind (query, parent) { - return map((parent || document).querySelectorAll(query), function (node) { + return map((parent || globals.document).querySelectorAll(query), function (node) { return adopt(node) }) } diff --git a/src/modules/core/textable.js b/src/modules/core/textable.js index cf452c6..55df7c6 100644 --- a/src/modules/core/textable.js +++ b/src/modules/core/textable.js @@ -1,6 +1,4 @@ -import globals from '../../utils/window.js' - -const { document } = globals +import { globals } from '../../utils/window.js' // Create plain text node export function plain (text) { @@ -10,7 +8,7 @@ export function plain (text) { } // create text node - this.node.appendChild(document.createTextNode(text)) + this.node.appendChild(globals.document.createTextNode(text)) return this } diff --git a/src/types/Box.js b/src/types/Box.js index 97ba699..8c1c4ca 100644 --- a/src/types/Box.js +++ b/src/types/Box.js @@ -1,23 +1,21 @@ import { delimiter } from '../modules/core/regex.js' import { registerMethods } from '../utils/methods.js' -import globals from '../utils/window.js' +import { globals } from '../utils/window.js' import Point from './Point.js' import parser from '../modules/core/parser.js' -const { window, document } = globals - function isNulledBox (box) { return !box.w && !box.h && !box.x && !box.y } function domContains (node) { - return (document.documentElement.contains || function (node) { + return (globals.document.documentElement.contains || function (node) { // This is IE - it does not support contains() for top-level SVGs while (node.parentNode) { node = node.parentNode } return node === document - }).call(document.documentElement, node) + }).call(globals.document.documentElement, node) } export default class Box { @@ -88,8 +86,8 @@ export default class Box { addOffset () { // offset by window scroll position, because getBoundingClientRect changes when window is scrolled - this.x += window.pageXOffset - this.y += window.pageYOffset + this.x += globals.window.pageXOffset + this.y += globals.window.pageYOffset return this } @@ -121,7 +119,7 @@ function getBox (cb) { box = cb(clone.node) clone.remove() } catch (e) { - console.warn('Getting a bounding box of this element is not possible') + throw new Error('Getting a bounding box of element "' + this.node.nodeName + '" is not possible') } } return box diff --git a/src/utils/adopter.js b/src/utils/adopter.js index 5d5d1f0..5de4038 100644 --- a/src/utils/adopter.js +++ b/src/utils/adopter.js @@ -1,17 +1,15 @@ import { capitalize } from './utils.js' import { ns } from '../modules/core/namespaces.js' -import globals from '../utils/window.js' +import { globals } from '../utils/window.js' import Base from '../types/Base.js' -const { window, document } = globals - const elements = {} export const root = Symbol('root') // Method for element creation export function makeNode (name) { // create element - return document.createElementNS(ns, name) + return globals.document.createElementNS(ns, name) } export function makeInstance (element) { @@ -26,7 +24,7 @@ export function makeInstance (element) { } if (typeof element === 'string' && element.charAt(0) !== '<') { - return adopt(document.querySelector(element)) + return adopt(globals.document.querySelector(element)) } var node = makeNode('svg') @@ -40,7 +38,7 @@ export function makeInstance (element) { } export function nodeOrNew (name, node) { - return node instanceof window.Node ? node : makeNode(name) + return node instanceof globals.window.Node ? node : makeNode(name) } // Adopt existing svg elements @@ -51,7 +49,7 @@ export function adopt (node) { // make sure a node isn't already adopted if (node.instance instanceof Base) return node.instance - if (!(node instanceof window.SVGElement)) { + if (!(node instanceof globals.window.SVGElement)) { return new elements.HtmlNode(node) } diff --git a/src/utils/window.js b/src/utils/window.js index f44ebb9..9e51339 100644 --- a/src/utils/window.js +++ b/src/utils/window.js @@ -1,10 +1,9 @@ -const globals = { - window, document +export const globals = { + window: typeof window === 'undefined' ? null : window, + document: typeof document === 'undefined' ? null : document } -export default globals - -export function registerWindow (w) { - globals.window = w - globals.document = w.document +export function registerWindow (win = null, doc = null) { + globals.window = win + globals.document = doc } |