diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 09:35:37 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 09:35:37 +0100 |
commit | d3f8d83551799db061a558537bc89dcbfd522c21 (patch) | |
tree | f6456de7ac3599c35b217c9f769cb1adca03997f /src | |
parent | bf7703f5915c6154937f3febf812aad6483bff45 (diff) | |
download | svg.js-d3f8d83551799db061a558537bc89dcbfd522c21.tar.gz svg.js-d3f8d83551799db061a558537bc89dcbfd522c21.zip |
evaluate window and document on access and not on import
Diffstat (limited to 'src')
-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 |
15 files changed, 46 insertions, 140 deletions
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 } |